Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0213ca

Browse files
Merge pull request #6 from dabhitapan/main
Added js Tree and liskedList to DS
2 parents c5b2b7f + 8cdae8f commit a0213ca

File tree

4 files changed

+655
-0
lines changed

4 files changed

+655
-0
lines changed

‎LinkedList/doublyLinkedList.js‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//DOUBLY LINKED
2+
class Node {
3+
constructor(value) {
4+
this.value = value;
5+
this.prev = null;
6+
this.next = null;
7+
}
8+
}
9+
class DoublyLinkedList {
10+
constructor() {
11+
this.head = null;
12+
this.tail = null;
13+
this.length = 0;
14+
}
15+
16+
push(val) {
17+
const newNode = new Node(val);
18+
if (!this.head) {
19+
this.head = newNode;
20+
this.tail = newNode;
21+
} else {
22+
this.tail.next = newNode;
23+
newNode.prev = this.tail;
24+
this.tail = newNode;
25+
}
26+
this.length++;
27+
return this;
28+
}
29+
30+
pop() {
31+
//in case of empty list
32+
if (this.length === 0) {
33+
return false;
34+
}
35+
//get popped node
36+
const popped = this.tail;
37+
//save newTail to a variable (could be null)
38+
const newTail = this.tail.prev;
39+
//if newTail is not null
40+
if (newTail) {
41+
//sever connection to popped node
42+
newTail.next = null;
43+
//sever connection from popped node
44+
this.tail.prev = null;
45+
//in case of 1 length list
46+
} else {
47+
//make sure to edit head in case newTail is null
48+
this.head = null;
49+
}
50+
//assign new tail (could be null)
51+
this.tail = newTail;
52+
// subtract length
53+
this.length--;
54+
55+
return popped;
56+
}
57+
58+
shift() {
59+
//in case list is empty
60+
if (!this.head) {
61+
return false;
62+
}
63+
//save shifted node to variable
64+
const shiftedNode = this.head;
65+
//make the new head the next (might be null)
66+
const newHead = this.head.next; //might be null
67+
//if list is more than 1
68+
if (this.head !== this.tail) {
69+
newHead.prev = null;
70+
shiftedNode.next = null;
71+
} else {
72+
this.tail = null;
73+
}
74+
this.head = newHead;
75+
this.length--;
76+
return shiftedNode;
77+
}
78+
79+
unshift(val) {
80+
const newNode = new Node(val);
81+
if (!this.head) {
82+
this.head = newNode;
83+
this.tail = newNode;
84+
} else {
85+
this.head.prev = newNode;
86+
newNode.next = this.head;
87+
this.head = newNode;
88+
}
89+
this.length++;
90+
return this;
91+
}
92+
93+
insertAtIndex(index, val) {
94+
//if index doesn't exist
95+
if (index > this.length) {
96+
return false;
97+
}
98+
if (index === 0) {
99+
this.unshift(val);
100+
} else if (index === this.length) {
101+
this.push(val);
102+
} else {
103+
const newNode = new Node(val);
104+
const after = this.accessAtIndex(index);
105+
const before = after.prev;
106+
after.prev = newNode;
107+
before.next = newNode;
108+
newNode.next = after;
109+
newNode.prev = before;
110+
this.length++;
111+
}
112+
return this;
113+
}
114+
115+
removeAtIndex(index) {
116+
let removedNode;
117+
if (index >= this.length) {
118+
return false;
119+
}
120+
if (index == 0) {
121+
removedNode = this.shift();
122+
} else if (index == this.length - 1) {
123+
removedNode = this.pop();
124+
} else {
125+
removedNode = this.getNodeAtIndex(index);
126+
const after = removedNode.next;
127+
const before = removedNode.prev;
128+
removedNode.next = null;
129+
removedNode.prev = null;
130+
before.next = after;
131+
after.prev = before;
132+
this.length--;
133+
}
134+
return removedNode;
135+
}
136+
137+
getNodeAtIndex(index) {
138+
if (index >= this.length || index < 0) {
139+
return false;
140+
}
141+
let currentIndex = 0;
142+
let currentNode = this.head;
143+
while (currentIndex !== index) {
144+
currentNode = currentNode.next;
145+
currentIndex++;
146+
}
147+
return currentNode;
148+
}
149+
150+
setNodeAtIndex(index, val) {
151+
const foundNode = this.getNodeAtIndex(index);
152+
if (foundNode) {
153+
foundNode.value = val;
154+
return foundNode;
155+
}
156+
return null;
157+
}
158+
159+
printList() {
160+
console.log(list);
161+
if (this.head) {
162+
let current = this.head;
163+
while (current.next) {
164+
console.log(current);
165+
current = current.next;
166+
}
167+
console.log(current);
168+
} else {
169+
console.log("empty list");
170+
}
171+
}
172+
}

‎Tree/avlTree.js‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Create node
2+
const Node = function (item) {
3+
this.item = item;
4+
this.height = 1;
5+
this.left = null;
6+
this.right = null;
7+
};
8+
9+
//AVL Tree
10+
const AVLTree = function () {
11+
let root = null;
12+
13+
//return height of the node
14+
this.height = (N) => {
15+
if (N === null) {
16+
return 0;
17+
}
18+
19+
return N.height;
20+
};
21+
22+
//right rotate
23+
this.rightRotate = (y) => {
24+
let x = y.left;
25+
let T2 = x.right;
26+
x.right = y;
27+
y.left = T2;
28+
y.height = Math.max(this.height(y.left), this.height(y.right)) + 1;
29+
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
30+
return x;
31+
};
32+
33+
//left rotate
34+
this.leftRotate = (x) => {
35+
let y = x.right;
36+
let T2 = y.left;
37+
y.left = x;
38+
x.right = T2;
39+
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
40+
y.height = Math.max(this.height(y.left), this.height(y.right)) + 1;
41+
return y;
42+
};
43+
44+
// get balance factor of a node
45+
this.getBalanceFactor = (N) => {
46+
if (N == null) {
47+
return 0;
48+
}
49+
50+
return this.height(N.left) - this.height(N.right);
51+
};
52+
53+
// helper function to insert a node
54+
const insertNodeHelper = (node, item) => {
55+
// find the position and insert the node
56+
if (node === null) {
57+
return new Node(item);
58+
}
59+
60+
if (item < node.item) {
61+
node.left = insertNodeHelper(node.left, item);
62+
} else if (item > node.item) {
63+
node.right = insertNodeHelper(node.right, item);
64+
} else {
65+
return node;
66+
}
67+
68+
// update the balance factor of each node
69+
// and, balance the tree
70+
node.height = 1 + Math.max(this.height(node.left), this.height(node.right));
71+
72+
let balanceFactor = this.getBalanceFactor(node);
73+
74+
if (balanceFactor > 1) {
75+
if (item < node.left.item) {
76+
return this.rightRotate(node);
77+
} else if (item > node.left.item) {
78+
node.left = this.leftRotate(node.left);
79+
return this.rightRotate(node);
80+
}
81+
}
82+
83+
if (balanceFactor < -1) {
84+
if (item > node.right.item) {
85+
return this.leftRotate(node);
86+
} else if (item < node.right.item) {
87+
node.right = this.rightRotate(node.right);
88+
return this.leftRotate(node);
89+
}
90+
}
91+
92+
return node;
93+
};
94+
95+
// insert a node
96+
this.insertNode = (item) => {
97+
// console.log(root);
98+
root = insertNodeHelper(root, item);
99+
};
100+
101+
//get node with minimum value
102+
this.nodeWithMimumValue = (node) => {
103+
let current = node;
104+
while (current.left !== null) {
105+
current = current.left;
106+
}
107+
return current;
108+
};
109+
110+
// delete helper
111+
const deleteNodeHelper = (root, item) => {
112+
// find the node to be deleted and remove it
113+
if (root == null) {
114+
return root;
115+
}
116+
if (item < root.item) {
117+
root.left = deleteNodeHelper(root.left, item);
118+
} else if (item > root.item) {
119+
root.right = deleteNodeHelper(root.right, item);
120+
} else {
121+
if (root.left === null || root.right === null) {
122+
let temp = null;
123+
if (temp == root.left) {
124+
temp = root.right;
125+
} else {
126+
temp = root.left;
127+
}
128+
129+
if (temp == null) {
130+
temp = root;
131+
root = null;
132+
} else {
133+
root = temp;
134+
}
135+
} else {
136+
let temp = this.nodeWithMimumValue(root.right);
137+
root.item = temp.item;
138+
root.right = deleteNodeHelper(root.right, temp.item);
139+
}
140+
}
141+
if (root == null) {
142+
return root;
143+
}
144+
145+
// Update the balance factor of each node and balance the tree
146+
root.height = Math.max(this.height(root.left), this.height(root.right)) + 1;
147+
148+
let balanceFactor = this.getBalanceFactor(root);
149+
if (balanceFactor > 1) {
150+
if (this.getBalanceFactor(root.left) >= 0) {
151+
return this.rightRotate(root);
152+
} else {
153+
root.left = this.leftRotate(root.left);
154+
return this.rightRotate(root);
155+
}
156+
}
157+
if (balanceFactor < -1) {
158+
if (this.getBalanceFactor(root.right) <= 0) {
159+
return this.leftRotate(root);
160+
} else {
161+
root.right = this.rightRotate(root.right);
162+
return this.leftRotate(root);
163+
}
164+
}
165+
return root;
166+
};
167+
168+
//delete a node
169+
this.deleteNode = (item) => {
170+
root = deleteNodeHelper(root, item);
171+
};
172+
173+
// print the tree in pre - order
174+
this.preOrder = () => {
175+
preOrderHelper(root);
176+
};
177+
178+
const preOrderHelper = (node) => {
179+
if (node) {
180+
console.log(node.item);
181+
preOrderHelper(node.left);
182+
preOrderHelper(node.right);
183+
}
184+
};
185+
};
186+
187+
// Insert:
188+
var tree = new AVLTree();
189+
tree.insertNode(33);
190+
tree.insertNode(13);
191+
tree.insertNode(53);
192+
tree.insertNode(9);
193+
tree.insertNode(21);
194+
tree.insertNode(61);
195+
tree.insertNode(8);
196+
tree.insertNode(11);
197+
tree.preOrder();
198+
tree.deleteNode(13);
199+
console.log("After Deletion: ");
200+
tree.preOrder();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /