11
11
*/
12
12
13
13
// class Node
14
- const Node = ( function ( ) {
14
+ const Node = ( function Node ( ) {
15
15
// Node in the tree
16
16
function Node ( val ) {
17
17
this . value = val
@@ -23,9 +23,9 @@ const Node = (function () {
23
23
Node . prototype . search = function ( val ) {
24
24
if ( this . value === val ) {
25
25
return this
26
- } else if ( val < this . value && this . left != null ) {
26
+ } else if ( val < this . value && this . left !== null ) {
27
27
return this . left . search ( val )
28
- } else if ( val > this . value && this . right != null ) {
28
+ } else if ( val > this . value && this . right !== null ) {
29
29
return this . right . search ( val )
30
30
}
31
31
return null
@@ -34,34 +34,73 @@ const Node = (function () {
34
34
// Visit a node
35
35
Node . prototype . visit = function ( ) {
36
36
// Recursively go left
37
- if ( this . left != null ) {
37
+ if ( this . left !== null ) {
38
38
this . left . visit ( )
39
39
}
40
40
// Print out value
41
41
console . log ( this . value )
42
42
// Recursively go right
43
- if ( this . right != null ) {
43
+ if ( this . right !== null ) {
44
44
this . right . visit ( )
45
45
}
46
46
}
47
47
48
48
// Add a node
49
49
Node . prototype . addNode = function ( n ) {
50
50
if ( n . value < this . value ) {
51
- if ( this . left == null ) {
51
+ if ( this . left === null ) {
52
52
this . left = n
53
53
} else {
54
54
this . left . addNode ( n )
55
55
}
56
56
} else if ( n . value > this . value ) {
57
- if ( this . right == null ) {
57
+ if ( this . right === null ) {
58
58
this . right = n
59
59
} else {
60
60
this . right . addNode ( n )
61
61
}
62
62
}
63
63
}
64
64
65
+ // remove a node
66
+ Node . prototype . removeNode = function ( val ) {
67
+ if ( val === this . value ) {
68
+ if ( ! this . left && ! this . right ) {
69
+ return null
70
+ } else {
71
+ if ( this . left ) {
72
+ const leftMax = maxVal ( this . left )
73
+ this . value = leftMax
74
+ this . left = this . left . removeNode ( leftMax )
75
+ } else {
76
+ const rightMin = minVal ( this . right )
77
+ this . value = rightMin
78
+ this . right = this . right . removeNode ( rightMin )
79
+ }
80
+ }
81
+ } else if ( val < this . value ) {
82
+ this . left = this . left && this . left . removeNode ( val )
83
+ } else if ( val > this . value ) {
84
+ this . right = this . right && this . right . removeNode ( val )
85
+ }
86
+ return this
87
+ }
88
+
89
+ // find maximum value in the tree
90
+ const maxVal = function ( node ) {
91
+ if ( ! node . right ) {
92
+ return node . value
93
+ }
94
+ return maxVal ( node . right )
95
+ }
96
+
97
+ // find minimum value in the tree
98
+ const minVal = function ( node ) {
99
+ if ( ! node . left ) {
100
+ return node . value
101
+ }
102
+ return minVal ( node . left )
103
+ }
65
104
// returns the constructor
66
105
return Node
67
106
} ( ) )
@@ -75,6 +114,10 @@ const Tree = (function () {
75
114
76
115
// Inorder traversal
77
116
Tree . prototype . traverse = function ( ) {
117
+ if ( ! this . root ) {
118
+ console . log ( 'No nodes are there in the tree till now' )
119
+ return
120
+ }
78
121
this . root . visit ( )
79
122
}
80
123
@@ -91,13 +134,19 @@ const Tree = (function () {
91
134
// Add a new value to the tree
92
135
Tree . prototype . addValue = function ( val ) {
93
136
const n = new Node ( val )
94
- if ( this . root == null ) {
137
+ if ( this . root === null ) {
95
138
this . root = n
96
139
} else {
97
140
this . root . addNode ( n )
98
141
}
99
142
}
100
143
144
+ // remove a value from the tree
145
+ Tree . prototype . removeValue = function ( val ) {
146
+ // remove something if root exists
147
+ this . root = this . root && this . root . removeNode ( val )
148
+ }
149
+
101
150
// returns the constructor
102
151
return Tree
103
152
} ( ) )
@@ -112,3 +161,6 @@ bst.addValue(8)
112
161
bst . addValue ( 4 )
113
162
bst . traverse ( )
114
163
bst . search ( 8 )
164
+ bst . removeValue ( 3 )
165
+ bst . removeValue ( 8 )
166
+ bst . traverse ( )
0 commit comments