1. 程式人生 > >樹結構的自定義及基本演算法(Java資料結構學習筆記)

樹結構的自定義及基本演算法(Java資料結構學習筆記)

資料結構可以歸類兩大型別:線性結構與非線性結構,本文的內容關於非線性結構:的基本定義及相關演算法。關於樹的一些基本概念定義可參考:維基百科
樹的ADT模型:
根據樹的定義,每個節點的後代均構成一棵樹樹,稱為子樹。因此從資料型別來講,樹、子樹、樹節點是等同地位,可將其看作為一個節點,用通類:Tree表示。如下圖所示:
這裡寫圖片描述
圖:Tree ADT模型示意圖
可採用“父親-兒子-兄弟”模型來表示樹的ADT。如圖所示,除資料項外,分別用三個引用表示指向該節點的父親,兒子,兄弟。
這裡寫圖片描述

圖:“父親-兒子-兄弟”模型的資料結構示意圖
表:樹ADT實現的操作
這裡寫圖片描述

就對樹的更新操作而言,不同的應用問題會要求樹結構提供不同方法。這方面的差異太大,無法在樹 ADT 中定義出通用的更新操作。在之後將結合各種應用問題,陸續給出一些具體的更新操作的實現。

樹的java介面:

package com.tree;

/**
 * Java Structure for Tree
 * Construct interface Tree
 * @author gannyee
 *
 */
public interface Tree {
    //Get size of tree which recent node as parent
    public int getSize();

    //Get height of recent node
    public int getHeight();

    //Get depth of recent node
public int getDepth(); //Get element of recent node public Object getElement(); //Set element of recent node, return former element public Object setElement(Object newElement); //Get parent of recent node public TreeLinkedList getParent(); //Get first child of recent node
public TreeLinkedList getFirstChild(); //Get biggest sibling of recent node public TreeLinkedList getNextSibling(); }

Java程式碼:樹節點模型實現

package com.tree;

import java.lang.reflect.Constructor;

public class TreeLinkedList implements Tree{
    //Pointer point to parent
    private TreeLinkedList parent;
    //Element
    private Object element;
    //Pointer point to firstChild
    private TreeLinkedList firstChild;
    //Pointer point to nextSibling
    private TreeLinkedList nextSibling;

    //Constructor
    public TreeLinkedList(){
        this(null,null,null,null);
    }

    //Constructor with parameters
    public TreeLinkedList(TreeLinkedList p, Object e,TreeLinkedList f,TreeLinkedList n){
        this.parent = p;
        this.element = e;
        this.firstChild = f;
        this.nextSibling = n;
    }
    //Get size of tree which recent node as parent
    public int getSize(){
        int size = 1; //Recent node also include own children
        TreeLinkedList subTree = firstChild;//Start with first child
        while(null != subTree){
            size += subTree.getSize(); 
            subTree = subTree.getNextSibling();//Get all descendants
        }
        return size;
    }

    //Get height of recent node
    public int getHeight(){
        int height = -1;//Recent node's(parent) height
        TreeLinkedList subTree = firstChild;//Start with first child

        while(null != subTree){
            height = Math.max(height, subTree.getHeight());//Get the max height
            subTree = subTree.getNextSibling();
        }
        return height + 1;//Get recent node height
    }

    //Get depth of recent node
    public int getDepth(){
        int depth = 0;
        TreeLinkedList p = parent;//Start with parent
        while(p != null){
            depth ++;
            p = p.getParent();//Get all parents of every node
        }
        return depth;
    }

    //Get element of recent node,if nothing return null
    public Object getElement(){
        return this.element;
    }

    //Set element of recent node,return former element
    public Object setElement(Object newElement){
        Object swap;
        swap = this.element;
        this.element = newElement;
        return this.element;
    }

    //Get parent of recent node,if nothing return null
    public TreeLinkedList getParent(){
        return parent;
    }

    //Get first child of recent node,if nothing return null
    public TreeLinkedList getFirstChild(){
        return firstChild;
    }

    //Get biggest sibling of recent node,if nothing return null
    public TreeLinkedList getNextSibling(){
        return nextSibling;
    }
}

測試程式碼:

package com.tree;

public class TreeTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        TreeLinkedList a = new TreeLinkedList();
        TreeLinkedList b = new TreeLinkedList();
        TreeLinkedList c = new TreeLinkedList();
        TreeLinkedList d = new TreeLinkedList();
        TreeLinkedList e = new TreeLinkedList();
        TreeLinkedList f = new TreeLinkedList();
        TreeLinkedList g = new TreeLinkedList();

        a = new TreeLinkedList(null,0,d,null);
        b = new TreeLinkedList(a,1,d,c.getFirstChild());
        c = new TreeLinkedList(a,2,null,null);
        d = new TreeLinkedList(b,3,f,e.getFirstChild());
        e = new TreeLinkedList(b,4,null,null);
        f = new TreeLinkedList(d,5,null,g.getFirstChild());
        g = new TreeLinkedList(d,6,null,null);

        System.out.println(a.getDepth());
        System.out.println(b.getDepth());
        System.out.println(c.getDepth());
        System.out.println(d.getDepth());
        System.out.println(e.getDepth());
        System.out.println(f.getDepth());

        System.out.println(a.getHeight());
        System.out.println(b.getHeight());
        System.out.println(c.getHeight());
        System.out.println(d.getHeight());
        System.out.println(e.getHeight());
        System.out.println(f.getHeight());
        System.out.println(g.getHeight());

    }

}

測試結果:

0
1
2
com.tree.TreeLinkedList@7c1c8c58
1
null
null

後續將給出關於樹的遍歷演算法!

參考文獻:資料結構與演算法( Java 描述)鄧俊輝 著