代码实现
class TreeNode {
constructor(val) {
this.value = val;
this.left = null;
this.right = null;
}
}
class Tree {
constructor(...args) {
this.treeRoot = null;
if (args && args.length) this.insertTreeNodeList(args);
}
createTreeNode(val) {
if (typeof val === 'number') {
return new TreeNode(val);
} else if (typeof val === 'string' && val !== '') {
return new TreeNode(Number(val));
}
return null;
}
insertTreeNode(val, node = this.treeRoot) {
if (!this.treeRoot) {
this.treeRoot = this.createTreeNode(val);
} else {
if (val > node.value) {
if (!node.right) node.right = this.createTreeNode(val);
else this.insertTreeNode(val, node.right)
} else {
if (!node.left) node.left = this.createTreeNode(val);
else this.insertTreeNode(val, node.left)
}
}
}
insertTreeNodeList(vals = []) {
vals.forEach(val => {
this.insertTreeNode(val)
})
}
delTreeNode(val, node = this.treeRoot) {
const currTreeNode = this.getTreeNode(val);
const currTreeNodeParent = this.getTreeNodeParent(val);
if (!currTreeNode.left && !currTreeNode.right) {
if (currTreeNode === this.treeRoot) this.treeRoot = null;
else {
if (currTreeNodeParent.left === currTreeNode) currTreeNodeParent.left = null;
else currTreeNodeParent.right = null;
}
}
else if (currTreeNode.left && currTreeNode.right) {
const lastTreeNode = this.getNextor(currTreeNode);
if (currTreeNode === this.treeRoot) {
this.treeRoot = lastTreeNode;
} else if (currTreeNodeParent.left === currTreeNode) {
currTreeNodeParent.left = lastTreeNode;
} else {
currTreeNodeParent.right = lastTreeNode;
}
lastTreeNode.left = currTreeNode.left
}
else {
const delType = currTreeNode.left ? 'left' : 'right';
if (currTreeNode === this.treeRoot) this.treeRoot = currTreeNode[delType];
else {
if (currTreeNodeParent.left === currTreeNode) currTreeNodeParent.left = currTreeNode[delType];
else currTreeNodeParent.right = currTreeNode[delType];
}
}
}
getTreeNode(val, node = this.treeRoot) {
if (!node) return null;
else if (node.value === val) return node;
else if (val > node.value) return this.getTreeNode(val, node.right);
else return this.getTreeNode(val, node.left)
}
getTreeNodeParent(val, node = this.treeRoot) {
if (!node) return null;
if (node.left && node.left.value === val) return node;
if (node.right && node.right.value === val) return node;
return this.getTreeNodeParent(val, node.left) || this.getTreeNodeParent(val, node.right)
}
getNextor(node = this.treeRoot) {
if (!node.right) return null;
const delNode = node;
let parentNode = node;
node = node.right;
while (node.left) {
parentNode = node;
node = node.left;
}
parentNode.left = node.right;
node.right = delNode.right;
return node;
}
getPrevor(node = this.treeRoot) {
if (!node.left) return null;
const delNode = node;
let parentNode = node;
node = node.left;
while (node.right) {
parentNode = node;
node = node.right
}
parentNode.right = node.left;
node.left = delNode.left;
return node;
}
prevEach(node = this.treeRoot) {
if (node) console.log(node.value);
if (node && node.left) this.prevEach(node.left);
if (node && node.right) this.prevEach(node.right)
}
midEach(node = this.treeRoot) {
if (node && node.left) this.midEach(node.left)
if (node) console.log(node.value)
if (node && node.right) this.midEach(node.right)
}
nextEach(node = this.treeRoot) {
if (node && node.left) this.nextEach(node.left)
if (node && node.right) this.nextEach(node.right)
if (node) console.log(node.value);
}
}
使用
const tree = new Tree(7, 5, 9, 3, 8, 10);
tree.insertTreeNode(6)
console.log(tree.treeRoot)