Skip to content

Feature/#25 avl tree #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Trigger production build
Data structures covered so far -

- [Binary Search Tree](#binary-search-tree)
- [AVL Tree](#avl-tree) - In Progress
- [Graph](#graph)
- [Queue](#queue)
- [Linked List](#link-list)
Expand Down Expand Up @@ -130,6 +131,8 @@ Delete elements from binary search tree
bst.delete(10);
bst.delete(20);
```
# <a name="avl-tree"></a>AVL Tree
AVL Tree Docs.

# <a name="graph"></a> Graph

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prepare": "npm run build & npm run dev",
"publish:npm": "npm publish --access public",
"test:jest": "jest",
"test:jest:watch": "jest --watch",
"lint": "eslint src/ --ext .ts"
},
"jest": {
Expand All @@ -44,7 +45,8 @@
"es6",
"queue",
"graph",
"binary search tree"
"binary search tree",
"avl tree"
],
"author": {
"name": "Abhishek Prakash",
Expand Down
59 changes: 59 additions & 0 deletions src/avl-tree/__test__/avl-node.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { AVLNode } from "../avl-node";

class Int extends AVLNode<number> {
constructor(value: number) {
super(value)
}

public compareTo(o: AVLNode<number>): number {
if (this.getValue() < o.getValue()) {
return -1;
} else if (this.getValue() > o.getValue()) {
return 1;
} else {
return 0;
}
}
}

describe("Test for AVLNode", () => {
let node: AVLNode<number>;
let node1: AVLNode<number>;
let node2: AVLNode<number>;
let node3: AVLNode<number>;

beforeEach(() => {
node = new Int(5);
node1 = new Int(4);
node2 = new Int(5);
node3 = new Int(6);
});

it("should create an AVL node of type number", () => {
expect(node).toBeDefined();
expect(node.getValue()).toBe(5);
expect(node.getLeft()).toBe(null);
expect(node.getRight()).toBe(null);
expect(node.getLeftHeight()).toBe(0);
expect(node.getRightHeight()).toBe(0);
expect(node.nodeHeight()).toBe(0);
expect(node.balanceFactor()).toBe(0);
expect(node.compareTo(node1)).toBe(1);
expect(node.compareTo(node2)).toBe(0);
expect(node.compareTo(node3)).toBe(-1);
});

it("should calculate the hight correctly", () => {
node.setLeft(node1);
node.setRight(node3);
node.setLeftHeight(1);
node.setRightHeight(1)

expect(node.getLeft()).toEqual(node1);
expect(node.getRight()).toEqual(node3);
expect(node.getLeftHeight()).toBe(1);
expect(node.getRightHeight()).toBe(1);
expect(node.nodeHeight()).toBe(1);
expect(node.balanceFactor()).toBe(0);
})
});
58 changes: 58 additions & 0 deletions src/avl-tree/__test__/avl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AVLTree } from "../avl"
import { AVLNode } from "../avl-node";

class Int extends AVLNode<number> {
constructor(value: number) {
super(value)
}

public compareTo(o: AVLNode<number>): number {
if (this.getValue() < o.getValue()) {
return -1;
} else if (this.getValue() > o.getValue()) {
return 1;
} else {
return 0;
}
}
}

describe("AVL Tree", () => {
describe("Insert operation", () => {
const avlTree = new AVLTree<number>();

it("should have null node", () => {
const root = avlTree.getRoot();
expect(root).toBe(null);
});

it("should add a node as root node in an empty tree", () => {
const isNodeInsterted = avlTree.insert(new Int(5));
const root = avlTree.getRoot();
expect(isNodeInsterted).toBeTruthy();
expect(root.getValue()).toBe(5);
expect(root.nodeHeight()).toBe(0);
});

it("should add a left child to the avl tree", () => {
const isNodeInsterted = avlTree.insert(new Int(3));
const left = avlTree.getRoot().getLeft();
expect(isNodeInsterted).toBeTruthy();
expect(left.getValue()).toBe(3);
expect(avlTree.getRoot().nodeHeight()).toBe(1);
});

it("should add a right child to the avl tree", () => {
const isNodeInsterted = avlTree.insert(new Int(9));
const right = avlTree.getRoot().getRight();
expect(isNodeInsterted).toBeTruthy();
expect(right.getValue()).toBe(9);
expect(avlTree.getRoot().nodeHeight()).toBe(1);
});

it("should not add node with the same value", () => {
const isNodeInsterted = avlTree.insert(new Int(9));
expect(isNodeInsterted).toBeFalsy();
})
})
})
89 changes: 89 additions & 0 deletions src/avl-tree/avl-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
export abstract class AVLNode<T> {
private left: AVLNode<T>;
private right: AVLNode<T>;
private leftHeight: number;
private rightHeight: number;
private value: number;
private parent: AVLNode<T>


constructor(value: number) {
this.left = null;
this.right = null;
this.parent = null;
this.leftHeight = 0;
this.rightHeight = 0;
this.value = value;
}

public balanceFactor(): number {
return (this.rightHeight - this.leftHeight);
}

public nodeHeight(): number {
return (this.rightHeight > this.leftHeight) ? this.rightHeight : this.leftHeight;
}

public setNodeHeights(): void {
if (this.right == null) {
this.rightHeight = 0;
} else {
this.rightHeight = this.right.nodeHeight() + 1;
}

if (this.left == null) {
this.leftHeight = 0;
} else {
this.leftHeight = this.left.nodeHeight() + 1;
}
}

public abstract compareTo(o: AVLNode<number>): number

/* #region(collapsed) Getter and Setter */
public getLeft(): AVLNode<T> {
return this.left;
}

public setLeft(left: AVLNode<T>): void {
this.left = left;
}

public getRight(): AVLNode<T> {
return this.right;
}

public setRight(right: AVLNode<T>): void {
this.right = right;
}

public getLeftHeight(): number {
return this.leftHeight;
}

public setLeftHeight(leftHeight: number): void {
this.leftHeight = leftHeight;
}

public getRightHeight(): number {
return this.rightHeight;
}

public setRightHeight(rightHeight: number): void {
this.rightHeight = rightHeight;
}

public getValue(): number {
return this.value;
}
public getParent(): AVLNode<T> {
return this.parent;
}

public setParent(parent: AVLNode<T>): void {
this.parent = parent;
}

/* #endregion */

}
72 changes: 72 additions & 0 deletions src/avl-tree/avl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { AVLNode } from "./avl-node";

export interface IAVLTree<T> {
insert(node: AVLNode<T>): boolean
search(node: AVLNode<T>): boolean
delete(node: AVLNode<T>): boolean
}

export class AVLTree<T> implements IAVLTree<T> {
constructor(private root: AVLNode<T> = null) { }

public insert(node: AVLNode<T>, root: AVLNode<T> = this.root): boolean {
if (root == null) {
this.root = node;
return true;
}

if (node.compareTo(root) === 0) {
return false;
} else if (node.compareTo(root) > 0) {
if (root.getRight() == null) {
root.setRight(node);
this.updateNodeAfterInsert(node, root);
return true;
} else {
return this.insert(node, root.getRight());
}
} else {
if (root.getLeft() == null) {
root.setLeft(node);
this.updateNodeAfterInsert(node, root);
return true;
} else {
return this.insert(node, root.getLeft());
}
}
}

public search(node: AVLNode<T>): boolean {
// TODO: Implement search
return true;
}
public delete(node: AVLNode<T>): boolean {
// TODO: Implement delete
return true;
}

public getRoot(): AVLNode<T> {
return this.root;
}

private updateNodeAfterInsert(node: AVLNode<T>, parent: AVLNode<T>): void {
node.setParent(parent);
node.setNodeHeights();
parent.setNodeHeights()
}
private detectRotation(node: AVLNode<T>): void {
// TODO: add logic
}
private leftRotation(node: AVLNode<T>): void {
// TODO: add logic
}
private rightRotation(node: AVLNode<T>): void {
// TODO: add logic
}
private leftRightRotation(node: AVLNode<T>): void {
// TODO: add logic
}
private rightLeftRotation(node: AVLNode<T>): void {
// TODO: add logic
}
}
Loading