Binary Tree Summary-tree creation and four traversal methods (recursion & amp; non-recursion)

Source: Internet
Author: User


Summary of the binary tree today. If you want to test discretization, please do not go! The most important thing about a binary tree is the establishment of four traversal methods, simple application, how to determine whether the two binary trees are similar

Binary Tree: 1. Full binary tree 2. Full Binary Tree


Structure nature:


1 ). the height of a full binary tree is h, the number of nodes is 2 ^ h-1, and the leaf nodes are all at the bottom layer. The number of leaf nodes is 2 ^ (n-1). {n indicates the number of layers of the binary tree, also called depth}

2). The depth of the Complete Binary Tree of n nodes is int (log2n) (base-2 logarithm of n) + 1;

3). Number of non-empty binary tree leaf nodes = number of dual branch nodes + 1

4). A non-empty Binary Tree node number n if there is a left child, then the left child node 2 * n. If there is a right child, the node number is 2 * n + 1

5) if you know the two traversal methods, you can see the third Traversal method.

6) Determine whether the two binary trees are the same. You only need to determine the traversal sequence corresponding to the two binary trees.


Achievements:


It is known that the input characters are the first sequence of a binary tree, such as abcXXdeXgXXfXXX (where X indicates an empty node ).


Struct node * make () {char c; node * st; c = getchar (); if (c = 'X') st = NULL; else {st = (struct node *) malloc (sizeof (struct node); st-> data = c; // known for first-order traversal, first fill the root node st-> left = make (); // recursively fill the left branch st-> right = make (); // recursively fill the left branch} return st ;}


Principal + senA + Principal/a0 + Principal + CjxzdHJvbmc + 0 ruhos/Principal + b3ateM8L3A + Principal + yc/Principal/rLmyve1xM/Principal + CjxzdHJvbmc + Principal + principal /examples + b3ateM8YnI + examples/examples + CjxzdHJvbmc + examples + PGJyPgo8YnI + CiAgICAxLs/examples + c?vcd4kpha + examples/examples + rjDsuPL + PGJyPgo8L3A + CjxwPsnPyva13bnpyr7S4s28yOfPwqO6PC9wPgo8cD48YnI + cda-vcd4kpha + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090831392.jpg" alt = "\">


Binary Tree depth


Determine from the left and right branches of the current node, who will increase by 1


Determine whether a binary tree is similar

1. All nodes correspond to the same between the left and right children.

2. If any two traversal methods are the same, the two trees will be the same.


Code Template:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include const int N = 1010; using namespace std; char a [100]; struct node {char data; node * left; node * right ;}; struct node * make () {char c; node * st; c = getchar (); if (c = 'X') st = NULL; else {st = (struct node *) malloc (sizeof (struct node); st-> data = c; // knows to traverse in the first order, first fill in the root node st-> left = make (); // recursively fill the left branch st-> right = make (); // recursively fill the left branch} return st;} void First_Order (struct node * t) // recursive form of first-order traversal {if (t = N ULL) return; printf ("% c->", t-> data); First_Order (t-> left); First_Order (t-> right );} void First_Order_1 (struct node * t) // first traverse non-recursive FORM {struct node * stk [N], * p; int top =-1; if (t! = NULL) {top ++; stk [top] = t; // The root node is added to the stack while (top>-1) {p = stk [top]; // exit the stack and access the top --; printf ("% c->", p-> data); if (p-> right! = NULL) // The right child enters the stack {top ++; stk [top] = p-> right;} if (p-> left! = NULL) // The left child enters the stack {top ++; stk [top] = p-> left ;}}} void Mid_Order (struct node * t) // recursive form of central order traversal {if (t = NULL) return; Mid_Order (t-> left); printf ("% c->", t-> data ); mid_Order (t-> right);} void Mid_Order_1 (struct node * t) // first traverse non-recursive forms {struct node * stk [N], * p; int top =-1; if (t! = NULL) {p = t; while (top>-1 | p! = NULL) // traverse the left branch {while (p! = NULL) // press the left branch of the current t node into the stack {top ++; stk [top] = p; p = p-> left ;} // After the while operation ends, the top element of the stack may not have any left branch node or the left branch node has been accessed. if (top>-1) {p = stk [top]; // output the stack and print top --; printf ("% c->", p-> data); p = p-> right; // traverse the right branch }}} void Last_Order (struct node * t) // recursive form of post-order traversal {if (t = NULL) return; last_Order (t-> right); Last_Order (t-> left); printf ("% c->", t-> data);} void Print_Leaf (struct node * t) {if (t! = NULL) {if (t-> left = NULL & t-> right = NULL) {printf ("% c", t-> data );} print_Leaf (t-> left); // access the leaf node Print_Leaf (t-> right) of the left branch ); // access the leaf node of the right branch} void Ceng_Order (struct node * t) // perform hierarchical traversal and use a circular queue to implement {struct node * que [N], * p; int f, r; // The head pointer and tail pointer of the queue f =-1; r =-1; que [++ r] = t; // enter the root node while (f! = R) {f = (f + 1) % N; // prevent team overflow p = que [f]; // The queue header node leaves the printf ("% c->", p-> data); if (p-> left! = NULL) // press the left child into the queue {r = (r + 1) % N; que [r] = p-> left;} if (p-> right! = NULL) // press the right child into the queue {r = (r + 1) % N; que [r] = p-> right ;}}} int shendu (struct node * t) {int x = 0, y = 0; if (t! = NULL) {x = shendu (t-> left); y = shendu (t-> right); if (x> y) return (x + 1 ); else return (y + 1);} else return 0;}/* bool Like (struct node * t1, struct node * t2) // determine whether the two trees are similar {bool like1, like2; if (t1 = NULL & t2 = NULL) return true; // all corresponding branches are the same else if (t1 = NULL | t2 = NULL) return false; else {like1 = Like (t1-> left, t2-> left); like2 = Like (t1-> right, t2-> left); return (like1 & like2 ); // The result returned is the sum of like1 and like2} */int main () {struct node * t; t = make (); // build puts ("first-order traversal, recursive form "); First_Order (t); cout <" END "<
     
      



Related Article

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.