《資料結構與演算法分析》第四章--樹 (1)

來源:互聯網
上載者:User

標籤:efi   div   ddc   輸出   stat   while   資料      技術   

4.1 預備知識

定義:

  樹的遞迴定義:一棵樹是一些節點的集合,這個集合若為空白集;否則由一個根結點以及該節點的0個或者若干個非空子樹組成,這些子樹都與該根節點有邊串連。

  樹葉:沒有子節點的節點。

  兄弟(Siblings):有相同父親節點的節點。

  節點n1到nj的路徑:一個節點 序列:n1,n2...,nj。

  路徑的長:路徑上的邊數。

  節點ni的深度:根節點到ni的唯一路徑的長。根節點深度為0。

  節點ni的高度:該節點ni到葉子節點的最長路徑。樹的高:根節點的高度。

4.1.1 樹的實現

左孩子右兄弟鏈表法:鏈表中增加兩個附加元素一個記錄其最左邊的孩子,另一個記錄其右邊的兄弟。(樹中子節點不分左右,這裡為了儲存方便)

//左孩子右兄弟節點表示class TreeNode<E>{    E data;    private TreeNode<E> firstChild;    private TreeNode<E> nextSibling;    public void set_firstChild(TreeNode<E> t){        firstChild=t;    }    public TreeNode<E> get_firstChild(){        return firstChild;    }    public void set_nextSiblling(TreeNode<E> t){        nextSibling=t;    }    public TreeNode<E> get_nextSiblling(TreeNode<E> t){        return nextSibling;    }    public TreeNode(TreeNode<E> firstChild,TreeNode<E> nextSibling){        this.firstChild=firstChild;        this.nextSibling=nextSibling;    }}

  這裡樹上只給了節點的資料表示,沒有給出樹上的操作,這裡我給該樹ADT增加操作;

樹ADT設計:

 1 ADT Tree{ 2   資料對象Data:D是具有左孩子右兄弟和資料的相同類型資料元素集合; 3   基本操作P: 4       InitTree(TreeNode T) 5             操作結果:構造空樹T; 6       CreateTree(TreeNode T,definition) 7              初始條件:definition給出樹T的定義; 8              操作結果:按definition構造樹 9       TreeEmpty()10              操作結果:返回是否為空白;11       Add(TreeNode k,TreeNode x)12             初始條件:k是樹中的一個節點;13             操作結果:x作為k的子節點被添加進去14       ShowPreOrder()15            操作結果:輸出樹的結構;16       17 }        

 樹的遍曆的簡單應用:

  先序遍曆:採用遞迴的方法,在根節點對子樹遞迴調用先序遍曆方法;遞迴終止條件:輸入節點為葉子節點,則列印並且停止;否則列印並遞迴向下;

class Tree<E>{    private static class TreeNode<E>    {        E data;        TreeNode<E> firstchild;        TreeNode<E> nextSibling;        TreeNode(E d)        {            data=d;            firstchild=null;            nextSibling=null;        }    }    private TreeNode<E> root=null;    public TreeNode<E> getroot(){        return root;    }    Tree()    {    }    Tree(E root)    {        this.root=new TreeNode<E>(root);    }    public boolean treeEmpty()    {        return root==null;    }    public TreeNode<E> find(int d,int w)    {        if(d<0||w<0)            throw new NullPointerException("d<0");        if (d==0)        {            return root;        }        TreeNode<E> p=root;        while(d!=0){            p=p.firstchild;            d--;        }        while(w!=0)        {            p=p.nextSibling;            w--;        }        return p;    }    public boolean addChild(int d,int w,E x)    {        return addChild(find(d,w),new TreeNode<E>(x));    }    public boolean addChild(TreeNode<E> k,E x)    {        return addChild(k,new TreeNode<E>(x));    }    public boolean addChild(TreeNode<E> k,TreeNode<E> x)    {        if(k.firstchild==null)        {            k.firstchild=x;            return true;        }        TreeNode<E> p=k.firstchild;        while(p.nextSibling!=null)        {            p=p.nextSibling;        }        p.nextSibling = x;        return true;    }    public void printPreOrder(int depth,TreeNode<E> r)    {        String nbsp="";        for(int i=0;i<depth;i++){            nbsp=nbsp+"   ";        }        System.out.println(nbsp+(String) r.data);        TreeNode<E> q=r.firstchild;        if(q!=null)        {            while(q.nextSibling!=null){                printPreOrder(depth+1,q);                q=q.nextSibling;            }            printPreOrder(depth+1,q);        }    }}

: 

 

《資料結構與演算法分析》第四章--樹 (1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.