数据结构4——Java 二叉树 遍历

二叉树这个东西哇看着真是无语…尤其是自学的,..,,.简直想哭啊有木有!!!Java的资料太少,书上都是C或者C++,自己转java,心好累~~

二叉树常用构建方式
参考资料大致有两种方式,

  1. 顺序输入
  2. 前序遍历方式输入

下面上代码:

  • 结点类
public class BiNode<T> {

    private T data;
    private BiNode<T>lChild;
    private BiNode<T> rChild;

    BiNode( T Data){
        this.data=Data;
    }

    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public BiNode<T> getlChild() {
        return lChild;
    }
    public void setlChild(BiNode<T> lChild) {
        this.lChild = lChild;
    }
    public BiNode<T> getrChild() {
        return rChild;
    }
    public void setrChild(BiNode<T> rChild) {
        this.rChild = rChild;
    }   
}
  • 顺序遍历代码(数组存储,线性)
    大致思路是顺序输入一串元素,根据儿叉树的关系进行转换(完全二叉树使用,非完全浪费空间太多)
    private List<BiNode<T>> nodeList =null;

    public  List<BiNode<T>> CreateBiTree(T[] t){

        nodeList = new LinkedList<BiNode<T>>();

        //把数组t中的元素转换成二叉树的结点
        for(int i=0;i<t.length;i++){
            BiNode<T> bNode = new BiNode<T>(t[i]);
            nodeList.add(bNode);
        }

        //对于数组中的父节点,最后一个父节点(t.length/2-1)可能没有右节点
        //故要分情况讨论(1.最后一个父节点前情况,2.最后一个父节点的情况)
        for(int i=0;i<t.length/2-1;i++){
            //左孩子为2i+1,右孩子为2i+2....顺序读
            nodeList.get(i).setlChild(nodeList.get(2*i+1));
            nodeList.get(i).setrChild(nodeList.get(2*i+2));
        }

        int lastParentIndex = t.length/2-1;
        nodeList.get(lastParentIndex).setlChild(nodeList.get(lastParentIndex*2+1));

        //如果数组的长度为奇数才建立右孩子
        if(t.length%2==1){
            nodeList.get(lastParentIndex).setrChild(nodeList.get(lastParentIndex*2+2));
        }   

        return nodeList;
    }
  • 前序遍历方式输入
public String[] str;

    int count=0;

    private  BiNode<Integer> root; 

    public BiNode<Integer> getRoot() {
        return root;
    }

    public void setRoot(BiNode<Integer> root) {
        this.root = root;
    }

    public BiNode<T> CreateBiTree(){
        BiNode<T> bNode =null;
        if(count >= str.length || str[count++].equals("#")){
            bNode = null;
        }
        else{
            bNode = new BiNode<T>((T)str[count-1]);
            bNode.setlChild(CreateBiTree());
            bNode.setrChild(CreateBiTree());

        }
        return bNode;
    }
  • 遍历
    /*前序*/
    public static void preOederTraverse(BiNode<Integer> root) {
          if (root != null) {
           System.out.print(root.getData() + " ");
           preOederTraverse(root.getlChild());
           preOederTraverse(root.getrChild());
          }
         }
    /*中序*/
     public static void inOrderTraverse(BiNode<Integer>  root) {

          if (root != null) {
            inOrderTraverse(root.getlChild());
           System.out.print(root.getData() + " ");
           inOrderTraverse(root.getrChild());
          }
         }
     /*后序*/
     public static void postOrderTraverse(BiNode<Integer> root) {
          if (root != null) {
            postOrderTraverse(root.getlChild());
            postOrderTraverse(root.getrChild());
           System.out.print(root.getData() + " ");
          }
     }
  • 测试
    测试用前序输入进行测试
    public static  void main(String[] args) {

          Scanner cin = new Scanner(System.in);

          while (cin.hasNext()) {
             String s = cin.nextLine();
             String[] str = s.split(",");

             BiTreeClass<Integer> tree = new BiTreeClass<Integer>();
             tree.str=str;
             BiNode<Integer> root = tree.CreateBiTree();
             preOederTraverse(root);
             System.out.println();
             inOrderTraverse(root);
             System.out.println();
             postOrderTraverse(root);
          }
    }
  • 结果

输入:A,B,D,G,#,#,H,#,#,#,C,E,#,T,#,#,F,#,#

输出:
A B D G H C E T F (前序)
G D H B A E T C F (中序)
G H D B T E F C A (后序)

图结构

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值