二叉树(下)
Posted 编程加油站
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树(下)相关的知识,希望对你有一定的参考价值。
一、二叉查找树
1、二叉查找树的查找操作
public class BinarySearchTree {
private Node tree;
public Node find(int data) {
Node p = tree;
while (p != null) {
if (data < p.data) p = p.left;
else if (data > p.data) p = p.right;
else return p;
}
return null;
}
public static class Node {
private int data;
private Node left;
private Node right;
public Node(int data) {
this.data = data;
}
}
}
2、二叉树的插入操作
public void insert(int data) {
if (tree == null) {
tree = new Node(data);
return;
}
Node p = tree;
while (p != null) {
if (data > p.data) {
if (p.right == null) {
p.right = new Node(data);
return;
}
p = p.right;
} else { // data < p.data
if (p.left == null) {
p.left = new Node(data);
return;
}
p = p.left;
}
}
}
3、二叉查找树的删除操作
public void delete(int data) {
Node p = tree; // p指向要删除的节点,初始化指向根节点
Node pp = null; // pp记录的是p的父节点
while (p != null && p.data != data) {
pp = p;
if (data > p.data) p = p.right;
else p = p.left;
}
if (p == null) return; // 没有找到
// 要删除的节点有两个子节点
if (p.left != null && p.right != null) { // 查找右子树中最小节点
Node minP = p.right;
Node minPP = p; // minPP表示minP的父节点
while (minP.left != null) {
minPP = minP;
minP = minP.left;
}
p.data = minP.data; // 将minP的数据替换到p中
p = minP; // 下面就变成了删除minP了
pp = minPP;
}
// 删除节点是叶子节点或者仅有一个子节点
Node child; // p的子节点
if (p.left != null) child = p.left;
else if (p.right != null) child = p.right;
else child = null;
if (pp == null) tree = child; // 删除的是根节点
else if (pp.left == p) pp.left = child;
else pp.right = child;
}
4、二叉树的其他操作
class Node {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
/**
* 搜索二叉树
* 允许重复值添加
*/
class SearchTree {
constructor() {
this.root = null
}
insert(num) {
let node = new Node(num)
if (this.root === null) {
this.root = node
return
}
let prent = this.getPrev(num)
if (num < prent.value) {
prent.left = node
} else {
prent.right = node
}
}
remove(num) {
let point = this.root
let prent = null
let tree = this
let res = null
while (true) {
if (point.left) {
if (num < point.left.value || num < point.value) {
prent = point
point = point.left
continue
}
}
if (point.right) {
if (num >= point.right.value || num >= point.value) {
if (num === point.value) {
delMethod(point, prent)
if (prent === null) {
point = this.root
} else {
prent = prent
point = prent.right
}
res = true
continue
}
prent = point
point = point.right
continue
}
}
if (point.value === num) {
res = true
delMethod(point, prent)
}
break
}
return res
function delMethod(delNode, parent) {
let p = delNode // p指向要删除的节点
let pp = parent // pp记录的是p的父节点
// 要删除的节点有两个子节点
if (p.left != null && p.right != null) { // 查找右子树中最小节点
let minP = p.right
let minPP = p // minPP表示minP的父节点
while (minP.left != null) {
minPP = minP
minP = minP.left
}
p.value = minP.value // 将minP的数据替换到p中
p = minP // 下面就变成了删除minP了
pp = minPP
}
// 删除节点是叶子节点或者仅有一个子节点
let child // p的子节点
if (p.left != null) {
child = p.left
} else if (p.right != null) {
child = p.right
} else {
child = null
}
if (pp == null) {
tree.root = child
} else if (pp.left == p) {
pp.left = child
} else {
pp.right = child
}
}
}
//中序遍历
print() {
let point = this.root
if (point) {
printAll(point.left)
console.log(point.value)
printAll(point.right)
}
function printAll(point) {
if (point == null) {
return
}
printAll(point.left)
console.log(point.value)
printAll(point.right)
}
}
find(num) {
if (this.root === null) {
return
}
return this.getPrev(num, true)
}
//添加和查找的公用部分
getPrev(num, find = false) {
let point = this.root
let res = []
while (true) {
if (point.left) {
if (num < point.left.value || num < point.value) {
point = point.left
continue
}
}
if (point.right) {
if (num >= point.right.value || num >= point.value) {
//搜索时如果有多个值则缓存
if (find && num === point.value) {
res.push(point.value)
}
point = point.right
continue
}
}
//如果是搜索
if (find) {
if (point.value === num) {
res.push(point.value)
}
if (res.length === 0) {
return null
}
if (res.length === 1) {
return res[0]
}
return res
}
//如果是添加 返回的是应该添加的那各节点的父节点
return point
}
}
}
//测试
function doTest() {
let searchTree = new SearchTree()
console.log('新增 4 1 2 5 四个元素')
searchTree.insert(4)
searchTree.insert(1)
searchTree.insert(2)
searchTree.insert(5)
console.log('新增 4 1 2 5 四个元素后,中序遍历结果如下:')
searchTree.print() //1 2 4 5
console.log('删除元素1')
searchTree.remove(1)
console.log('删除元素1后结果如下:')
searchTree.print() // 2 4 5
console.log('删除元素4')
searchTree.remove(4)
console.log('删除元素4后结果如下:')
searchTree.print() // 2 5
console.log('插入元素 3 7 1 5 5 5')
searchTree.insert(3)
searchTree.insert(7)
searchTree.insert(1)
searchTree.insert(5)
searchTree.insert(5)
searchTree.insert(5)
console.log('插入元素 3 7 1 5 5 5后结果如下:')
searchTree.print()// 1 2 3 5 5 5 5 7
console.log('打印查找5')
console.log(searchTree.find(5))// [5,5,5,5]
console.log('移除元素5')
searchTree.remove(5)
console.log('移除元素5结果如下:')
searchTree.print()// 1 2 3 7
console.log('打印查找2')
console.log(searchTree.find(2))
console.log('打印查找30')
console.log(searchTree.find(30))
}
doTest()
二、二叉查找树的时间复杂度分析
n >= 1+2+4+8+...+2^(k-2)+1
n <= 1+2+4+8+...+2^(k-2)+2^(k-1)
以上是关于二叉树(下)的主要内容,如果未能解决你的问题,请参考以下文章
NC41 最长无重复子数组/NC133链表的奇偶重排/NC116把数字翻译成字符串/NC135 股票交易的最大收益/NC126换钱的最少货币数/NC45实现二叉树先序,中序和后序遍历(递归)(代码片段