Skip to content

Commit 32f25ef

Browse files
committed
adding insert tests
1 parent 4c5afa3 commit 32f25ef

File tree

3 files changed

+128
-8
lines changed

3 files changed

+128
-8
lines changed

src/avl-tree/__test__/avl.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { AVLTree } from "../avl"
2+
import { AVLNode } from "../avl-node";
3+
4+
class Int extends AVLNode<number> {
5+
constructor(value: number) {
6+
super(value)
7+
}
8+
9+
public compareTo(o: AVLNode<number>): number {
10+
if (this.getValue() < o.getValue()) {
11+
return -1;
12+
} else if (this.getValue() > o.getValue()) {
13+
return 1;
14+
} else {
15+
return 0;
16+
}
17+
}
18+
}
19+
20+
describe("AVL Tree", () => {
21+
describe("Insert operation", () => {
22+
const avlTree = new AVLTree<number>();
23+
24+
it("should have null node", () => {
25+
const root = avlTree.getRoot();
26+
expect(root).toBe(null);
27+
});
28+
29+
it("should add a node as root node in an empty tree", () => {
30+
const isNodeInsterted = avlTree.insert(new Int(5));
31+
const root = avlTree.getRoot();
32+
expect(isNodeInsterted).toBeTruthy();
33+
expect(root.getValue()).toBe(5);
34+
expect(root.nodeHeight()).toBe(0);
35+
});
36+
37+
it("should add a left child to the avl tree", () => {
38+
const isNodeInsterted = avlTree.insert(new Int(3));
39+
const left = avlTree.getRoot().getLeft();
40+
expect(isNodeInsterted).toBeTruthy();
41+
expect(left.getValue()).toBe(3);
42+
expect(avlTree.getRoot().nodeHeight()).toBe(1);
43+
});
44+
45+
it("should add a right child to the avl tree", () => {
46+
const isNodeInsterted = avlTree.insert(new Int(9));
47+
const right = avlTree.getRoot().getRight();
48+
expect(isNodeInsterted).toBeTruthy();
49+
expect(right.getValue()).toBe(9);
50+
expect(avlTree.getRoot().nodeHeight()).toBe(1);
51+
});
52+
})
53+
})

src/avl-tree/avl-node.ts

+25
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ export abstract class AVLNode<T> {
44
private leftHeight: number;
55
private rightHeight: number;
66
private value: number;
7+
private parent: AVLNode<T>
8+
79

810
constructor(value: number) {
911
this.left = null;
1012
this.right = null;
13+
this.parent = null;
1114
this.leftHeight = 0;
1215
this.rightHeight = 0;
1316
this.value = value;
@@ -21,6 +24,20 @@ export abstract class AVLNode<T> {
2124
return (this.rightHeight > this.leftHeight) ? this.rightHeight : this.leftHeight;
2225
}
2326

27+
public setNodeHeights(): void {
28+
if (this.right == null) {
29+
this.rightHeight = 0;
30+
} else {
31+
this.rightHeight = this.right.nodeHeight() + 1;
32+
}
33+
34+
if (this.left == null) {
35+
this.leftHeight = 0;
36+
} else {
37+
this.leftHeight = this.left.nodeHeight() + 1;
38+
}
39+
}
40+
2441
public abstract compareTo(o: AVLNode<number>): number
2542

2643
/* #region(collapsed) Getter and Setter */
@@ -59,6 +76,14 @@ export abstract class AVLNode<T> {
5976
public getValue(): number {
6077
return this.value;
6178
}
79+
public getParent(): AVLNode<T> {
80+
return this.parent;
81+
}
82+
83+
public setParent(parent: AVLNode<T>): void {
84+
this.parent = parent;
85+
}
86+
6287
/* #endregion */
6388

6489
}

src/avl-tree/avl.ts

+50-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,33 @@ export interface IAVLTree<T> {
99
export 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

Comments
 (0)