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 875f07b

Browse files
max & min element tree
1 parent a89c7d5 commit 875f07b

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

‎BST/bst.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func main() {
2727
fmt.Println("Size: ", tree.Size())
2828
fmt.Println("Show: ")
2929
tree.Show()
30+
fmt.Println("Min element: ")
31+
tree.FindMin()
32+
fmt.Println("Max element: ")
33+
tree.FindMax()
3034
}
3135

3236
// Node is a representation of a single node in tree. (recursive ADT)
@@ -49,7 +53,7 @@ Binary Search Tree ADT Operations
4953
* + Search(k): поиск значения элемента k в структуре, есть он или нет.
5054
* FindMax(): поиск максимального значения.
5155
* FindMin(): поиск минимального значения.
52-
* Show & Size(): печать дерева и размер.
56+
* + Show & Size(): печать дерева и размер.
5357
*/
5458

5559
// New - Construtor BST
@@ -117,15 +121,43 @@ func searchElement(root *Node, value int) bool {
117121

118122
// Show the tree (Print the tree in-order)
119123
func (tree *Bst) Show() {
120-
showInOrder(tree.root)
124+
printNode(tree.root)
121125
}
122126

123127
// Print the tree in-order
124128
// Traverse the left sub-tree, root, right sub-tree
125-
func showInOrder(root *Node) {
129+
func printNode(root *Node) {
126130
if root != nil {
127-
showInOrder(root.left)
131+
printNode(root.left)
128132
fmt.Println(root.key)
129-
showInOrder(root.right)
133+
printNode(root.right)
134+
}
135+
}
136+
137+
// FindMin - print min element tree
138+
func (tree *Bst) FindMin() {
139+
minValue(tree.root)
140+
}
141+
142+
func minValue(root *Node) {
143+
if root != nil {
144+
if root.left == nil {
145+
fmt.Println(root.key)
146+
}
147+
minValue(root.left)
148+
}
149+
}
150+
151+
// FindMax - print max element tree
152+
func (tree *Bst) FindMax() {
153+
maxValue(tree.root)
154+
}
155+
156+
func maxValue(root *Node) {
157+
if root != nil {
158+
if root.right == nil {
159+
fmt.Println(root.key)
160+
}
161+
maxValue(root.right)
130162
}
131163
}

0 commit comments

Comments
(0)

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