1 / 74

Chapter 11 Multiway Trees

Chapter 11 Multiway Trees. Content Points. Orchards, Trees, and Binary Trees Traverse of Orchards and trees Huffman trees. On the classification of species.

caleb-mays
Télécharger la présentation

Chapter 11 Multiway Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 Multiway Trees

  2. Content Points • Orchards, Trees, and Binary Trees • Traverse of Orchards and trees • Huffman trees

  3. On the classification of species • A (free) tree(自由树)is any set of points (called vertices,顶点) and any set of pairs of distinct vertices (called edges or branches(边或分支)such that • (1) there is a sequence of edges (a path,路径) from any vertex to any other, • (2) there are no circuits(回路), that is, no paths starting from a vertex and returning to the same vertex.

  4. On the classification of species

  5. On the classification of species • A rooted tree(有根树)is a tree in which one vertex, called the root, is distinguished. • An ordered tree (有序树)is a rooted tree in which the children of each vertex are assigned an order. • A forest(森林)is a set of trees. We usually assume that all trees in a forest are rooted. • An orchard (also called an ordered forest,有序森林) is an ordered set of ordered trees.

  6. 例如: A C D B E F G H I J K L M A( B(E, F(K, L)),C(G),D(H, I, J(M))) 树根 T1 T2 T3

  7. on the classification of species

  8. implementations of ordered Trees • Multiple links(多重链表) • first_child and next_sibling links (孩子兄弟链表) • Correspondence with binary trees (和二叉树的对应)

  9. implementations of ordered Trees

  10. Recursive Definitions • A rooted treeconsists of a single vertex v, called the root of the tree, together with a forest F , whose trees are called the subtrees of the root. • A forest Fis a (possibly empty) set of rooted trees. • An ordered tree T consists of a single vertex v, called the root of the tree, together with an orchard O, whose trees are called the subtrees of the root v. We may denote the ordered tree with the ordered pair T ={v,O}. • An orchard O is either the empty set , or consists of an ordered tree T , called the first tree of the orchard, together with another orchard O1 (which contains the remaining trees of the orchard). We may denote the orchard with the ordered pair O =(T ,O1).

  11. The Formal Correspondence • A binary treeB is either the empty set , or consists of a root vertex v with two binary trees B1 and B2 . We may denote the binary tree with the ordered triple(元组)B= [v,B1,B2]. • THEOREM 11.1 Let S be any finite set(有限集)of vertices. There is a one-to-one correspondencef from the set of orchards whose set of vertices is S to the set of binary trees whose set of vertices is S. • Proof. Refer page 526 Define f (Φ)=Φ. Define f ({v,O1},O2)= [v, f (O1), f (O2)]. Show by mathematical induction)(数学归纳法)on the number of vertices that f is a one-to-one correspondence.

  12. Rotations • Draw the orchard so that the first child of each vertex is immediately below the vertex. • Draw a vertical link from each vertex to its first child, and draw a horizontal link from each vertex to its next sibling. • Remove the remaining original links. • Rotate the diagram 45 degrees clockwise(顺时针方向), so that the vertical links appear as left links and the horizontal links as right links.

  13. Rotations

  14. Summary • Orchards and binary trees correspond by any of: • first child and next sibling links, • rotations of diagrams, • formal notational equivalence.

  15. 对比树型结构和线性结构的结构特点

  16. 树型结构 线性结构 根结点 (无前驱) 第一个数据元素 (无前驱) 最后一个数据元素 (无后继) 多个叶子结点 (无后继) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 其它数据元素 (一个前驱、 一个后继) 其它数据元素 (一个前驱、 多个后继)

  17. Traverse of trees and orchards

  18. Preorder: 若树不空,则先访问根结点,然后依次先根遍历各棵子树。 Postorder: 若树不空,则先依次后根遍历各棵子树,然后访问根结点。 Level traverse: 若树不空,则自上而下自左至右访问树中每个结点。

  19. preorder: A B C D E F G H I J K A B E F C D G H I J K postorder: E F B C I J K H G D A Level order: A B C D E F G H I J K

  20. B C D E F G H I J K Orchards have three parts: 1.The root of the first tree 2.The orchard of the first tree 3.Other trees in the orchard

  21. 有序森林的遍历 若有序森林不空,则 访问森林中第一棵树的根结点; 先序遍历森林中第一棵树的子树森林; 先序遍历森林中(除第一棵树之外)其 余树构成的森林。 1. 先序遍历 即:依次从左至右对森林中的每一棵树进行先根遍历。

  22. 2.中序遍历 若森林不空,则 中序遍历森林中第一棵树的子树森林; 访问森林中第一棵树的根结点; 中序遍历森林中(除第一棵树之外)其 余树构成的森林。 即:依次从左至右对森林中的每一棵树进行后根遍历。

  23. 树的遍历和二叉树遍历的对应关系 ? 树 森林 二叉树 先根遍历 先序遍历 先序遍历 中序遍历 中序遍历 后根遍历

  24. 哈 夫 曼 树 与 哈 夫 曼 编 码 • 哈夫曼树 • 如何构造哈夫曼树 • 哈夫曼编码

  25. 一、哈夫曼树 树的带权路径长度定义为: 树中所有叶子结点的带权路径长度之和 WPL(T) = wklk (对所有叶子结点)。 例如: 在所有含 n 个叶子结点、并带相同权 值的 m 叉树中,必存在一棵其带权路径 长度取最小值的树,称为“最优树”。

  26. 在所有含 n 个叶子结点、并带相同权值的 二 叉树中,必存在一棵其带权路径长度取最小值的树,称为“最优二叉树”或“哈夫曼树”。

  27. 2 4 7 5 9 5 4 2 7 9 WPL(T)= 72+52+23+43+92 =60 WPL(T)= 74+94+53+42+21 =89

  28. 二、如何构造哈夫曼树 根据给定的 n 个权值 {w1, w2, …, wn}, 构造 n 棵二叉树的集合 F = {T1, T2, … , Tn}, 其中每棵二叉树中均只含一个带权值 为 wi 的根结点,其左、右子树为空树; (1)

  29. 在 F 中选取其根结点的权值为最 小的两棵二叉树,分别作为左、 右子树构造一棵新的二叉树,并 置这棵新的二叉树根结点的权值 为其左、右子树根结点的权值之 和; (2)

  30. 从F中删去这两棵树,同时加入 刚生成的新树; (3) 重复(2)和(3)两步,直至 F 中只 含一棵树为止。 (4)

  31. 例如: 已知权值 W={ 5, 6, 2, 9, 7 } 5 6 2 9 7 6 9 7 7 5 2 9 7 13 5 2 6 7

  32. 9 7 13 5 2 6 7 29 1 0 13 16 0 0 1 1 6 7 9 7 1 0 00 01 10 5 2 110 111

  33. 三、哈夫曼编码 主要用途是实现数据压缩。 设给出一段报文: CAST CAST SAT AT A TASA 字符集合是 { C, A, S, T },各个字符出现的频度(次数)是 W={ 2, 7, 4, 5 }。 (1)以ASCII 码传输 (2)若给每个字符以等长编码 A : 00 T : 10 C : 01 S : 11 则总编码长度为 ( 2+7+4+5 ) * 2 = 36. (3)若按各个字符出现的概率不同而给予不等长编码,可望减少总编码长度。

  34. 以各字符出现的次数 { 2, 7, 4, 5 }为各叶结点上的权值,建立哈夫曼树。左分支赋 0,右分支赋 1,得哈夫曼编码(变长编码)。 A : 0 T : 10 C : 110 S : 111 它的总编码长度:7*1+5*2+( 2+4 )*3 = 35。比等长编码的情形要短。

  35. Lexicographic Search Trees: Tries • Definition • One method is to prune from the tree all the branches that do not lead to any key. In English, for example, there are no words that begin with the letters ‘bb,’ ‘bc,’ ‘bf,’ ‘bg,’ : : : , but there are words beginning with ‘ba,’ ‘bd,’ or ‘be.’ Hence all the branches and nodes for nonexistent words can be removed from the tree. The resulting tree is called a trie. • A trie of order m is either empty or consists of an ordered sequence of exactly m tries of order m.

  36. Lexicographic Search Trees: Tries

  37. Lexicographic Search Trees: Tries • C++ Tie Declarations class Trie { public: // Add method prototypes here. private: // data members Trie node *root; }; const int num chars = 28; struct Trie node { // data members Record *data; Trie node *branch[num chars]; // constructors Trie node( ); };

  38. Lexicographic Search Trees: Tries • Searching a Trie Error code Trie :: trie search(const Key &target, Record &x) const /* Post: If the search is successful, a code of success is returned, and the output parameterx is set as a copy of theTrie 's record that holdstarget . Otherwise, a code ofnot present is returned. Uses: Methods of classKey . */ { int position = 0; char next char; Trie node *location = root; while (location != NULL && (next char = target.key letter(position)) != 0 0) { // Terminate search for aNULL location or a blank in the target. location = location->branch[alphabetic order(next char)]; // Move down the appropriate branch of the trie.

  39. Lexicographic Search Trees: Tries • Searching a Trie position++; // Move to the next character of the target. } if (location != NULL && location->data != NULL) { x = *(location->data); return success; } else return not present; }

  40. Lexicographic Search Trees: Tries • inserting into a Trie: Error code Trie :: insert(const Record &new entry) /* Post: If theKey ofnew entry is already in the Trie , a code of duplicate error is returned. Otherwise, a code of success is returned and the Record new entry is inserted into theTrie . Uses: Methods of classesRecord andTrie node . */ { Error code result = success; if (root == NULL) root = new Trie node; // Create a newemptyTrie . int position = 0; //indexes letters ofnew entry char next char;

  41. Lexicographic Search Trees: Tries • inserting into a Trie: Trie node *location = root; // moves through theTrie while (location != NULL && (next char = new entry.key letter(position)) != 0 0) { int next position = alphabetic order(next char); if (location->branch[next position] == NULL) location->branch[next position] = new Trie node; location = location->branch[next position]; position++; } // At this point, we have tested for all nonblank characters of new entry . if (location->data != NULL) result = duplicate error; else location->data = new Record(new entry); return result; }

  42. Multiway Search Trees • An m-way search treeis a tree in which, for some integer m called the order of the tree, each node has at most m children. • If k<=m is the number of children, then the node contains exactly k -1 keys, which partition all the keys into k subsets consisting of all the keys less than the first key in the node, all the keys between a pair of keys in the node, and all keys greater than the largest key in the node.

  43. Multiway Search Tree

  44. Balanced Multiway Trees (B-Trees)

  45. Balanced Multiway Trees (B-Trees)

  46. Insertion into a B-Tree • In contrast to binary search trees, B-trees are not allowed to grow at their leaves; instead, they are forced to grow at the root. • General insertion method: • Search the tree for the new key. This search (if the key is truly new) will terminate in failure at a leaf. • Insert the new key into to the leaf node. If the node was not previously full, then the insertion is finished. • When a key is added to a full node, then the node splits into two nodes, side by side on the same level, except that the median key is not put into either of the two new nodes. • When a node splits, move up one level, insert the median key into this parent node, and repeat the splitting process if necessary. • When a key is added to a full root, then the root splits in two and the median key sent upward becomes a new root. This is the only time when the B-tree grows in height.

  47. Growth of a B-Tree

  48. Growth of a B-Tree

  49. B-Tree Declaration in C++ • B-Tree Class declaration: template <class Record, int order> class B tree { public: // Add public methods. private: // data members B node<Record, order> *root; // Add private auxiliary functions here. };

  50. B-Tree Declaration in C++ • B-Tree Node declaration: template <class Record, int order> struct B node { // data members: int count; Record data[order - 1]; B node<Record, order> *branch[order]; // constructor: B node( ); };

More Related