Tree and forest, hashiman tree

Source: Internet
Author: User
/***** [Tree and forest] ** tree: it can be divided into ordered trees and disordered trees. we mainly study disordered trees: all kinds of tree problems can be solved by converting to a binary tree (if a binary tree represents a tree: Then the left is no longer a left subtree, but a child, and the right is not a right subtree, but a brother) * [1] Tree Representation Method: ① parental representation: [Taking sequential storage as an example] node Structure: typedef struct ptnode {elemtype data; // data domain int parent; // parent-parent location} ptnode; tree structure: typedef struct pttree {ptnode nodes [maxsize]; // sequence table int rootpos, num; // location of the root node, and number of nodes} ptree; ② child linked list representation: [Taking sequential storage as an example] child node Structure: typedef struct ctnode {int child; // place The subscripts of the child node. [Sequence Structure] struct ctnode * pnext;} * childptr; // Children's Section Point pointer: connect the child with the same parent to a chain parent node Structure: typedef struct {ELEM data; childptr firstchild; // head pointer of the Child linked list} ctbox; // parent node, tree Structure of the head pointer to store the child linked list with the same parent: typedef struct {ctbox nodes [maxsize]; // sequential storage of int num and rootpos; // number of nodes and root node location} ctree; ③ tree binary linked list (child-sibling linked list) Representation: [used for tree <=> Binary Tree] typedef struct csnode {ELEM data; // data field struct csnode * firstchild; // The left pointer of the binary linked list, pointing to the Child root [if there is a left and right subtree, only pointing to the left subtree, if there is only the right subtree, it points to the right subtree (pointing to the first subtree). If there is no left and right subtree, it points to null] struct csnode * nextsibling; // Binary Tree Right pointer, pointing to Brother node} csnode, * cstree; 1. conversion rules from forest to binary tree: If Forest F is empty, Binary Tree B is empty. Otherwise, the node (Root) is obtained from root (T1 ): the first tree T1 in the forest acts as the left subtree of the binary tree [recursive call], and the remaining t2 ..... TN corresponds to the right subtree of the binary tree. 2. conversion rules from binary tree to forest: If Binary Tree B is empty, the forest F is empty; otherwise, the root (T1) is obtained from the node (Root ); the first tree T1 of the forest is obtained from the left subtree, and the remaining tree T2, T3 ..... TN ④ ⑤ [2] traversal of trees and forests: [See] ① first root traversal [first traversal of the corresponding binary tree] ② second root traversal [Middle traversal of the corresponding binary tree] ③ traverse of the forest by hierarchy: [Reference for tree traversal only] ① traverse first [use first root traversal of tree] ② traverse in Middle [use later root traversal of tree] ③ traverse by hierarchy [3] tree traversal algorithm application ① Algorithm for Finding the depth of the tree int treedepth (cstree T) // t point The root node of the first tree in the forest is equivalent to the root node {If (! T) return 0; else {H1 = treedepth (t-> firstchild ); // calculate the depth of the subtree forest of the first tree H2 = treedepth (t-> nextsibling ); // calculate the depth return (max (H1 + 1, H2) of the sub-tree forest except the first tree ));}} ② Algorithm for outputting all the paths from the root node to the leaf node on the binary tree: [leaf node on the binary tree: Left and Right pointers are empty] void allpath (bitree T, stack & S) {If (t) {push (S, T-> data); If (! T-> left &&! T-> right) {printstack (s);} else {allpath (t-> left, S); allpath (t-> right, S );} pop (s) ;}③ all the path algorithms from the root node to the leaf node in the output tree: [in the binary tree converted from the tree, the so-called leaf node is different from the leaf node of the original tree (left and right pointers are empty), but the left child is empty, that is, the leaf node] void outpath (bitree T, stack & S) // T is the root pointer of the Binary Tree pointing to the forest {// output all the paths from the root to the leaf in the forest while (! T) {push (S, T-> data); If (! T-> lchild) printstack (s); elseoutpath (t-> lchild, S); POP (s); t = T-> rchild ;}} ④ creation of the storage structure algorithm [queue to use] :( '#', 'A') // start ('A', 'B') ('A', 'C ') ('A', 'D') ('C', 'E') ('C', 'F') ('F', 'G ') ('#', '#') // end the queue: <-a B C D E F G <-void creattree (cstree & T) {T = NULL; // first, the empty tree for (scanf (& FA, & Ch); ch! = '#'; Scanf (& FA, & Ch) {P = gettreenode (CH); // obtain the node pointer enqueue (Q, P) for the current input string ch ); // put the pointer of the current node into the queue if (FA = '#') t = P; // end esle {gethead (Q, S ); // extract the Header element from Q and place it in the S pointer while (S-> data! = FA) // if the data field of S is not the same as the characters of the two parents entered this time, take s out of the queue and take the next element from the queue! {Dequeue (Q, S); gethead (Q, S) ;}if (! (S-> firstchild) // If the left child of the node is empty {S-> firstchild = P; // The left child domain is assigned Pr = P; // R always points to the Child root node connected to the current parent node} else {r-> nextsibling = P; // if you already have a left child, then assign the value to the right child r = P ;}}}-> [4] Application of the tree [user tree (or Optimal Tree) (or user tree) the definition of the Optimal Tree: [tree with the shortest length of the weighted path, called the "Optimal Tree"] [recall] the definition of the path length of the node: the number of Route branches from the root node to the node. The path length of the tree is defined as: the path length of each node in the tree and the weighted path length of the tree are defined: the sum of weighted path lengths of all leaf nodes in the tree WPL (t) = Σ wk * LK (weights of all leaf nodes * sum of Node path lengths) [Picture below for better understanding] Optimal Tree: In all M-tree trees with N leaf nodes and the same weight, there must be a tree with the minimum length of its weight path, called the "Optimal Tree "! ② How to construct an Optimal Tree. For example, the following six leaf nodes are given: 19 21, 10, 4, 9, and 27. [using a binary tree as an example] 1. take the two smallest (272) of the leaves to form a new tree, delete the two leaf nodes, and the remaining 19 21 10 13. repeat 1 operation: (273) ---> the remaining 19 21 23. repeat 1 operation: (274) ---> the remaining 40 23. repeat operation 1: (505) ---> the remaining 40. repeat 1 operation: () ---> the remaining 90, stop repeating when a node is obtained. [See the image below] ③ prefix encoding [hashiman encoding: minimum length of the total message encoding, optimal prefix encoding: the length of each character's message encoding is equal and not long encoding: the length of each character's message encoding is not equal, but any encoding cannot be the prefix of another code! Otherwise, there will be two definitions during translation: the encoding of any character is not the prefix of another character encoding in the same character set. [user-defined encoding is an optimal prefix encoding.] (see the figure below )****/

 

Tree traversal:

 

 

 

Forest traversal:

 

 

First root traversal of the Forest:

 

 

In-order traversal of the Forest:

The length of the tree's weighted path is illustrated in the following figure:

 

 

Example of hashiman tree generation:

 

Prefix encoding example:

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.