- 定义:二叉树在图论中是这样定义的:二叉树是一个连通的无环图,并且每一个顶点的度不大于3。有根二叉树还要满足根结点的度不大于2。有了根结点之后,每个顶点定义了唯一的父结点,和最多2个子结点。然而,没有足够的信息来区分左结点和右结点。如果不考虑连通性,允许图中有多个连通分量,这样的结构叫做森林。
- 二叉树性质
(1) 在非空二叉树中,第i层的结点总数不超过 , i>=1;
(2) 深度为h的二叉树最多有 个结点(h>=1),最少有h个结点;
(3) 对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;
(4) 具有n个结点的完全二叉树的深度为
(5)有N个结点的完全二叉树各结点如果用顺序方式存储,则结点之间有如下关系:
若I为结点编号则 如果I>1,则其父结点的编号为I/2;
如果2*I<=N,则其左儿子(即左子树的根结点)的编号为2*I;若2*I>N,则无左儿子;
如果2*I+1<=N,则其右儿子的结点编号为2*I+1;若2*I+1>N,则无右儿子。
package BinaryTreeTest;
class Node{
int value;
Node leftchild;
Node rightchild;
}
class Tree{
Node root;
public Node find(int value){
Node current=root;
while(current.value!=value){
if(value<current.value){
current=current.leftchild;
}
else
current=current.rightchild ;
if(current==null)
return null;
}
return current;
}
public void insert(int value){
Node newNode=new Node();
newNode.value =value;
if(root==null)
root=newNode;
else{
Node current=root;
Node parent;
while(true){
parent=current;
if(value<current.value){
current=current.leftchild;
if(current==null){
parent.leftchild =newNode;
return;
}
}
else{
current=current.rightchild ;
if(current==null){
parent.rightchild =newNode;
return;
}
}
}
}
}
public boolean delete(int value){
Node current=root;
Node parent=root;
boolean isleftchild=true;
while(current.value!=value){
parent=current;
if(value<current.value){
isleftchild=true;
current=current.leftchild;
}
else{
isleftchild=false;
current=current.rightchild;
}
if(current==null)
return false;
}
if(current.leftchild==null&¤t.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.leftchild;
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;
}
private Node getSuccessor(Node delnode){
Node Successorparent=delnode;
Node Successor=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 inOrder(Node root){
if(root!=null){
inOrder(root.leftchild);
System.out.println(root.value );
inOrder(root.rightchild);
}
}
public void preOrder(Node root){
if(root!=null){
System.out.println(root.value );
inOrder(root.leftchild);
inOrder(root.rightchild);
}
}
public void posOrder(Node root){
if(root!=null){
inOrder(root.leftchild);
inOrder(root.rightchild);
System.out.println(root.value );
}
}
public Node maxNode(){
Node current,Last = null;
current=root;
while(current!=null){
Last=current;
current=current.rightchild ;
}
return Last;
}
public Node minNode(){
Node current,Last = null;
current=root;
while(current!=null){
Last=current;
current=current.leftchild ;
}
return Last;
}
}
public class BinaryTree {
public static void main(String[] args) {
Tree tree=new Tree();
tree.insert(10);
tree.insert(22);
tree.insert(13);
tree.insert(44);
tree.insert(27);
tree.insert(5);
tree.insert(48);
tree.inOrder(tree.root);
System.out.println("---------------------------");
tree.delete(13);
tree.inOrder(tree.root);
}
}