標籤:level public lse insert 判斷 刪除 one onclick 擷取
樹的定義是遞迴的,用樹來定義樹。因此,樹(以及二叉 樹)的許多演算法都使用了遞迴。
結點(Node):表示樹中的資料元素。
結點的度(Degree of Node):結點所擁有的子樹的個數。
樹的度(Degree of Tree):樹中各結點度的最大值。
葉子結點(Leaf Node):度為 0 的結點,也叫終端結點。
結點的層次(Level of Node):從根結點到樹中某結點所經路徑上的分支 數稱為該結點的層次。根結點的層次規定為 1,其餘結點的層次等於其雙親結點 的層次加 1。
二叉樹的形態共有 5 種:空二叉樹、只有根結點的二叉樹、右子樹為空白的二 叉樹、左子樹為空白的二叉樹和左、右子樹非空的二叉樹。
滿二叉樹(Full Binary Tree):如果一棵二叉樹只有度為 0 的結點和度為 2 的結點,並且度為 0 的結點在同一層上。
完全二叉樹(Complete Binary Tree):深度為 k,有 n 個結點的二叉樹若且唯若其每一個結點都與深度為 k,有 n 個結點的滿二叉樹中編號從1到n 的結點一一對應時
二叉樹的二叉鏈表格儲存體結構:一個資料域和兩個參考網域。
不帶頭結點的 二叉樹的二叉鏈表的類 BiTree<T>類的實現:
public class BiTree<T> { private Node<T> head; //頭引用 //頭引用屬性 public Node<T> Head { get{return head;} set{head=value;} } //構造器 public BiTree() { head = null; } //構造器 public BiTree(T val) { Node<T> p = new Node<T>(val); head = p; } //構造器 public BiTree(T val, Node<T> lp, Node<T> rp) { Node<T> p = new Node<T>(val,lp,rp); head = p; } //判斷是否是空二叉樹 public bool IsEmpty() { if (head == null) { return true; } else { return false; } } //擷取根結點 public Node<T> Root() { return head; } //擷取結點的左孩子結點 public Node<T> GetLChild(Node<T> p) { return p.LChild; } //擷取結點的右孩子結點 public Node<T> GetRChild(Node<T> p) { return p.RChild; } //將結點p的左子樹插入值為val的新結點,原來的左子樹成為新結點的左子樹 public void InsertL(T val, Node<T> p) { Node<T> tmp = new Node<T>(val); tmp.LChild = p.LChild; p.LChild = tmp; } //將結點p的右子樹插入值為val的新結點,原來的右子樹成為新結點的右子樹 public void InsertR(T val, Node<T> p) { Node<T> tmp = new Node<T>(val); tmp.RChild = p.RChild; p.RChild = tmp; } //若p非空,刪除p的左子樹 public Node<T> DeleteL(Node<T> p) { if ((p == null) || (p.LChild == null)) { return null; } Node<T> tmp = p.LChild; p.LChild = null; return tmp; } //若p非空,刪除p的右子樹 public Node<T> DeleteR(Node<T> p) { if ((p == null) || (p.RChild == null)) { return null; } Node<T> tmp = p.RChild; p.RChild = null; return tmp; } //判斷是否是葉子結點 public bool IsLeaf(Node<T> p) { if ((p != null) && (p.LChild == null) && (p.RChild == null)) { return true; } else { return false; } } BiTree
二叉樹的遍曆:DLR(先序遍曆)、LDR(中序遍曆)和 LRD(後序遍曆),層序遍曆(Level Order)。
哈夫曼樹(Huffman Tree),又叫最優二叉樹,指的是對於一組具有確定權值 的葉子結點的具有最小帶權路徑長度的二叉樹。
C#資料結構_樹