Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc8a177

Browse files
add Size
1 parent f0314a4 commit dc8a177

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

‎BST/bst.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import "fmt"
44

55
func main() {
66
tree := New()
7-
fmt.Println("Insert 15 1 2 3 1 3")
7+
fmt.Println("Insert 15 1 2")
88
tree.Insert(15)
99
tree.Insert(1)
1010
tree.Insert(2)
11+
fmt.Println("Size: ", tree.Size())
12+
fmt.Println("Insert 3 1 3")
1113
tree.Insert(3)
1214
tree.Insert(1)
1315
tree.Insert(3)
16+
fmt.Println("Size: ", tree.Size())
1417
fmt.Println("Search 4: ", tree.Search(4))
1518
fmt.Println("Search 3: ", tree.Search(3))
1619
}
@@ -75,6 +78,11 @@ func (root *Node) insert(newNode *Node) {
7578
}
7679
}
7780

81+
// Size - return size tree
82+
func (tree *Bst) Size() int {
83+
return tree.size
84+
}
85+
7886
// Search element on tree
7987
func (tree *Bst) Search(value int) bool {
8088
tree.size--
@@ -94,3 +102,33 @@ func searchElement(root *Node, value int) bool {
94102
}
95103
return false
96104
}
105+
106+
// Print the tree in-order
107+
// Traverse the left sub-tree, root, right sub-tree
108+
func showInOrder(root *Node) {
109+
if root != nil {
110+
showInOrder(root.left)
111+
fmt.Println(root.key)
112+
showInOrder(root.right)
113+
}
114+
}
115+
116+
// Print the tree pre-order
117+
// Traverse the root, left sub-tree, right sub-tree
118+
func showPreOrder(root *Node) {
119+
if root != nil {
120+
fmt.Println(root.key)
121+
showInOrder(root.left)
122+
showInOrder(root.right)
123+
}
124+
}
125+
126+
// Print the tree post-order
127+
// Traverse left sub-tree, right sub-tree, root
128+
func showPostOrder(root *Node) {
129+
if root != nil {
130+
fmt.Println(root.key)
131+
showInOrder(root.left)
132+
showInOrder(root.right)
133+
}
134+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /