-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheightBst.go
117 lines (102 loc) · 1.86 KB
/
heightBst.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Find the height a of Binary Search Tree.
// The height of a binary search tree is the maximum number of edges from root node to the leaf node
package main
import (
"fmt"
"github.com/souvikhaldar/go-ds/bst"
"github.com/souvikhaldar/go-ds/queue"
"github.com/souvikhaldar/go-ds/stack"
)
func main() {
b := bst.NewIterativeBst()
b.Insert(4)
b.Insert(2)
b.Insert(1)
b.Insert(0)
b.Insert(5)
b.Insert(6)
b.Insert(7)
b.Insert(8)
b.Insert(9)
temp := b.Root
if temp == nil {
fmt.Println(bst.ErrEmptyTree)
return
}
heights := make([]int, 0)
var edge int
q := queue.NewQueue()
s := stack.NewStack()
visited := make(map[int]struct{})
q.Enqueue(temp)
for !q.IsEmpty() {
t, err := q.Dequeue()
if err != nil {
break
}
temp, ok := t.(*bst.Node)
if !ok {
break
}
_, ok = visited[temp.Value]
if !ok {
if temp.Value != b.Root.Value {
visited[temp.Value] = struct{}{}
edge++
}
} else {
t, err := q.Dequeue()
if err != nil {
break
}
temp, ok = t.(*bst.Node)
if !ok {
break
}
}
for temp != nil || !s.IsEmpty() {
if _, ok := visited[temp.Value]; ok {
t, err := s.Pop()
if err != nil {
break
}
temp, ok = t.(*bst.Node)
if !ok {
break
}
}
if bst.IsALeafNode(temp) {
heights = append(heights, edge)
t, err := q.Dequeue()
if err != nil {
break
}
temp, ok = t.(*bst.Node)
if !ok {
break
}
// traverse a new path to leaf
edge = 0
} else if bst.IsNodeWithOneChild(temp) {
edge++
// go either left sub-tree or right
if temp.Left != nil {
temp = temp.Left
} else {
temp = temp.Right
}
} else {
edge++
// go the left subtree and hold the right
temp = temp.Left
}
}
}
max := 0
for _, i := range heights {
if i > max {
max = i
}
}
fmt.Println("Height of the bst is: ", max)
}