From bcfa318172e328f08a83ac68858c7f0dbd92a23d Mon Sep 17 00:00:00 2001 From: quesurifn Date: Sat, 16 Apr 2022 16:39:44 -0500 Subject: [PATCH] WIP --- BinarySearchTree.ts | 99 ++++++++++++++++++++++++++++++++------------- index.ts | 8 ++-- package.json | 4 +- tsconfig.json | 6 +-- 4 files changed, 83 insertions(+), 34 deletions(-) diff --git a/BinarySearchTree.ts b/BinarySearchTree.ts index 6d87a76..b48f1d8 100644 --- a/BinarySearchTree.ts +++ b/BinarySearchTree.ts @@ -24,8 +24,7 @@ class Tree { return null; } - let current = this.root; - + let current = startingNode; while(current) { if(current.left == null) { return current; @@ -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; } } @@ -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; @@ -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; \ No newline at end of file +export default Tree; diff --git a/index.ts b/index.ts index 6b1058f..cc26424 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,5 @@ import BinarySearchTree from './BinarySearchTree.js'; +import esserializer from 'esserializer'; let bst = new BinarySearchTree(); @@ -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()) \ No newline at end of file +console.log(bst.isValidTree()) +console.log(esserializer.serialize(bst)) \ No newline at end of file diff --git a/package.json b/package.json index 1917615..6b670a1 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,7 @@ }, "author": "Kyle Fahey", "license": "ISC", - "type": "module" + "dependencies": { + "esserializer": "^1.3.2" + } } diff --git a/tsconfig.json b/tsconfig.json index 97963c4..7c794e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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,