1
+ function Node ( data , left , right ) {
2
+ this . data = data ;
3
+ this . left = left ;
4
+ this . right = right ;
5
+ this . show = show ;
6
+ }
7
+
8
+ function show ( ) {
9
+ return this . data ;
10
+ }
11
+
12
+ function BST ( ) {
13
+ this . root = null ;
14
+ this . insert = insert ;
15
+ this . inOrder = inOrder ;
16
+ this . preOrder = preOrder ;
17
+ this . postOrder = postOrder ;
18
+ this . getmin = getmin ;
19
+ this . getmax = getmax ;
20
+ this . find = find ;
21
+ this . remove = remove ;
22
+ this . removeNode = removeNode ;
23
+ this . getSmallest = getSmallest ;
24
+ }
25
+
26
+ function insert ( data ) {
27
+ var n = new Node ( data , null , null ) ;
28
+ if ( this . root == null ) {
29
+ this . root = n ;
30
+ }
31
+ else {
32
+ var current = this . root ;
33
+ var parent ;
34
+ while ( true ) {
35
+ parent = current ;
36
+ if ( data < current . data ) {
37
+ current = current . left ;
38
+ if ( current == null ) {
39
+ parent . left = n ;
40
+ break ;
41
+ }
42
+ }
43
+ else {
44
+ current = current . right ;
45
+ if ( current == null ) {
46
+ parent . right = n ;
47
+ break ;
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ function inOrder ( node ) {
55
+ if ( ! ( node == null ) ) {
56
+ inOrder ( node . left ) ;
57
+ putstr ( node . show ( ) + " " ) ;
58
+ inOrder ( node . right ) ;
59
+ }
60
+ }
61
+
62
+ function preOrder ( node ) {
63
+ if ( ! ( node == null ) ) {
64
+ putstr ( node . show ( ) + " " ) ;
65
+ preOrder ( node . left ) ;
66
+ preOrder ( node . right ) ;
67
+ }
68
+ }
69
+
70
+ function postOrder ( node ) {
71
+ if ( ! ( node == null ) ) {
72
+ postOrder ( node . left ) ;
73
+ postOrder ( node . right ) ;
74
+ putstr ( node . show ( ) + " " ) ;
75
+ }
76
+ }
77
+
78
+ function getmin ( ) {
79
+ var current = this . root ;
80
+ print ( "debug: " + current . data ) ;
81
+ while ( ! ( current . left == null ) ) {
82
+ current = current . left ;
83
+ }
84
+ return current . data ;
85
+ }
86
+
87
+ function getmax ( ) {
88
+ var current = this . root ;
89
+ while ( ! ( current . right == null ) ) {
90
+ current = current . right ;
91
+ }
92
+ return current . data ;
93
+ }
94
+
95
+ function find ( data ) {
96
+ var current = this . root ;
97
+ while ( current . data != data ) {
98
+ if ( data < current . data ) {
99
+ current = current . left ;
100
+ }
101
+ else {
102
+ current = current . right ;
103
+ }
104
+ if ( current == null ) {
105
+ return null ;
106
+ }
107
+ }
108
+ return current ;
109
+ }
110
+
111
+ function getSmallest ( node ) {
112
+ if ( node . left == null ) {
113
+ return node ;
114
+ }
115
+ else {
116
+ return getSmallest ( node . left ) ;
117
+ }
118
+ }
119
+
120
+ function remove ( data ) {
121
+ root = removeNode ( this . root , data ) ;
122
+ }
123
+
124
+ function removeNode ( node , data ) {
125
+ if ( node == null ) {
126
+ return null ;
127
+ }
128
+ if ( data == node . data ) {
129
+ // node has no children
130
+ if ( node . left == null && node . right == null ) {
131
+ return null ;
132
+ }
133
+ // node has no left child
134
+ if ( node . left == null ) {
135
+ return node . right ;
136
+ }
137
+ // node has no right child
138
+ if ( node . right == null ) {
139
+ return node . left ;
140
+ }
141
+ // node has two children
142
+ var tempNode = getSmallest ( node . right ) ;
143
+ node . data = tempNode . data ;
144
+ node . right = removeNode ( node . right , tempNode . data ) ;
145
+ return node ;
146
+ }
147
+ else if ( data < node . data ) {
148
+ node . left = removeNode ( node . left , data ) ;
149
+ return node ;
150
+ }
151
+ else {
152
+ node . right = removeNode ( node . right , data ) ;
153
+ return node ;
154
+ }
155
+ }
0 commit comments