数据结构之二叉数的实现

来源:互联网 发布:su软件是什么软件p图 编辑:程序博客网 时间:2024/05/23 16:53

1、二叉树的建立
首先,我们采用广义表建立二叉树(关于广义表的概念,请查看百科的介绍:http://baike.baidu.com/view/203611.htm)
我们建立一个字符串类型的广义表作为输入:
String expression = “A(B(D(,G)),C(E,F))”;与该广义表对应的二叉树为:

这里写图片描述
写代码前,我们通过观察二叉树和广义表,先得出一些结论:
每当遇到字母,将要创建节点
每当遇到“(”,表面要创建左孩子节点
每当遇到“,”,表明要创建又孩子节点
每当遇到“)”,表明要返回上一层节点
广义表中“(”的数量正好是二叉树的层数

之前一直理解不了为什么b=p之后返回的b就拥有了左右2个字树,想了一晚上才发现自己走进了一个误区。就是java的对象全都是引用。

package shujujiegou;/** * Created by lcc on 2017/6/22. */@SuppressWarnings("all")public class ErTree {    public static void main(String[] args) {        ErTree lcc = new ErTree();        String  expression = "A(B(D(,G)),C(E,F))";        Node b = lcc.createTree(expression);        Node c = b.getLchild();        lcc.PostOrder(b);        System.out.println("xxxx");        lcc.PostOrderNoRecursive(b);    }     private Node createTree(String exp) {        Node[] nodes = new Node[99];        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;    }        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 void PreOrder(Node node) {        if (node == null) {            return;        } else {            PreOrder(node.getLchild());            System.out.print(node.getData() + " ");            PreOrder(node.getRchild());        }    }    public void InOrder(Node node) {        if (node == null) {            return;        } else {            InOrder(node.getLchild());            System.out.print(node.getData() + " ");            InOrder(node.getRchild());        }    }    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[99];        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) {  //DBBAADBBAA   DGBAECF        Node nodes[] = new Node[99];        Node p = null;        int top = -1;        if (node != null)            p = node;        while (p != null) {            top++;            nodes[top] = p;            p = p.getLchild();        }        while (top > 0) {            System.out.print(nodes[top].getData());            if (nodes[top].getRchild() != null) {                System.out.print(nodes[top].getRchild().getData());            }            top--;        }        System.out.print(nodes[top].getData());        if (nodes[top].getRchild() != null) {            p = nodes[top].getRchild();            while (p != null) {                top++;                nodes[top] = p;                p = p.getLchild();            }            while (top >0) {                System.out.print(nodes[top].getData());                if (nodes[top].getRchild() != null) {                    System.out.print(nodes[top].getRchild().getData());                }                top--;            }        }    }    public void PostOrderNoRecursive(Node node) {        Node[] nodes = new Node[99];        Node p = null;        int top =-1;        if (node != null)            p = node;        while(p!=null) {            top++;            nodes[top] =p;            p=p.getLchild();        }        while(top>0) {            if (nodes[top].getRchild() != null) {                System.out.print(" "+nodes[top].getRchild().getData());            }            System.out.print(" "+nodes[top].getData());            top--;        }        p=nodes[top].getRchild();        while(p!=null) {            top++;            nodes[top] =p;            p=p.getLchild();        }        while(top>0) {            if (nodes[top].getRchild() != null) {                System.out.print(" "+nodes[top].getRchild().getData());            }            System.out.print(" "+nodes[top].getData());            top--;        }        System.out.print(" "+nodes[top].getData());    }

参考:http://blog.csdn.net/zhangerqing/article/details/8822476