二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加。但是他也有自己的缺点:删除操作复杂。
先介绍一些关于二叉树的概念名词:
二叉树:是每个结点最多有两个子树的有序树,在使用二叉树的时候,数据并不是随便插入到节点中的,一个节点的左子节点的关键值必须小于此节点,右子节点的关键值必须大于或者是等于此节点,所以又称二叉查找树、二叉排序树、二叉搜索树。
完全二叉树:若设二叉树的高度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第h层有叶子结点,并且叶子结点都是从左到右依次排布,这就是完全二叉树。
满二叉树——除了叶结点外每一个结点都有左右子叶且叶子结点都处在最底层的二叉树。
深度——二叉树的层数,就是深度。
下图中展现了五种不同基本形态的二叉树:
其中 (a) 为空树, (b) 为仅有一个结点的二叉树, (c) 是仅有左子树而右子树为空的二叉树, (d) 是仅有右子树而左子树为空的二叉树, (e) 是左、右子树均非空的二叉树。这里应特别注意的是,二叉树的左子树和右子树是严格区分并且不能随意颠倒的,图 (c) 与图 (d) 就是两棵不同的二叉树。
package ch12;
/*
* 二叉树节点
*/
public class Node {
//数据项
public long data;
//数据项
public String sData;
//左子节点
public Node leftChild;
//右子节点
public Node rightChild;
/**
* 构造方法
* @param data
*/
public Node(long data,String sData) {
this.data = data;
this.sData = sData;
}
}
package ch13;
/*
* 二叉树类
*/
public class Tree {
//根节点
public Node root;
/**
* 插入节点
* @param value
*/
public void insert(long value,String sValue) {
//封装节点
Node newNode = new Node(value,sValue);
//引用当前节点
Node current = root;
//引用父节点
Node parent;
//如果root为null,也就是第一插入的时候
if(root == null) {
root = newNode;
return;
} else {
while(true) {
//父节点指向当前节点
parent = current;
//如果当前指向的节点数据比插入的要大,则向左走
if(current.data > value) {
current = current.leftChild;
if(current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if(current == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
/**
* 查找节点
* @param value
*/
public Node find(long value) {
//引用当前节点,从根节点开始
Node current = root;
//循环,只要查找值不等于当前节点的数据项
while(current.data != value) {
//进行比较,比较查找值和当前节点的大小
if(current.data > value) {
current = current.leftChild;
} else {
current = current.rightChild;
}
//如果查找不到
if(current == null) {
return null;
}
}
return current;
}
/**
* 删除节点
* @param value
*/
public boolean delete(long value) {
//引用当前节点,从根节点开始
Node current = root;
//应用当前节点的父节点
Node parent = root;
//是否为左节点
boolean isLeftChild = true;
while(current.data != value) {
parent = current;
//进行比较,比较查找值和当前节点的大小
if(current.data > value) {
current = current.leftChild;
isLeftChild = true;
} else {
current = current.rightChild;
isLeftChild = false;
}
//如果查找不到
if(current == null) {
return false;
}
}
//删除叶子节点,也就是该节点没有子节点
if(current.leftChild == null && current.rightChild == null) {
if(current == root) {
root = null;
} else if(isLeftChild) {
parent.leftChild = null;
} else {
parent.rightChild = null;
}
} else if(current.rightChild == null) {
if(current == root) {
root = current.leftChild;
}else if(isLeftChild) {
parent.leftChild = current.leftChild;
} else {
parent.rightChild = current.leftChild;
}
} else if(current.leftChild == null) {
if(current == root) {
root = current.rightChild;
} else if(isLeftChild) {
parent.leftChild = current.rightChild;
} else {
parent.rightChild = current.rightChild;
}
} else {
Node successor = getSuccessor(current);
if(current == root) {
root = successor;
} else if(isLeftChild) {
parent.leftChild = successor;
} else{
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}
return true;
}
public Node getSuccessor(Node delNode) {
Node successor = delNode;
Node successorParent = delNode;
Node current = delNode.rightChild;
while(current != null) {
successorParent = successor;
successor = current;
current = current.leftChild;
}
if(successor != delNode.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}
/**
* 前序遍历
*/
public void frontOrder(Node localNode) {
if(localNode != null) {
//访问根节点
System.out.println(localNode.data + ", " + localNode.sData);
//前序遍历左子树
frontOrder(localNode.leftChild);
//前序遍历右子树
frontOrder(localNode.rightChild);
}
}
/**
* 中序遍历
*/
public void inOrder(Node localNode) {
if(localNode != null) {
//中序遍历左子树
inOrder(localNode.leftChild);
//访问根节点
System.out.println(localNode.data + ", " + localNode.sData);
//中旬遍历右子树
inOrder(localNode.rightChild);
}
}
/**
* 后序遍历
*/
public void afterOrder(Node localNode) {
if(localNode != null) {
//后序遍历左子树
afterOrder(localNode.leftChild);
//后序遍历右子树
afterOrder(localNode.rightChild);
//访问根节点
System.out.println(localNode.data + ", " + localNode.sData);
}
}
}
package ch13;
public class TestTree {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(10,"James");
tree.insert(20,"YAO");
tree.insert(15,"Kobi");
tree.insert(3,"Mac");
tree.insert(4, "Zhangsan");
tree.insert(90, "Lisi");
// System.out.println(tree.root.data);
// System.out.println(tree.root.rightChild.data);
// System.out.println(tree.root.rightChild.leftChild.data);
// System.out.println(tree.root.leftChild.data);
//
// Node node = tree.find(3);
// System.out.println(node.data + ", " + node.sData);
// tree.frontOrder(tree.root);
// tree.inOrder(tree.root);
// tree.afterOrder(tree.root);
tree.delete(90);
tree.inOrder(tree.root);
}
}