Binary Tree Learning in detail [clue binary tree also has some confusion]

Source: Internet
Author: User
Tags stack pop
/*** [1] understand the concept of a binary tree: Left subtree, right subtree, root, cousin, left child, right child, parent node * Nature 1: at most 2 ^ I nodes exist on the I layer of a binary tree. 2: A binary tree with a depth of H. the maximum number of nodes is: 2 ^ 0 + 2 ^ 1 + 2 ^ 2 +... 2 ^ (h-1) = 2 ^ h-1 properties 3: [except the root node has no parent, other nodes have parent] number of nodes = number of branches + 1 * Full Binary Tree: A binary tree with a depth of K and 2 ^ k-1 nodes. That is, A Complete Binary Tree With a node with a degree of 1 does not exist: the N nodes in the tree correspond to the nodes numbered from 1 to n in the full binary tree one by one (that is, you must sort them up to the bottom and right). features: (1) if there are n nodes, its depth d = [logn] + 1; // The log base on 2, after the N logarithm is obtained, rounded up + 1 * (2) if a Binary Tree Containing N nodes is numbered 1 to n from top to bottom and left to right [full Binary Tree], any node numbered I in the binary tree is: 1. if I = 1, the node is the root node and has no parent nodes. Otherwise The rounded down node with no. I/2 is its parent node 2. if 2I> N, no child is left on the node; otherwise, node 2I is 3. if 2I + 1> N, the node has no right child node; otherwise, the node numbered 2I + 1 is the chain storage of the Binary Tree of the right child node [2]: 1. binary linked list type typedef struct trnode {telemtype data; // data field struct trct trnode * lchild, * rchild; // pointer to left child, right child} trtrnode, * trtrree; 2. double linked list type: [A pointer field pointing to a parent node is more than the binary linked list type] typedef struct trnode {telemtype data; // data field struct trnode * lchild, * rchild; // pointer to the left child and the Right child struct trnode * parent; // pointer to the parent node} trtrnode, * Trree; 3. parent-Child linked list type typedef struct bptnode {telemtype data; int * parent; // pointer to the parent-child char lrtag; // left and right child flag} bptnode; // node type typedf struct bptree {bptnode nodes [maxsize]; int num; // number of nodes int root; // location of the root node} bptree; // Binary Tree Type 4. clue linked list [explained later] pointer to the "precursor" and "Successor" in the linear sequence, called "clue ", in the storage structure, "clue" is called "Clue linked list". [3] binary tree traversal (1) first-left-right Traversal Algorithm 1. first traverse 2. traverse in the middle order 3. post-order traversal recursion form [main]: void preorder (bitree T, void (* visit) (telemtype &) e) {// first-order traversal of binary tree if (t) {visit (t-> data); // access Ask the root node preorder (t-> lchild, visit); // traverse the left subtree preorder (t-> rchild, visit ); // traverse the right subtree} non-recursive form [Use Stack]: // take the ordinal traversal as an example. When accessing a root node, if the node has a left subtree, if the left subtree is null, The bitnode * gofarleft (bitree T, stack * s) {If (! T) // If the node itself is empty, nullreturn null is returned; while (t-> lchild) // If the left subtree is not empty, it is always in the stack, until null {push (S, T); // T = T-> lchild; // make t equal to the left subtree of the current t} return t ;} void inorder (bitree T, void (* visit) (telemtype & E) {stack * s; t = gofarleft (t, s ); // find the leftmost node while (t) {visit (t-> data); If (t-> rchild) {T = gofarleft (t-> rchild, s);} else if (! Statckempty (s) // If the stack is not empty, you can roll back the stack {T = POP (s); // point to the node at the top of the stack} else {T = NULL; // stack empty indicates that all traversal is finished !}}} [4] Use Traversal Algorithms to Solve the Problem 1. count the number of leaf nodes in a binary tree (first-order traversal), recursively call void countleaf (bitree T, Int & COUNT) {If (t) {If ((! T-> lchild )&&(! T-> rchild) // left subtree, and right subtree are empty, so it is the leaf node {count ++;} countleaf (t-> lchild, count ); countleaf (t-> rchild, count) ;}} 2. calculate the depth of the binary tree (post-order traversal) and perform recursive call analysis: the depth of the current tree = the maximum value of the subtree depth + 1 because the depth of the Left subtree and the depth of the right subtree need to be obtained, compare to get the maximum value, so traverse int depth (bitree t) {If (! T) depthval = 0; else {depthleft = depth (t-> lchild); depthright = depth (t-> rchild); depthval = 1 + (depthleft> depthright )? Depthleft: depthright;} return depthval;} 3. copy a binary tree (post-sequential traversal) // algorithm used to generate a new binary tree node: bitnode * gettreenode (telemtype item, bitnode * lptr, bitnode * rptr) {If (! (T = (bitreenode *) malloc (sizeof (bitnde) // dynamically allocate space exit (1); t-> DATA = item; // copy the data domain T-> lchild = lptr; t-> rchild = rptr; return t;} // copy algorithm: bitnode * copytree (bitnode * t) {If (! T) return NULL; If (t-> lchild) newlptr = copytree (t-> lchild); elsenewlptr = NULL; // The left subtree pointer field points to null if (t-> rchild) newrptr = copytree (t-> rchild); elsenewrptr = NULL; // The right subtree pointer field points to null newnode = gettreenode (t-> data, newlptr, newrptr); Return newnode;} 4. create a binary tree storage structure ① create a binary linked list status createbitree (bitree & T) {scanf (& Ch); // If the input is a ''space character, it indicates nullif (CH = '') t = NULL; // The returned root node is empty else {If (! (T = (bitnode *) malloc (sizeof (bitnode) Exit (overflow); t-> DATA = CH; // generate the root node createbitree (t-> lchild); // construct the left subtree createbitree (t-> rchild); // construct the right subtree} Return OK ;} // createbitree ② create the corresponding binary tree based on the given expression [convert the learned stack expression into a suffix expression + binary tree traversal] // Algorithm for creating leaf nodes: void crtnode (bitree & T, char ch) {T = (bitnode *) malloc (sizeof (bitnode); t-> DATA = CH; t-> lchild = T-> rchild = NULL; // The left and right subtree of the leaf node should be nullpush (PTR, T);} // create a sub-tree algorithm: void crtsubtree (bitree & T, char c) {T = (bitnode *) malloc (sizeof (bitnode); t-> DATA = C; POP (PTR, RC ); // exit the right subtree pointer and put it in the RDBMS-> rchild = RC; POP (PTR, LC); // exit the left subtree pointer and put it in the LCT-> lchild = Lc; push (PTR, T);} void crtexptree (bitree & T, char exp []) // Method for changing the binary tree expression {// two stacks are required, one storage operator, another storage suffix expression initstack (s); // initialize a stack and place the operator push (S, '#'); // put one at the beginning # at the bottom, it has the lowest priority initstack (PTR); // The stack char * P = exp of the expression; // The Pointer Points to the expression char CH = * P; while (! (Gettop (S) = '#' & Ch = '#') // if the expression is not over and the stack has not reached the bottom {If (! In (CH, OP) // If the operator {crtnode (T, CH) is not used; // create a leaf node and merge it into the stack} else // if the operator is used, {Switch (CH) {Case '(': // left extension push (S, CH); break; Case ')': // when the right extension code is generated to '(' {Pop (S, C); While (C! = '(') {Crtsubtree (T, C); // create a binary tree and merge it into the stack POP (S, C); // continue the pop-up, until '('} break; default: // other operators {While (! Gettop (S, C) & (precede (C, CH ))) // compare the character extracted from the expression with the top-level character of the stack, which has a higher priority {// if the current operator has a lower priority than the top-level operator, the off-stack crtsubtree (T, C); POP (S, C) ;}if (Ch! = '#') // If the current character is #, that is, the expression is over. Otherwise, the expression is not over. After the while character is over, you can safely import it to the stack {push (S, ch) ;}} break ;}}③ determine a binary tree by means of first-order traversal + middle-order traversal. For example, the first-order sequence of known binary trees: abcdefg middle-order sequence: cdbaepidermal analysis: the root node is a, the left sub-tree is BCD, And the right sub-tree is. [5] Clue binary tree [no stack is required for traversing the clue linked list] (1) Definition: the pointer to the "precursor" and "Successor" in the linear sequence, called "clue". The storage structure contains "clue", which is called "Clue linked list" and its corresponding binary tree, it is called a "Clue Binary Tree". [There are first-order clue-Based Binary Trees, middle-order clue-Based Binary Trees, and post-order clue-Based Binary Trees.] precursor clue: Add the following clues to the node where the left subtree of the node is empty: the Node Type of the linked list of the clues added to the node with the right subtree empty: [two more flag domains than the common binary linked list type] adds two flag domains to the node of the binary linked list, and stipulates that if the left subtree of the node is not empty, lchil The pointer of the D field points to its left subtree, and the value of the left flag field is 0. Otherwise, the pointer field of the lchild field points to the precursor node, the value of the left flag field is 1. If the right subtree of the node is not empty, the rchild field Pointer Points to its right subtree, and the value of the right flag field is 0. Otherwise, the pointer field of the rchild domain points to the successor node, and the value of the right flag field is 1 Type Definition: typedef Enum {link, thread} pointerthr; // Enumeration type: link = 0: pointer, thread = 1: clue typedef struct bithrnode {telemtype data; struct bithrnode * lchild, * rchild; // left/right pointer pointerthr ltag, rtag; // left/right flag} bithrnode, * bithrtree; [Note: an empty head node root is added for the clue binary tree.] (2) traversal of the clue binary tree [sequential clue traversal] Status inordertra Versethr (bithrtree T. status, (* visit) (telemtype E) {P = T-> lchild; // T is the root node [empty node] at the beginning ], so P points to the root node while (P! = T) // when the tree is left or the traversal ends, P = t {While (p-> ltag = link) // If the left flag field of the node is = link, so there is a left subtree {// keep going down until you find a node without a left subtree, then this node is the first node accessed by this tree [central traversal] P = p-> lchild;} If (! Visit (p-> data) Return Error; while (p-> rtag = thread & P-> rchild! = T) // if the right flag domain is a clue domain [that is, it points to a successor node], and the successor node does not point to the header pointer [root (null)] {P = p-> rchild; // then let P point to its successor node and access its successor node visit (p-> data );} P = p-> rchild; // P goes to the right subtree and then traverses the right subtree} // whilereturn OK;} (3) how to make a clue to a binary tree [demonstrate with first-order traversal] [recursive call method] void inthreading (bithrtree p) {If (P) {// use pre to point to the front node of the current node, and use P to point to the current node inthreading (p-> lchild); // The right subtree leads If (! P-> lchild) // If the left subtree is empty {P-> ltag = thread; // The left flag field = clue p-> lchild = pre; // create a lead.} If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = P; // create a successor clue} Pre = P; // keep the pre-directed P precursor inthreading (p-> rchild); // right subtree clue} // inthreadingstatus inorderthreading (bithrtree & thrt, bithrtree T) {If (! (Thrt = (bithrtree) malloc (sizeof (bithrnode) {exit (overflow); // assign a header node [root]} thrt-> ltag = link; thrt-> rtag = thread; thrt-> rchild = thrt; // The right clue of the header node points to itself if (! T) // if it is an empty tree, the left flag field of the header node should be changed to thread, and then point to itself {thrt-> ltag = thread; // if it is an empty tree, the successor is their own thrt-> lchild = thrt;} else {thrt-> lchild = T; // The left pointer field of the header node points to the root of the binary tree [truly valued roots] pre = thrt; // The header node [root] is saved as the front node [temporarily saved] inthreading (t); // The middle-order clue tpre-> rchild = thrt; pre-> rtag = thread; thrt-> rchild = pre; // process the last node Return OK ;}}*/

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.