二叉搜索树
来源:互联网 发布:turn.js 单屏翻页 编辑:程序博客网 时间:2024/06/16 12:07
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
public class Node {
public int iData;
public double dData;
public Node leftChild;
public Node rightChild;
public void display(){
System.out.println("{");
System.out.println("iData");
System.out.println(iData);
System.out.println("dData");
System.out.println(dData);
}
}
---------------------------------------------------------------
public class Tree {
private Node root;
public Tree(){
root = null;
}
public Node find(int key){ //查找
Node current = root;
while (current.iData != key) {
if (current.iData < key) {
current = current.rightChild;
} else {
current = current.leftChild;
}
}
return current;
}
public void insert(int id, double dd){ //插入元素
Node node = new Node();
node.iData = id;
node.dData = dd;
if (root == null) {
root = node;
return;
}
Node parrent = root;
Node current = root;
boolean isLeft = true;
while (true) {
parrent = current;
if (parrent.iData > id) {
current = parrent.leftChild;
isLeft = true;
} else {
current = parrent.rightChild;
isLeft = false;
}
if (current == null) {
if (isLeft) {
parrent.leftChild = node;
} else {
parrent.rightChild = node;
}
return;
}
}
}
public void delete(int key){ //删除元素
Node parrent =root;
Node current = root;
boolean isLeft = true;
while (current.iData != key) {
parrent = current;
if (current.iData < key) {
isLeft = false;
current = current.rightChild;
} else {
isLeft = true;
current = current.leftChild;
}
}
if (current == null) {
return;
}
if (current.leftChild == null && current.rightChild == null) {
if (isLeft) {
parrent.leftChild = null;
}
if (!isLeft) {
parrent.rightChild = null;
}
} else if (current.rightChild == null) {
if (isLeft) {
parrent.leftChild = current.leftChild;
} else {
parrent.rightChild = current.rightChild;
}
} else if (current.leftChild == null) {
if (isLeft) {
parrent.leftChild = current.rightChild;
} else {
parrent.rightChild = current.leftChild;
}
} else {
Node successor = getSuccessor(current);
successor.leftChild = current.leftChild;
if (successor != current.rightChild) {
successor.rightChild = current.rightChild;
}
if (isLeft) {
parrent.leftChild = successor;
}else {
parrent.rightChild = successor;
}
}
}
private Node getSuccessor(Node node) {//获取待删除元素node的后继节点。
Node parrent = node;
Node current = node.rightChild;
while (current.leftChild != null) {
parrent = current;
current = current.leftChild;
}
if (current.rightChild != null) {
parrent.leftChild = current.rightChild;
}
return null;
}
}
- 【二叉搜索数】HDU3791二叉搜索树
- 二叉树--二叉搜索树
- 【二叉树】二叉搜索树
- 二叉树- 二叉搜索树
- 【搜索树】二叉搜索树
- 二叉搜索树BSTree
- 二叉搜索树
- 二叉搜索树
- 二叉搜索树
- 最优二叉搜索树
- 二叉搜索树
- 二叉搜索树
- HDOJ3791 二叉搜索树
- 二叉查找树搜索
- 二叉搜索树
- 二叉搜索树
- BST 二叉搜索树
- 二叉搜索树
- 复现数据集画的GT框matlab代码
- QTcpSocket readAll 后程序崩溃
- 最小二乘法推导和证明(matlab代码实现)
- POJ 3691 DNA repair 笔记
- 1067. Sort with Swap(0,*) (25)
- 二叉搜索树
- c++继承与派生
- Mybatis及其使用
- Active Directory组网实验
- 关于Python from VideoCapture import Device 报错的解决方法
- Pade 逼近
- Error: A JNI error has occurred, please check your installation and try again
- 统计字符串中的单词数目
- window下phpstudy的nginx配置虚拟主机和伪静态