Skip to content

Commit 921d2aa

Browse files
committed
update readme
1 parent 0e49077 commit 921d2aa

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

GeneralTree.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
*
3+
* @file GeneralTree.go
4+
* @author Max Base (maxbasecode@gmail.com)
5+
* @brief General Tree Implementation in Go
6+
* @version 0.1
7+
* @date 2022-12-14
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
112
package main
213

314
import "fmt"

README.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,140 @@
11
# General Tree Go
22

3-
Implentation of a general tree in *Go Programming Language**.
3+
Implentation of a general tree in *Go Programming Language**. A general tree is a tree data structure in which each node can have an arbitrary number of children. We are storing children in an array.
4+
5+
## Structure
6+
7+
**Types:**
8+
9+
```
10+
type Node struct
11+
Value interface{}
12+
Children []*Node
13+
}
14+
15+
type GeneralTree struct
16+
Root *Node
17+
}
18+
```
19+
20+
**Functions:**
21+
22+
```
23+
// Create a new general tree
24+
func NewGeneralTree() *GeneralTree
25+
return &GeneralTree{}
26+
}
27+
28+
// Create a new node
29+
func NewNode() *Node
30+
return &Node{}
31+
}
32+
```
33+
34+
**Methods:**
35+
36+
```go
37+
// Add a new node to the tree
38+
func (t *GeneralTree) AddNode(value interface{})
39+
40+
// Add a new node to the tree
41+
func (t *GeneralTree) AddNodeToParent(value int, parent *Node)
42+
43+
// Get the root node of the tree
44+
func (t *GeneralTree) GetRoot() *Node
45+
46+
// Get the children of a node
47+
func (t *GeneralTree) GetChildren(node *Node) []*Node
48+
49+
// Get the value of a node
50+
func (t *GeneralTree) GetValue(node *Node) interface{}
51+
52+
// Set the value of a node
53+
func (t *GeneralTree) SetValue(node *Node, value interface{})
54+
55+
// Get the number of nodes in the tree
56+
func (t *GeneralTree) GetSize() int
57+
func (t *GeneralTree) getSize(node *Node) int
58+
59+
// Get the height of the tree
60+
func (t *GeneralTree) getHeight() int
61+
func (t *Node) GetHeight(node *Node) int
62+
63+
// Print the tree
64+
func (t *GeneralTree) PrintTree()
65+
func (t *GeneralTree) printTree(node *Node, level int)
66+
```
67+
68+
## Usage
69+
70+
```go
71+
package main
72+
73+
import "fmt"
74+
75+
func main()
76+
// Create a new tree
77+
tree := NewGeneralTree()
78+
79+
// Add a root node
80+
tree.Root = &Node{Value: 1}
81+
82+
// Add a node to the root
83+
tree.AddNode(2)
84+
85+
// Add a node to the root
86+
tree.AddNode(3)
87+
88+
// Add a node to the root
89+
tree.AddNode(4)
90+
91+
// Add a node to the root
92+
tree.AddNode(5)
93+
94+
// Add a node to the root
95+
tree.AddNode(6)
96+
97+
// Adding a child to the first node
98+
tree.AddNodeToParent(7, tree.Root.Children[0])
99+
100+
// Adding a child to the first node
101+
tree.AddNodeToParent(8, tree.Root.Children[0])
102+
103+
// Adding a child to the first node
104+
tree.AddNodeToParent(9, tree.Root.Children[0])
105+
106+
// Print the tree
107+
tree.PrintTree()
108+
109+
// Get the size of the tree
110+
fmt.Println("Size of the tree: ", tree.GetSize())
111+
112+
// Get the height of the tree
113+
fmt.Println("Height of the tree: ", tree.getHeight())
114+
115+
// Get the root node
116+
fmt.Println("Root node: ", tree.GetRoot().Value)
117+
118+
// Get the children of the root node
119+
child := tree.GetChildren(tree.Root)
120+
fmt.Println("Children of the root node: ", tree.GetChildren(tree.Root))
121+
for _, c := range child
122+
fmt.Println("\tChildren of the root node: ", c.Value)
123+
}
124+
125+
// Get the value of the root node
126+
fmt.Println("Value of the root node: ", tree.GetValue(tree.Root))
127+
128+
// Set the value of the root node
129+
tree.SetValue(tree.Root, 10)
130+
131+
// Get the value of the root node
132+
fmt.Println("Value of the root node: ", tree.GetValue(tree.Root))
133+
}
134+
```
135+
136+
## License
137+
138+
This project is licensed under the GPL-3.0 License - see the [LICENSE](LICENSE) file for details.
139+
140+
Copyright (c) 2022, Max Base

0 commit comments

Comments
 (0)