2
2
3
3
function Node ( data ) {
4
4
this . key = data
5
+ this . parent = null
5
6
this . left = null
6
7
this . right = null
7
8
}
@@ -24,35 +25,64 @@ BST.prototype.insert = function (value) {
24
25
if ( current === null ) this . root = node
25
26
else if ( node . key < current . key ) current . left = node
26
27
else current . right = node
28
+ node . parent = current
27
29
}
28
30
29
31
BST . prototype . delete = function ( value ) {
30
- // Need help with this one, as most other languages implementations
31
- // depend on the parent being stored in the node
32
- // Is there another way of traversing the three in O(lg n) without
33
- // that ?
34
- let current = this . root ,
32
+ // Need help with this one
33
+ // What would be a good implementation:
34
+ // All these methods in the Node object
35
+ // or in the Tree object ?
36
+ let current = this . search ( value ) ,
35
37
parent = null ,
36
38
found = false
37
39
38
- while ( ! found && current ) {
39
- parent = current
40
- if ( value < current . key ) current = current . left
41
- else if ( value > current . key ) current = current . right
42
- else found = true
43
- }
40
+ // while (!found && current) {
41
+ // parent = current
42
+ // if (value < current.key) current = current.left
43
+ // else if (value > current.key) current = current.right
44
+ // else found = true
45
+ // }
46
+
47
+ // if (!found) return
48
+
49
+ if ( ! current ) return null
44
50
45
- if ( ! found ) return
51
+ if ( current === this . root ) {
52
+ let pseudoRoot = new Node ( 0 )
53
+ pseudoRoot . left = this . root
54
+ this . root . parent = pseudoRoot
55
+ let deleted = this . delete ( this . root . key )
56
+ this . root = pseudoRoot . left
57
+ if ( this . root ) this . root . parent = null
58
+
59
+ return deleted
60
+ } else {
61
+ return this . delete ( current )
62
+ }
46
63
47
64
// delete a node with no children
48
- // if (!current.left || !current.right) {
49
- // if (current === parent.left) {
50
- // parent.left = current.left || current.right
51
- // if (!parent.left) {
52
- // parent.left.parent = parent
53
- // }
54
- // }
55
- // }
65
+ if ( ! current . left || ! current . right ) {
66
+ if ( current === parent . left ) {
67
+ parent . left = current . left || current . right
68
+ if ( parent . left ) {
69
+ parent . left . parent = parent
70
+ }
71
+ } else {
72
+ parent . right = current . left || current . right
73
+ if ( parent . right ) {
74
+ parent . right . parent = parent
75
+ }
76
+ }
77
+ return current
78
+ } else {
79
+ let s = this . sucessor ( current . key ) ,
80
+ temp = current . key
81
+
82
+ current . key = s . key
83
+ s . key = temp
84
+ return this . delete ( s . key )
85
+ }
56
86
}
57
87
58
88
BST . prototype . search = function ( value ) {
@@ -65,16 +95,34 @@ BST.prototype.search = function (value) {
65
95
return null
66
96
l }
67
97
68
- BST . prototype . min = function ( ) {
69
- let current = this . root . left
70
- while ( current . left ) current = current . left
71
- return current
98
+ BST . prototype . min = function ( node ) {
99
+ while ( node . left ) node = node . left
100
+ return node
101
+ }
102
+
103
+ BST . prototype . max = function ( node ) {
104
+ while ( node . right ) node = node . right
105
+ return node
72
106
}
73
107
74
- BST . prototype . max = function ( ) {
75
- let current = this . root . right
76
- while ( current . right ) current = current . right
77
- return current
108
+ BST . prototype . sucessor = function ( value ) {
109
+ let current = this . search ( value )
110
+ if ( current . right ) {
111
+ return this . min ( current . right )
112
+ }
113
+ while ( current . parent && current === current . parent . right )
114
+ current = current . parent
115
+ return current . parent
116
+ }
117
+
118
+ BST . prototype . predecessor = function ( value ) {
119
+ let current = this . search ( value )
120
+ if ( current . left ) {
121
+ return this . max ( current . left )
122
+ }
123
+ while ( current . parent && current === current . parent . left )
124
+ current = current . parent
125
+ return current . parent
78
126
}
79
127
80
128
BST . prototype . length = function ( ) {
@@ -110,9 +158,16 @@ test('BST', assert => {
110
158
bst . insert ( 8 )
111
159
bst . insert ( 15 )
112
160
assert . deepEqual ( bst . search ( 15 ) . key , 15 )
113
- assert . deepEqual ( bst . min ( ) . key , 4 )
114
- assert . deepEqual ( bst . max ( ) . key , 15 )
161
+ assert . deepEqual ( bst . min ( bst . root ) . key , 4 )
162
+ assert . deepEqual ( bst . max ( bst . root ) . key , 15 )
115
163
assert . deepEqual ( bst . toArray ( ) , [ 4 , 8 , 12 , 15 ] )
116
164
assert . deepEqual ( bst . length ( ) , 4 )
165
+ assert . deepEqual ( bst . root . left . parent . key , 12 )
166
+ assert . deepEqual ( bst . sucessor ( 12 ) . key , 15 )
167
+ assert . deepEqual ( bst . sucessor ( 8 ) . key , 12 )
168
+ assert . deepEqual ( bst . sucessor ( 15 ) , null )
169
+ assert . deepEqual ( bst . predecessor ( 12 ) . key , 8 )
170
+ assert . deepEqual ( bst . predecessor ( 8 ) . key , 4 )
171
+ assert . deepEqual ( bst . predecessor ( 4 ) , null )
117
172
assert . end ( )
118
173
} )
0 commit comments