java自定义构造二叉树及其遍历

首先:二叉树的建立

首先,我们采用广义表建立二叉树(关于广义表的概念,请查看百科的介绍:http://baike.baidu.com/view/203611.htm

我们建立一个字符串类型的广义表作为输入:

String expression = "A(B(D(,G)),C(E,F))";与该广义表对应的二叉树为

1366533767_1515.jpg

写代码前,我们通过观察二叉树和广义表,先得出一些结论:

每当遇到字母,将要创建节点
每当遇到“(”,表面要创建左孩子节点
每当遇到“,”,表明要创建又孩子节点
每当遇到“)”,表明要返回上一层节点   
广义表中“(”的数量正好是二叉树的层数

节点类的构建###

public class Node {  
  
    private char data;  
    private Node lchild;  
    private Node rchild;  
  
    public Node(){  
          
    }  
    public char getData() {  
        return data;  
    }  
  
    public void setData(char data) {  
        this.data = data;  
    }  
  
    public Node getRchild() {  
        return rchild;  
    }  
  
    public void setRchild(Node rchild) {  
        this.rchild = rchild;  
    }  
  
    public Node getLchild() {  
        return lchild;  
    }  
  
    public void setLchild(Node lchild) {  
        this.lchild = lchild;  
    }  
  
    public Node(char ch, Node rchild, Node lchild) {  
        this.data = ch;  
        this.rchild = rchild;  
        this.lchild = lchild;  
    }  
  
    public String toString() {  
        return "" + getData();  
    }  
}  

创建二叉树###

public Node createTree(String exp) {  
        Node[] nodes = new Node[3];  
        Node b, p = null;  
        int top = -1, k = 0, j = 0;  
        char[] exps = exp.toCharArray();  
        char data = exps[j];  
        b = null;  
        while (j < exps.length - 1) {  
            switch (data) {  
            case '(':  
                top++;  
                nodes[top] = p;  
                k = 1;  
                break;  
            case ')':  
                top--;  
                break;  
            case ',':  
                k = 2;  
                break;  
            default:  
                p = new Node(data, null, null);  
                if (b == null) {  
                    b = p;  
                } else {  
                    switch (k) {  
                    case 1:  
                        nodes[top].setLchild(p);  
                        break;  
                    case 2:  
                        nodes[top].setRchild(p);  
                        break;  
                    }  
                }  
            }  
            j++;  
            data = exps[j];  
        }  
        return b;
}  

递归执行三种遍历###

    /** 
         * pre order recursive 
         *  
         * @param node 
         */  
        public void PreOrder(Node node) {  
            if (node == null) {  
                return;  
            } else {  
                System.out.print(node.getData() + " ");  
                PreOrder(node.getLchild());  
                PreOrder(node.getRchild());  
      
            }  
        }  
      
        /** 
         * in order recursive 
         *  
         * @param node 
         */  
        public void InOrder(Node node) {  
            if (node == null) {  
                return;  
            } else {  
                InOrder(node.getLchild());  
                System.out.print(node.getData() + " ");  
                InOrder(node.getRchild());  
            }  
        }  
      
        /** 
         * post order recursive 
         *  
         * @param node 
         */  
        public void PostOrder(Node node) {  
            if (node == null) {  
                return;  
            } else {  
                PostOrder(node.getLchild());  
                PostOrder(node.getRchild());  
                System.out.print(node.getData() + " ");  
            }  
        }  

非递归遍历###

前序遍历
    public void PreOrderNoRecursive(Node node) {  
            Node nodes[] = new Node[CAPACITY];  
            Node p = null;  
            int top = -1;  
            if (node != null) {  
                top++;  
                nodes[top] = node;  
                while (top > -1) {  
                    p = nodes[top];  
                    top--;  
                    System.out.print(p.getData() + " ");  
                    if (p.getRchild() != null) {  
                        top++;  
                        nodes[top] = p.getRchild();  
                    }  
                    if (p.getLchild() != null) {  
                        top++;  
                        nodes[top] = p.getLchild();  
                    }  
                }  
            }  
        }  

中序遍历
    public void InOrderNoRecursive(Node node) {  
            Node nodes[] = new Node[CAPACITY];  
            Node p = null;  
            int top = -1;  
            if (node != null)  
                p = node;  
            while (p != null || top > -1) {  
                while (p != null) {  
                    top++;  
                    nodes[top] = p;  
                    p = p.getLchild();  
                }  
                if (top > -1) {  
                    p = nodes[top];  
                    top--;  
                    System.out.print(p.getData() + " ");  
                    p = p.getRchild();  
                }  
            }  
        }  

后序遍历
    public void PostOrderNoRecursive(Node node) {  
            Node[] nodes = new Node[CAPACITY];  
            Node p = null;  
            int flag = 0, top = -1;  
            if (node != null) {  
                do {  
                    while (node != null) {  
                        top++;  
                        nodes[top] = node;  
                        node = node.getLchild();  
                    }  
                    p = null;  
                    flag = 1;  
                    while (top != -1 && flag != 0) {  
                        node = nodes[top];  
                        if (node.getRchild() == p) {  
                            System.out.print(node.getData() + " ");  
                            top--;  
                            p = node;  
                        } else {  
                            node = node.getRchild();  
                            flag = 0;  
                        }  
                    }  
                } while (top != -1);  
            }  
        }  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,475评论 1 31
  • //转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/d...
    大海一滴写字的地方阅读 697评论 0 2
  • 树和二叉树 1、树的定义 树(Tree)是由一个 或 多个结点 组成的有限集合T,且满足: ①有且仅有一个称为根的...
    利伊奥克儿阅读 1,394评论 0 1
  • 人生其实说长也长,说短也短,那我们应该怎样度过才算不枉此生呢?这是一个引人深思的话题。 人的一生...
    诗慕凝阅读 327评论 0 0
  • 亲爱的,我做了一件特别傻的事!当时,真的很受伤。我问他会不会因为听到一首歌而想到旧情人,他说有时候会,我问是什么歌...
    王美淇阅读 225评论 0 2