1-
21class Node {
3- left : any ;
4- right : any ;
2+ left : Node | null ;
3+ right : Node | null ; ;
54 value : number ;
65 constructor ( value : number ) {
76 this . left = null ;
@@ -10,28 +9,132 @@ class Node {
109 }
1110}
1211
13- class BinarySearchTree {
14- root : any
12+ export class BinarySearchTree {
13+ root : Node | null ;
1514 constructor ( ) {
1615 this . root = null ;
1716 }
1817 insert ( value : number ) {
18+ const newNode = new Node ( value ) ;
19+
1920 if ( this . root === null ) {
20- this . root = new Node ( value )
21+ this . root = newNode ;
22+ } else {
23+ let currentNode = this . root ;
24+ while ( true ) {
25+ if ( currentNode . value > value ) {
26+ // Left
27+ if ( ! currentNode . left ) {
28+ currentNode . left = newNode ;
29+ return this ;
30+ }
31+ currentNode = currentNode . left ;
32+ } else {
33+ // Right
34+ if ( ! currentNode . right ) {
35+ currentNode . right = newNode ;
36+ return this ;
37+ }
38+ currentNode = currentNode . right ;
39+ }
40+ }
2141 }
22- // Làm sao để lặp qua (Chẳng lẽ sử dụng recursion)
23-
24- // So sánh
25-
26- // Gán giá trị
27-
2842 }
2943 lookup ( value : number ) {
44+ if ( ! this . root ) {
45+ return false ;
46+ }
47+ let currentNode : Node | null = this . root ;
3048
49+ while ( currentNode ) {
50+ if ( currentNode . value > value ) {
51+ // Left
52+ currentNode = currentNode . left ;
53+ } else if ( currentNode . value < value ) {
54+ // Right
55+ currentNode = currentNode . right ;
56+ } else if ( currentNode . value === value ) {
57+ return currentNode ;
58+ }
59+ }
60+ return false ;
3161 }
3262
3363 remove ( value : number ) {
64+ if ( ! this . root ) {
65+ return false ;
66+ }
67+ let currentNode : Node | null = this . root ;
68+ let parentNode = null ;
69+ while ( currentNode ) {
70+ if ( value < currentNode . value ) {
71+ parentNode = currentNode ;
72+ currentNode = currentNode . left ;
73+ } else if ( value > currentNode . value ) {
74+ parentNode = currentNode ;
75+ currentNode = currentNode . right ;
76+ } else if ( currentNode . value === value ) {
77+ //We have a match, get to work!
3478
79+ //Option 1: No right child:
80+ if ( currentNode . right === null ) {
81+ if ( parentNode === null ) {
82+ this . root = currentNode . left ;
83+ } else {
84+ //if parent > current value, make current left child a child of parent
85+ if ( currentNode . value < parentNode . value ) {
86+ parentNode . left = currentNode . left ;
87+
88+ //if parent < current value, make left child a right child of parent
89+ } else if ( currentNode . value > parentNode . value ) {
90+ parentNode . right = currentNode . left ;
91+ }
92+ }
93+
94+ //Option 2: Right child which doesnt have a left child
95+ } else if ( currentNode . right . left === null ) {
96+ currentNode . right . left = currentNode . left ;
97+ if ( parentNode === null ) {
98+ this . root = currentNode . right ;
99+ } else {
100+ //if parent > current, make right child of the left the parent
101+ if ( currentNode . value < parentNode . value ) {
102+ parentNode . left = currentNode . right ;
103+
104+ //if parent < current, make right child a right child of the parent
105+ } else if ( currentNode . value > parentNode . value ) {
106+ parentNode . right = currentNode . right ;
107+ }
108+ }
109+
110+ //Option 3: Right child that has a left child
111+ } else {
112+ //find the Right child's left most child
113+ let leftmost = currentNode . right . left ;
114+ let leftmostParent = currentNode . right ;
115+ while ( leftmost . left !== null ) {
116+ leftmostParent = leftmost ;
117+ leftmost = leftmost . left ;
118+ }
119+
120+ //Parent's left subtree is now leftmost's right subtree
121+ leftmostParent . left = leftmost . right ;
122+ leftmost . left = currentNode . left ;
123+ leftmost . right = currentNode . right ;
124+
125+ if ( parentNode === null ) {
126+ this . root = leftmost ;
127+ } else {
128+ if ( currentNode . value < parentNode . value ) {
129+ parentNode . left = leftmost ;
130+ } else if ( currentNode . value > parentNode . value ) {
131+ parentNode . right = leftmost ;
132+ }
133+ }
134+ }
135+ return true ;
136+ }
137+ }
35138 }
36139}
37140
@@ -46,13 +149,13 @@ tree.insert(1);
46149tree . remove ( 170 ) ;
47150JSON . stringify ( traverse ( tree . root ) ) ;
48151console . log ( tree . lookup ( 20 ) ) ;
49-
50152// 9
51153// 4 20
52154//1 6 15 170
53155
54- function traverse ( node : any ) {
55- const tree : any = { value : node . value } ;
156+ function traverse ( node : Node | null ) {
157+ if ( ! node ) return null
158+ const tree : Node | null = { value : node . value , left : null , right : null } ;
56159 tree . left = node . left === null ? null : traverse ( node . left ) ;
57160 tree . right = node . right === null ? null : traverse ( node . right ) ;
58161 return tree ;
0 commit comments