Basic Algorithms of binary trees

Source: Internet
Author: User
# Include binary tree creation, traversal algorithm (non-recursive), recursion in other code, depth algorithm, number of leaf nodes algorithm. # Include # includeusingnamespacestd; typedefstructtree {chardata; structtree * lchild, * rchild;} tree, * bitree
# Include
 
  
// Establish a binary tree, traverse the algorithm (non-recursive), and recursion in other code, such as the depth algorithm and the number of leaf nodes. # Include
  
   
# Include
   
    
Using namespace std; typedef struct tree {char data; struct tree * lchild, * rchild;} tree, * bitree; typedef struct post {bitree bit; char tag;} post, * postorder; int createtree (bitree & T) {char data; cin> data; if (data = ',') {T = NULL ;} else {T = new tree; T-> data = data; createtree (T-> lchild); createtree (T-> rchild);} return 0 ;} void preorder (bitree T) {stack
    
     
Stack; bitree p = T; while (p |! Stack. empty () {if (p! = NULL) {stack. push (p); cout <
     
      
Data; p = p-> lchild;} else {p = stack. top (); stack. pop (); p = p-> rchild ;}} void inorder (bitree T) {stack
      
        Stack; bitree p = T; while (p |! Stack. empty () {if (p! = NULL) {stack. push (p); p = p-> lchild;} else {p = stack. top (); cout <
       
         Data; stack. pop (); p = p-> rchild ;}} void postordertree (bitree T) {stack
        
          Stack; bitree p = T; postorder bt; while (p |! Stack. empty () {while (p! = NULL) {bt = new post; bt-> bit = p; bt-> tag = 'l'; stack. push (bt); p = p-> lchild;} while (! Stack. empty () & (stack. top ()-> tag = 'r') {bt = stack. top (); stack. pop (); cout <
         
           Bit-> data;} if (! Stack. empty () {bt = stack. top (); bt-> tag = 'R'; p = bt-> bit; p = p-> rchild ;}} void levelorder (bitree T) {queue
          
            Queue; bitree p = T; queue. push (p); while (! Queue. empty () {p = queue. front (); cout <
           
             Data; queue. pop (); if (p-> lchild! = NULL) queue. push (p-> lchild); if (p-> rchild! = NULL) queue. push (p-> rchild) ;}} int high_count (bitree T) {int left, right, high; if (! T) {return 0;} left = high_count (T-> lchild); right = high_count (T-> rchild); high = (left> right? Left: right) + 1; return high;} int yezi_count (bitree T, int & n) {if (T) {if (T-> lchild = NULL & T-> rchild = NULL) n ++; yezi_count (T-> lchild, n ); yezi_count (T-> rchild, n);} return n;} int main () {int s = 0; bitree T; createtree (T); preorder (T ); cout <"\ n"; inorder (T); cout <"\ n"; postordertree (T); cout <"\ n"; levelorder (T ); cout <"\ n"; int h = high_count (T); cout <
            
             

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.