Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
quesurifn committed Apr 16, 2022
1 parent 3b3a6d9 commit bcfa318
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 34 deletions.
99 changes: 72 additions & 27 deletions BinarySearchTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class Tree {
return null;
}

let current = this.root;

let current = startingNode;
while(current) {
if(current.left == null) {
return current;
Expand Down Expand Up @@ -114,39 +113,81 @@ class Tree {
return null
}

let current = this.root;
let current: TreeNode = this.root;
let parent: TreeNode = null;

while(current) {
// is leaf
if(key === current.key) {
if (!current.left && !current.right) {
console.log("is leaf")
current = null;
if (parent.left === current) {
parent.left = null;
}
if (parent.right === current) {
parent.right = null;
}
this._count--;
return this;
}

if(current.left === null) {
current = current.right;
if (parent.left === current) {
parent.left = current.right;
}
if (parent.right === current) {
parent.right = current.right;
}

this._count--;
return this;
}

if(current.right === null) {
current = current.left;
if (parent.left === current) {
parent.left = current.left;
}
if (parent.right === current) {
parent.right = current.left;
}

this._count--;
return this;
}

if(current.right && current.left) {
console.log("right and left")
current = this.min(current.right)
continue;
let leftMost = this.min(current.right);
this.remove(leftMost.key);

if(!parent) {
this.root = leftMost;
this.root.left = current.left;
this.root.right = current.right;
return this;
}

if(parent.left === current) {
parent.left = leftMost;
return this;
}
if(parent.right === current) {
parent.right = leftMost;
return this;
}

this._count--;
return this;

}

}

if(key < current.key) {
parent = current;
current = current.left;
}

if(key > current.key) {
parent = current;
current = current.right;
}
}
Expand Down Expand Up @@ -179,15 +220,17 @@ class Tree {
if(currentLeft.left === null) {
break;
}
if(currentLeft.left !== null) {
// If the current node is less than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right
if(currentLeft.key < currentLeft.left.key) {
isValidLeft = false;
}
currentLeft = currentLeft.left;

// If the current node is less than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right
console.log("L_PREV", currentLeft.key)
console.log("L_NEXT", currentLeft.left.key)
if(currentLeft.key < currentLeft.left.key) {
isValidLeft = false;
}
currentLeft = currentLeft.left;

}
// Traverse Right
let isValidRight = true;
Expand All @@ -198,19 +241,21 @@ class Tree {
break;
}

if(currentRight.right !== null) {
// If the current node is greater than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right
if(currentRight.key > currentRight.right.key) {
isValidRight = false;
}
currentRight = currentRight.right;
// If the current node is greater than the child node
// Then we know that the tree is not valid because the child node
// should be greater than the current node because we're going right

console.log("R_PREV: ", currentRight.key)
console.log("R_NEXT: ", currentRight.right.key)
if(currentRight.key > currentRight.right.key) {
isValidRight = false;
}
currentRight = currentRight.right;
}

return isValidLeft && isValidRight;
}

}

export default Tree;
export default Tree;
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BinarySearchTree from './BinarySearchTree.js';
import esserializer from 'esserializer';

let bst = new BinarySearchTree();

Expand All @@ -22,8 +23,9 @@ bst.insert(14, "fourteen");
bst.insert(16, "sixteen");
bst.insert(17, "seventeen");


bst.remove(170);
bst.remove(9)
bst.remove(17)

console.log(bst.dfsInOrder())
console.log(bst.isValidTree())
console.log(bst.isValidTree())
console.log(esserializer.serialize(bst))
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
},
"author": "Kyle Fahey",
"license": "ISC",
"type": "module"
"dependencies": {
"esserializer": "^1.3.2"
}
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"module": "ES2022",
"noImplicitAny": true,
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es6",
"target": "es5",
"outDir": "./build",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down

0 comments on commit bcfa318

Please sign in to comment.