java自定義構(gòu)造二叉樹及其遍歷

首先:二叉樹的建立

首先,我們采用廣義表建立二叉樹(關(guān)于廣義表的概念,請查看百科的介紹:http://baike.baidu.com/view/203611.htm

我們建立一個字符串類型的廣義表作為輸入:

String expression = "A(B(D(,G)),C(E,F))";與該廣義表對應(yīng)的二叉樹為

1366533767_1515.jpg

寫代碼前,我們通過觀察二叉樹和廣義表,先得出一些結(jié)論:

每當(dāng)遇到字母,將要創(chuàng)建節(jié)點
每當(dāng)遇到“(”,表面要創(chuàng)建左孩子節(jié)點
每當(dāng)遇到“,”,表明要創(chuàng)建又孩子節(jié)點
每當(dāng)遇到“)”,表明要返回上一層節(jié)點   
廣義表中“(”的數(shù)量正好是二叉樹的層數(shù)

節(jié)點類的構(gòu)建###

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();  
    }  
}  

創(chuàng)建二叉樹###

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;
}  

遞歸執(zhí)行三種遍歷###

    /** 
         * 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);  
            }  
        }  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 樹的概述 樹是一種非常常用的數(shù)據(jù)結(jié)構(gòu),樹與前面介紹的線性表,棧,隊列等線性結(jié)構(gòu)不同,樹是一種非線性結(jié)構(gòu) 1.樹的定...
    Jack921閱讀 4,765評論 1 31
  • //轉(zhuǎn)載請標(biāo)明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/d...
    大海一滴寫字的地方閱讀 769評論 0 2
  • 樹和二叉樹 1、樹的定義 樹(Tree)是由一個 或 多個結(jié)點 組成的有限集合T,且滿足: ①有且僅有一個稱為根的...
    利伊奧克兒閱讀 1,569評論 0 1
  • 人生其實說長也長,說短也短,那我們應(yīng)該怎樣度過才算不枉此生呢?這是一個引人深思的話題。 人的一生...
    詩慕凝閱讀 356評論 0 0
  • 親愛的,我做了一件特別傻的事!當(dāng)時,真的很受傷。我問他會不會因為聽到一首歌而想到舊情人,他說有時候會,我問是什么歌...
    王美淇閱讀 241評論 0 2

友情鏈接更多精彩內(nèi)容