@@ -33,56 +33,6 @@ BST.prototype.delete = function (value) {
33
33
// What would be a good implementation:
34
34
// All these methods in the Node object
35
35
// or in the Tree object?
36
- let current = this . search ( value ) ,
37
- parent = null ,
38
- found = false
39
-
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
50
-
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
- }
63
-
64
- // delete a node with no children
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
- }
86
36
}
87
37
88
38
BST . prototype . search = function ( value ) {
@@ -93,7 +43,7 @@ BST.prototype.search = function (value) {
93
43
else current = current . right
94
44
}
95
45
return null
96
- l }
46
+ }
97
47
98
48
BST . prototype . min = function ( node ) {
99
49
while ( node . left ) node = node . left
0 commit comments