@@ -9,10 +9,33 @@ export interface IAVLTree<T> {
99export class AVLTree < T > implements IAVLTree < T > {
1010 constructor ( private root : AVLNode < T > = null ) { }
1111
12- public insert ( node : AVLNode < T > ) : boolean {
13- // TODO: Implement insert
14- return true ;
12+ public insert ( node : AVLNode < T > , root : AVLNode < T > = this . root ) : boolean {
13+ if ( root == null ) {
14+ this . root = node ;
15+ return true ;
16+ }
17+
18+ if ( node . compareTo ( root ) === 0 ) {
19+ return false ;
20+ } else if ( node . compareTo ( root ) > 0 ) {
21+ if ( root . getRight ( ) == null ) {
22+ root . setRight ( node ) ;
23+ this . updateNodeAfterInsert ( node , root ) ;
24+ return true ;
25+ } else {
26+ return this . insert ( node , root . getRight ( ) ) ;
27+ }
28+ } else {
29+ if ( root . getLeft ( ) == null ) {
30+ root . setLeft ( node ) ;
31+ this . updateNodeAfterInsert ( node , root ) ;
32+ return true ;
33+ } else {
34+ return this . insert ( node , root . getLeft ( ) ) ;
35+ }
36+ }
1537 }
38+
1639 public search ( node : AVLNode < T > ) : boolean {
1740 // TODO: Implement search
1841 return true ;
@@ -22,9 +45,28 @@ export class AVLTree<T> implements IAVLTree<T> {
2245 return true ;
2346 }
2447
25- private detectRotation ( node : AVLNode < T > ) : void { }
26- private leftRotation ( node : AVLNode < T > ) : void { }
27- private rightRotation ( node : AVLNode < T > ) : void { }
28- private leftRightRotation ( node : AVLNode < T > ) : void { }
29- private rightLeftRotation ( node : AVLNode < T > ) : void { }
48+ public getRoot ( ) : AVLNode < T > {
49+ return this . root ;
50+ }
51+
52+ private updateNodeAfterInsert ( node : AVLNode < T > , parent : AVLNode < T > ) : void {
53+ node . setParent ( parent ) ;
54+ node . setNodeHeights ( ) ;
55+ parent . setNodeHeights ( )
56+ }
57+ private detectRotation ( node : AVLNode < T > ) : void {
58+ // TODO: add logic
59+ }
60+ private leftRotation ( node : AVLNode < T > ) : void {
61+ // TODO: add logic
62+ }
63+ private rightRotation ( node : AVLNode < T > ) : void {
64+ // TODO: add logic
65+ }
66+ private leftRightRotation ( node : AVLNode < T > ) : void {
67+ // TODO: add logic
68+ }
69+ private rightLeftRotation ( node : AVLNode < T > ) : void {
70+ // TODO: add logic
71+ }
3072}
0 commit comments