class Node {
constructor(data) {
this.left = this.right = null
this.data = data
}
showData() {
console.log(this.data)
return this.data
}
}
class BianrySortTree {
constructor(root = null) {
this.root = root
}
insert(data) {
const node = new Node(data)
if (!this.root) {
this.root = node
} else {
let parent,
current = this.root
while (true) {
parent = current
if (data < parent.data) {
current = current.left
if (!current) {
parent.left = node
break
}
} else {
current = current.right
if (!current) {
parent.right = node
break
}
}
}
}
}
preOrder(root = this.root) {
if (root) {
root.showData()
this.preOrder(root.left)
this.preOrder(root.right)
}
}
inOrder(root = this.root) {
if (root) {
this.inOrder(root.left)
root.showData()
this.inOrder(root.right)
}
}
postOrder(root = this.root) {
if (root) {
this.postOrder(root.left)
this.postOrder(root.right)
root.showData()
}
}
preOrderInteration(root = this.root) {
const stack = []
if (root) stack.push(root)
while (stack.length) {
root = stack.pop()
root.showData()
if (root.right) {
stack.push(root.right)
}
if (root.left) {
stack.push(root.left)
}
}
}
inOrderInteration(root = this.root) {
const stack = []
while (true) {
if (root) {
stack.push(root)
root = root.left
} else if (stack.length) {
root = stack.pop()
root.showData()
root = root.right
} else {
break
}
}
}
postOrderInteration(root = this.root) {
const stack = [],
helpArr = []
if (root) stack.push(root)
while (stack.length) {
root = stack.pop()
helpArr.push(root.data)
if (root.left) stack.push(root.left)
if (root.right) stack.push(root.right)
}
helpArr.reverse().forEach(e => {
console.log(e)
})
}
postOrderInteration02(root = this.root) {
const stack = [],
unorderedMap = new Map()
while (root || stack.length) {
while (root) {
stack.push(root)
root = root.left
}
while (stack.length && unorderedMap.get(stack[stack.length - 1])) {
stack[stack.length - 1].showData()
stack.pop()
}
if (stack.length) {
root = stack[stack.length - 1].right
unorderedMap.set(stack[stack.length - 1], 1)
}
}
unorderedMap.clear()
}
bfs(root = this.root) {
const queue = []
if (root) queue.push(root)
while (queue.length) {
root = queue.shift()
root.showData()
if (root.left) queue.push(root.left)
if (root.right) queue.push(root.right)
}
}
insertPreOrderInteration(key, root = this.root) {
if (!root) return false
const stack = []
stack.push(root)
while (stack.length) {
root = stack.pop()
if (root.data === key) {
return false
} else if (root.data < key) {
if (root.right) stack.push(root.right)
} else {
if (root.left) stack.push(root.left)
}
}
const node = new Node(key)
if (key < root.data) {
root.left = node
} else {
root.right = node
}
return true
}
deletePreOrderInteration(key, root = this.root) {
if (!root) return false
const stack = []
stack.push(root)
while (stack.length) {
root = stack.pop()
if (root.data === key) {
if (!root.left) {
root = root.right
return true
}
if (!root.right) {
root = root.left
return true
}
let q = root,
s = root.left
while (s.right) {
q = s
s = s.right
}
root.data = s.data
if (q !== root) {
q.right = s.left
} else {
q.left = s.left
}
return true
} else if (root.data < key) {
if (root.right) stack.push(root.right)
} else {
if (root.left) stack.push(root.left)
}
}
return false
}
}
;(() => {
const tree = new BianrySortTree()
const nodes = [31, 36, 20, 68, 54, 33, 11, 24]
nodes.forEach(e => tree.insert(e))
console.log('----先序遍历--递归----')
tree.preOrder()
console.log('----中序遍历--递归----')
tree.inOrder()
console.log('----后序遍历--递归----')
tree.postOrder()
console.log('----先序遍历--迭代----')
tree.preOrderInteration()
console.log('----中序遍历--迭代----')
tree.inOrderInteration()
console.log('----后序遍历--迭代1----')
tree.postOrderInteration()
console.log('----后序遍历--迭代2----')
tree.postOrderInteration02()
console.log('----广度优先遍历--迭代----')
tree.bfs()
console.log('----前序查找--迭代--插入----')
console.log(tree.insertPreOrderInteration(17))
console.log('----中序遍历--迭代----')
tree.inOrderInteration()
console.log('----前序查找--迭代--删除----')
console.log(tree.deletePreOrderInteration(31))
console.log('----中序遍历--迭代----')
tree.inOrderInteration()
})()