Skip to content
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

added linked list and BST #56

Open
wants to merge 1 commit into
base: main
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
95 changes: 95 additions & 0 deletions Programming_Languages/Javascript/BinarySearchTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Node {
constructor(value) {
this.left = null;
this.right = null;
this.value = value;
}
}

class BST {
constructor() {
this.root = null;
}

insert(value) {
const newNode = new Node(value);
if (!this.root) {
return (this.root = newNode);
}

let node = this.root;

while (true) {
if (value < node.value) {
if (!node?.left) {
node.left = newNode;
return this;
}
node = node.left;
} else {
if (!node?.right) {
node.right = newNode;
return this;
}
node = node.right;
}
}
}

lookup(value) {
if (!this.root) return null;
if (this.root.value === value) return value;
let currentNode = this.root;
let i = 0;
while (currentNode) {
i++;
console.log(i);
if (value === currentNode.value) return value;
if (value < currentNode.value) {
if (!currentNode.left) return null;
currentNode = currentNode.left;
} else {
if (!currentNode.right) return null;
currentNode = currentNode.right;
}
}
}

remove(value) {
if (!this.root) return false;

let currentNode = this.root;
let parentNode = null;

while (currentNode) {
if (value < currentNode.value) {
currentNode = currentNode.left;
} else if (value > currentNode.value) {
currentNode = currentNode.right;
} else if (value === currentNode.value) {
if (!currentNode.right) {
} else if (!currentNode.right.left) {
} else {
}
}
}
}
}

const tree = new BST();
tree.insert(5);
tree.insert(10);
tree.insert(4);
tree.insert(6);
// console.log(tree);
console.log(tree.lookup(100));

function traverse(node) {
const tree = { value: node.value };
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);

return tree;
}

// console.log(JSON.stringify(traverse(tree.root)));
191 changes: 191 additions & 0 deletions Programming_Languages/Javascript/Linkedlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// 10-->5-->16

// Example linked list
let myLinkedList = {
head: {
value: 10,
next: {
value: 5,
next: {
value: 16,
next: null,
},
},
},
};

// singly linked list node
class Node {
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}

// doubly linked list node
class DNode {
constructor(value, next = null, prev = null) {
this.value = value;
this.next = next;
this.prev = prev;
}
}

class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
};
this.tail = this.head;
this.length = 1;
}

append(value) {
const node = new DNode(value);
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
this.length = this.length + 1;
}

prepend(value) {
const newNode = new DNode(value, this.head);
this.head.prev = newNode;
this.head = newNode;
this.length++;
}

printList() {
const array = [];
let currentNode = this.head;
while (currentNode !== null) {
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}

insert(index, value) {
if (index >= this.length) {
return this.append(value);
}
const newNode = new DNode(value);
const leader = this.traverseToIndex(index - 1);
const nextNode = leader.next;
leader.next = newNode;
newNode.prev = leader;
nextNode.prev = newNode;
newNode.next = nextNode;
this.length++;
}

remove(index) {
const leader = this.traverseToIndex(index - 1);
const leaderNext = leader.next.next;
leader.next = leaderNext;
leaderNext.prev = leader;
this.length--;
}

traverseToIndex(index) {
let counter = 0;
let currentNode = this.head;
while (counter !== index) {
currentNode = currentNode.next;
counter++;
}
return currentNode;
}

reverse() {
if (!this.head.next) {
return this.head;
}
let first = this.head;
this.tail = this.head;
let second = first.next;
while (second) {
const temp = second.next;
second.next = first;
first = second;
second = temp;
}
this.head.next = null;
this.head = first;
}
}

const myLl = new LinkedList(10);
myLl.append(2);
myLl.append(16);
// myLl.prepend(1);
// myLl.insert(2, 100);
// myLl.remove(1);
myLl.reverse();
console.log(myLl);

class DoublyLinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
prev: null,
};
this.tail = this.head;
this.length = 1;
}

append(value) {
const node = new Node(value);
this.tail.next = node;
this.tail = node;
this.length = this.length + 1;
}

prepend(value) {
const newNode = new Node(value, this.head);
console.log(newNode);
this.head = newNode;
this.length++;
}

printList() {
const array = [];
let currentNode = this.head;
while (currentNode !== null) {
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}

insert(index, value) {
if (index >= this.length) {
return this.append(value);
}
const newNode = new Node(value);
const leader = this.traverseToIndex(index - 1);
const nextNode = leader.next;
leader.next = newNode;
newNode.next = nextNode;
this.length++;
}

remove(index) {
const leader = this.traverseToIndex(index - 1);
const leaderNext = leader.next.next;
leader.next = leaderNext;
this.length--;
}

traverseToIndex(index) {
let counter = 0;
let currentNode = this.head;
while (counter !== index) {
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
}