在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。二叉树的第i层至多有 个结点;深度为k的二叉树至多有 个结点;对任何一棵二叉树T,如果其终端结点数为 ,度为2的结点数为 ,则 。
树和二叉树的三个主要差别:
- 树的结点个数至少为1,而二叉树的结点个数可以为0;
- 树中结点的最大度数没有限制,而二叉树结点的最大度数为2;
- 树的结点无左、右之分,而二叉树的结点有左、右之分。
完全二叉树和满二叉树
1. 满二叉树:一棵深度为k,且有 个节点成为满二叉树
2. 完全二叉树:深度为k,有n个节点的二叉树,当且仅当其每一个节点都与深度为k的满二叉树中序号为1至n的节点对应时,称之为完全二叉树
遍历二叉树:L、D、R分别表示遍历左子树、访问根结点和遍历右子树,
先(根)序遍历二叉树的顺序是DLR;
中(根)序遍历二叉树的顺序是LDR;
后(根)序遍历二叉树的顺序是LRD;
还有按层遍历二叉树。这些方法的时间复杂度都是O(n),n为结点个数。
简单例子:
java代码实现二叉树:
定义二叉树的节点,如下:
class Node
{
public int key; // 关键字
public double value; // 值
//指向节点的子节点的引用;
public Node leftChild;
public Node rightChild;
public void displayNode()
{
System.out.print('{');
System.out.print(key);
System.out.print(", ");
System.out.print(value);
System.out.print("} ");
}
}
创建树,如下:
//递归方式实现
public void insert(Node newNode,Node rootNode){
if(root==null){
root=newNode;
}else{
if(newNode.key<rootNode.key){
if(rootNode.leftChild==null){
rootNode.leftChild=newNode;
}else{
insert(newNode,rootNode.leftChild);
}
}else{
if(rootNode.rightChild==null){
rootNode.rightChild=newNode;
}else{
insert(newNode,rootNode.rightChild);
}//end else 3
}//end else 2
}//end else 1
}// end insert
//非递归方式实现
public void insert(int id, double dd)
{
//创建新节点
Node newNode = new Node();
newNode.key = id; //插入数据
newNode.value = dd;
if (root == null)
{ //创建根节点
root = newNode;
}
else
{
Node current = root; // start at root
Node parent;
while (true)
{
parent = current;
//插在左节点,从根节点开始,不断向下层节点移动;
if (id < current.key)
{
//current对象是不断变化的;
current = current.leftChild;
//如果当前对象左节点为空,
//否则表示下面有节点,继续运行while直到满足条件;
if (current == null)
{ //插在左节点,父节点的引用(句柄)指向左节点对象;
parent.leftChild = newNode;
return;
}
}
else
{
current = current.rightChild;
if (current == null) // if end of the line
{ //插在左节点,父节点的引用(句柄)指向右节点对象;
parent.rightChild = newNode;
return;
}
}
} // end while
} // end else not root
} // end insert()
查找树的节点
//递归方式实现
/*
* 递归实现查找树节点
* key:节点key,cTree:查找的树
*/
public Node find(int key,Node cTree){
if(cTree==null){
return null;
}else{
if(key<cTree.key){
return find(key,cTree.leftChild);
}else if(key>cTree.key){
return find(key,cTree.rightChild);
}else{
return cTree;
}
}
}
//非递归方式实现
/*
* 非递归实现查找树节点
* key:节点key
*/
public Node find(int key)
{
Node current = root; // start at root
while (current.key != key)
{
if (key < current.key)
{
current = current.leftChild;
}
else
{
current = current.rightChild;
}
if (current == null)
{
return null; // 没查到,返回空
}
}
return current;
} // end find()
二叉树的三种遍历
//前序遍历
private void preOrder(Node localRoot)
{
if (localRoot != null)
{
System.out.print(localRoot.key + " ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
//中序遍历
private void inOrder(Node localRoot)
{
if (localRoot != null)
{
inOrder(localRoot.leftChild);
System.out.print(localRoot.key + " ");
inOrder(localRoot.rightChild);
}
}
//后序遍历
private void postOrder(Node localRoot)
{
if (localRoot != null)
{
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
System.out.print(localRoot.key + " ");
}
}