二叉树(java实现)

部分参考

代码:

package com.important.data.struct.BinaryTree;

import java.util.Stack;
/*
 * 使用栈实现二叉树的非递归遍历
 */

public class Tree
{
    private static final String left = "leftSon";
    private static final String right = "rightSon";
    public static void main(String[] args)
    {
        TreeType treeType = new TreeType();
        treeType.init("A");
        treeType.add("B", "A", left);
        treeType.add("C", "A", right);
        treeType.add("D", "B", left);
        treeType.add("E", "B", right);
        treeType.add("F", "C", left);
        treeType.add("G", "C", right);
        treeType.add("H", "D", left);
        treeType.add("I", "F", right);
        System.out.println("前序遍历:");
        treeType.DLR(treeType.getRoot());
        System.out.println();
        System.out.println("非递归前序遍历: ");
        treeType.iteratorPre(treeType.getRoot());
        System.out.println();
        System.out.println("中序遍历:");
        treeType.LDR(treeType.getRoot());
        System.out.println();
        System.out.println("非递归中序遍历: ");
        treeType.iteratorMid(treeType.getRoot());
        System.out.println();
        System.out.println("后续遍历: ");
        treeType.LRD(treeType.getRoot());
        System.out.println();
        System.out.println("非递归后续遍历: ");
        treeType.iteratorLat(treeType.getRoot());
        System.out.println();
        System.out.println("按层遍历: ");
        treeType.level(treeType.getRoot());
        System.out.println();
        System.out.println("树的深度:");
        //树的深度
        System.out.println(treeType.dept(treeType.getRoot()));
    }

}

//创建树节点
class Node{
    private Object value;   //结点数据
    private Node left;  //左指针
    private Node right; //右指针
    public Node(Object va)
    {
        this.value = va;
        left = null;
        right = null;
    }

    public Object getValue(){
        return value;
    }

    public Node getLeft(){
        return this.left;
    }

    public void setLeft(Node node){
        this.left = node;
    }

    public Node getRight(){
        return this.right;
    }

    public void setRight(Node node){
        this.right = node;
    }

}

class TreeType{
    private Node root;
    private static int MaxLen=20;
    public Node init(Object val){
            root = new Node(val);
            return root;
    }

    //获取根节点
    public Node getRoot(){
        return root;
    }

    //添加节点
    public void add(Object val,Object parent,String direction){

        Node parentNode=getParent(root, parent);
        Node newNode = new Node(val);
        if(parentNode==null){
            System.out.println("what your insert is wrong");
            return;
        }

        switch (direction)
        {
            case "leftSon":
                if(parentNode.getLeft()!=null){
                    System.out.println("leftson is not null");
                }
                parentNode.setLeft(newNode);
                break;
            case "rightSon":
                if(parentNode.getRight()!=null){
                    System.out.println("rightson is not null");
                }
                parentNode.setRight(newNode);                
                break;
        }
    }

    //查找父节点
    public Node getParent(Node root,Object parent){

        Node p;
        Node temp = root;
        if(root==null){
            return null;
        }else {
            if(temp.getValue().equals(parent)){
                return temp;
            }else if ((p=getParent(temp.getLeft(), parent))!=null) {
                return p;
            }else if ((p=getParent(temp.getRight(), parent))!=null) {
                return p;
            }else {
                return null;
            }
        }
    }

    //显示节点
    public void show(Node parentNode){
        if(parentNode==null){
            System.out.println("does not exits");
        }
        System.out.print(parentNode.getValue()+" ");
    }

    //前序遍历--根左右(递归遍历)
    public void DLR(Node node){
        if(node!=null){
            show(node);
            DLR(node.getLeft());
            DLR(node.getRight());
        }
    }

    //中序遍历--左根右(递归遍历)
    public void LDR(Node node){
        if(node!=null){
            LDR(node.getLeft());
            show(node);
            LDR(node.getRight());
        }
    }

    //后序遍历--左右根(递归遍历)
    public void LRD(Node node){
        if(node!=null){
            LRD(node.getLeft());
            LRD(node.getRight());
            show(node);
        }
    }

    //按层遍历
    public void level(Node node){
        Node pNode;
        Node[] q = new Node[MaxLen];
        int head=0;
        int tail=0;
        if(node!=null){
            tail=(tail+1);
            q[tail]=node;
        }
        while(head!=tail){
            head=(head+1);
            pNode=q[head];
            show(pNode);
            if(pNode.getLeft()!=null){
                tail=(tail+1);
                q[tail]=pNode.getLeft();
            }
            if(pNode.getRight()!=null){
                tail=(tail+1);
                q[tail]=pNode.getRight();
            }
        }
    }

    //非递归前序遍历
    public void iteratorPre(Node node){
        Stack stack = new Stack<>();
        if(node!=null){
            stack.push(node);
            while(!stack.empty()){
                node = stack.pop();
                show(node);
                if(node.getRight()!=null){
                    stack.push(node.getRight());
                    //为什么p.getLeft() 在后,getRight()在前应为while 
                        //循环第一句就是pop visit所以要把left放上,先访问。之中方法是即压即访问法。
                }
                if(node.getLeft()!=null){
                    stack.push(node.getLeft());
                }
            }
        }
    }

    //非递归中序遍历
    public void iteratorMid(Node p){
        Stack stack = new Stack();    
        Node node = p;    
        while (node != null || stack.size() > 0) {    
            while (node != null) {    
                stack.push(node);    
                node = node.getLeft();    
            }    
            if (stack.size() > 0) {    
                node = stack.pop();    
                show(node);     
                node = node.getRight();    
            }    
        }    
    }

    //非递归后续遍历
    public void iteratorLat(Node p){
        Node q = p;    
        Stack stack = new Stack();    
        while (p != null) {    
            // 左子树入栈    
            for (; p.getLeft() != null; p = p.getLeft())    
                stack.push(p);    
            // 当前节点无右子或右子已经输出    
            while (p != null && (p.getRight() == null || p.getRight() == q)) {    
                show(p);    
                q = p;// 记录上一个已输出节点    
                if (stack.empty())    
                    return;    
                p = stack.pop();    
            }    
            // 处理右子    
            stack.push(p);    
            p = p.getRight();    
        }    
    }

    //深度
    public int dept(Node node){
        int deptleft;
        int deptright;
        if(node==null){
            return 0;
        }else {
            deptleft=dept(node.getLeft());
            deptright=dept(node.getRight());
            if(deptleft>=deptright){
              deptleft++;
              return deptleft;
            }else {
              deptright++;
              return deptright;
            }
        }
    }
}

运行结果是:

A B D H E C F I G 
中序遍历:
H D B E A F I C G 
非递归中序遍历: 
H D B E A F I C G 
后续遍历: 
H D E B I F G C A 
非递归后续遍历: 
H D E B I F G C A 
按层遍历: 
A B C D E F G H I 
树的深度:
4

你可能感兴趣的:(数据结构)