Chapter 11 Multiway Trees. Content Points. Orchards, Trees, and Binary Trees Traverse of Orchards and trees Huffman trees. On the classification of species.
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Content Points • Orchards, Trees, and Binary Trees • Traverse of Orchards and trees • Huffman trees
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.
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.
例如: 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
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).
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.
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.
Summary • Orchards and binary trees correspond by any of: • first child and next sibling links, • rotations of diagrams, • formal notational equivalence.
三、哈夫曼编码 主要用途是实现数据压缩。 设给出一段报文: 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)若按各个字符出现的概率不同而给予不等长编码,可望减少总编码长度。
以各字符出现的次数 { 2, 7, 4, 5 }为各叶结点上的权值,建立哈夫曼树。左分支赋 0,右分支赋 1,得哈夫曼编码(变长编码)。 A : 0 T : 10 C : 110 S : 111 它的总编码长度:7*1+5*2+( 2+4 )*3 = 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.
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( ); };
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.
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; }
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;
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; }
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.
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.
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. };
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( ); };
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.