【二叉树】二叉树的建立与遍历

1、定义

二叉树是一个连通的无环图,而且每个顶点的度不大于3。有根二叉树还要知足根结点的度不大于2。有了根结点以后,每一个顶点定义了惟一的父结点,和最多2个子结点。函数

2、性质

  •  在非空二叉树中,第i层的结点总数不超过, i>=1;
  •  深度为h的二叉树最多有个结点(h>=1),最少有h个结点;
  •  对于任意一棵二叉树,若是其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;
  •  具备n个结点的彻底二叉树的深度为(注:[ ]表示向下取整)
  • 有N个结点的彻底二叉树各结点若是用顺序方式存储,则结点之间有以下关系:
  1. 若I为结点编号则 若是I>1,则其父结点的编号为I/2;
  2. 若是2*I<=N,则其左孩子(即左子树的根结点)的编号为2*I;若2*I>N,则无左孩子;
  3. 若是2*I+1<=N,则其右孩子的结点编号为2*I+1;若2*I+1>N,则无右孩子。
  • 给定N个结点,能构成h(N)种不一样的二叉树。h(N)为卡特兰数的第N项。h(n)=C(2*n,n)/(n+1);
  • 设有i个枝点,I为全部枝点的道路长度总和,J为叶的道路长度总和J=I+2i。

3、二叉树的建立

@Setter @Getter @ToString public class BinaryTree { private Object data;//当前节点数据
    private BinaryTree lChild;//当前节点左孩子
    private BinaryTree rChild;//当前节点右孩子
    private BinaryTree root;//根节点 //构造函数
    public BinaryTree (Object data){ this.data = data; } //建立二叉树
    public void creatBinaryTree (Object data[]){ //将数据转换为节点存储
        List<BinaryTree> list = new ArrayList<BinaryTree>(); for (Object tmpdata:data){ list.add(new BinaryTree(tmpdata)); } //第1个节点做为根节点
        root = list.get(0); //构建二叉树
        for (int i=0;i<list.size()/2;i++){ //父节点位置为i,左子节点位置为i*2+1
            if (i*2+1<list.size()){ list.get(i).setLChild(list.get(i*2+1)); } //父节点位置为i,左子节点位置为i*2+2
            if (i*2+2<list.size()){ list.get(i).setRChild(list.get(i*2+2)); } } } }

4、二叉树的遍历

一、前序遍历
//前序遍历(递归)
public void preOrder_Recursive(BinaryTree root){ if (root!=null){ System.out.print(" "+root.getData()); preOrder_Recursive(root.getLChild()); preOrder_Recursive(root.getRChild()); } }
二、中序遍历
//中序遍历(递归)
public void inOrder_Recursive(BinaryTree root){ if (root!=null){ inOrder_Recursive(root.getLChild()); System.out.print(" "+root.getData()); inOrder_Recursive(root.getRChild()); } }
三、后序遍历
//后序遍历(递归)
public void postOrder_Recursive(BinaryTree root){ if (root!=null){ postOrder_Recursive(root.getLChild()); postOrder_Recursive(root.getRChild()); System.out.print(" "+root.getData()); } }
四、层次遍历
//层次遍历
public void levelOrder(BinaryTree root){ //当前节点
    BinaryTree current = null; //建立队列用于临时存放节点
    LinkedList<BinaryTree> queue = new LinkedList<BinaryTree>(); if (root!=null){ //根节点入队
 queue.offer(root); } while(!queue.isEmpty()){ //出队队头节点
        current = queue.poll(); System.out.print(" "+current.getData()); //如有左子节点,则将其入队
        if (current.getLChild()!=null){ queue.offer(current.getLChild()); } //如有右子节点,则将其入队
        if (current.getRChild()!=null){ queue.offer(current.getRChild()); } } }
5、二叉树的视图
一、左视图
//左视图
public void leftView(BinaryTree root){ //当前节点
    BinaryTree current = null; //建立队列用于临时存放节点
    LinkedList<BinaryTree> queue = new LinkedList<BinaryTree>(); if (root!=null){ //根节点入队
 queue.offer(root); System.out.print(" "+root.getData()); } while(!queue.isEmpty()){ int size = queue.size(); while(size>0) { //出队队头节点
            current = queue.poll(); //如有左子节点,则将其入队
            if (current.getLChild() != null) { queue.offer(current.getLChild()); } //如有右子节点,则将其入队
            if (current.getRChild() != null) { queue.offer(current.getRChild()); } size--; //size==0时,队列中第1个节点为下一层右边第1个节点
            if (size==0 && queue.size()>0){ System.out.print(" "+queue.getFirst().getData()); } } } }
二、右视图
//右视图
public void rightView(BinaryTree root){ //当前节点
    BinaryTree current = null; //建立队列用于临时存放节点
    LinkedList<BinaryTree> queue = new LinkedList<BinaryTree>(); if (root!=null){ //根节点入队
 queue.offer(root); System.out.print(" "+root.getData()); } while(!queue.isEmpty()){ int size = queue.size(); while(size>0) { //出队队头节点
            current = queue.poll(); //如有右子节点,则将其入队
            if (current.getRChild() != null) { queue.offer(current.getRChild()); } //如有左子节点,则将其入队
            if (current.getLChild() != null) { queue.offer(current.getLChild()); } size--; //size==0时,队列中第1个节点为下一层左边第1个节点
            if (size==0 && queue.size()>0){ System.out.print(" "+queue.getFirst().getData()); } } } }
相关文章
相关标签/搜索