Traversal of binary trees (binary tree)

Source: Internet
Author: User
Tags printf

A tree is a more important data structure, especially a binary tree. A binary tree is a tree structure with a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right subtree (subtree). Binary trees are often used to implement a two-fork search tree and a two-fork heap.

Binary tree node definition:

#define ElementType Char
typedef struct NODE {
    elementtype data;
    struct Node *lchild;
    struct Node *rchild;
} BinaryTree;


One or two cross-tree traversal recursive traversal algorithm 1. The recursive traversal algorithm of First order traversal

 /* (1) The traversal process of the first order traversal recursive method
 is:
    1. Access the root node
    2. Traverse its left subtree
    3. Traverse its right subtree
*
/void Preordertraversal ( Binarytree* bt) {
    if (BT) {
        printf ("%c", bt->data);
        Preordertraversal (bt->lchild);
        Preordertraversal (Bt->rchild);
    }
}


2. Middle sequence traversal recursive traversal algorithm

 /* (2) Middle sequence traversal recursive method
 traversal process:
    1. Traverse its left subtree
    2. Access the root node
    3. Traverse its right subtree
*
/void Inordertraversal (binarytree* BT) {
    if (BT) {
        inordertraversal (bt->lchild);
        printf ("%c", bt->data);
        Inordertraversal (Bt->rchild);
    }
}


3. Sequential traversal recursive traversal algorithm

 /* (3) post-traversal recursive method
 traversal process:
    1. Traverse its left subtree
    2. Traverse its right subtree
    3. Access the root node
*
/void Postordertraversal ( Binarytree* bt) {
    if (BT) {
        postordertraversal (bt->lchild);
        Postordertraversal (bt->rchild);
        printf ("%c", Bt->data);
    }
}


Verify the following:

#include <stdio.h> #include <stdlib.h> #define ElementType char typedef struct NODE {ElementType data;
    struct Node *lchild;
struct Node *rchild;

}binarytree;
    Create a two-fork tree node binarytree* createbinarytree (data) {binarytree* T = (binarytree*) malloc (sizeof (BinaryTree)); if (!t) {printf ("insufficient space.")
        \ n ");
    return NULL;
    } t->lchild = NULL;
    T->rchild = NULL;
    T->data = data;
return t; }/* (1) The traversal process of the first order traversal recursive method is: 1. Access the root node 2. Traverse its left subtree 3. Traverse its right subtree */void Preordertraversal (binarytree* bt) {if (BT)
        {printf ("%c", bt->data);
        Preordertraversal (Bt->lchild);
    Preordertraversal (Bt->rchild); }///(2) The traversal of the recursive method in the sequence traversal: 1. Traverse its left subtree 2. Access the root node 3. Traverse its right subtree */void Inordertraversal (binarytree* bt) {if (BT)
        {inordertraversal (bt->lchild);
        printf ("%c", bt->data);
    Inordertraversal (Bt->rchild); }/* (3) post-traversal recursive method traversal process: 1. Traverse its left subtree 2. Traverse its right subtree 3. access the root node */void Postordertraversal (binarytree* bt) {if (BT) {postordertraversal (bt->lchild);
        Postordertraversal (Bt->rchild);
    printf ("%c", bt->data);
    
    }} int main (int argc, const char * argv[]) {binarytree* tree_a = createbinarytree (' A ');
    binarytree* tree_b = Createbinarytree (' B ');
    
    Tree_a->lchild = Tree_b;
    binarytree* Tree_c = Createbinarytree (' C ');
    
    Tree_a->rchild = Tree_c;
    binarytree* tree_d = Createbinarytree (' D ');
    
    Tree_b->lchild = tree_d;
    binarytree* tree_f = Createbinarytree (' F ');
    
    Tree_b->rchild = Tree_f;
    binarytree* tree_e = Createbinarytree (' E ');
    
    Tree_f->lchild = tree_e;
    binarytree* tree_g = Createbinarytree (' G ');
    
    Tree_c->lchild = Tree_g;
    binarytree* tree_i = Createbinarytree (' I ');
    
    Tree_c->rchild = tree_i;
    binarytree* tree_h = Createbinarytree (' H ');
    
    Tree_g->rchild = Tree_h; First Order traversal = A B D F E CG H I preordertraversal (tree_a);
    
    printf ("\ n");
    
    Mid-sequence traversal = D B E F A G H C I inordertraversal (tree_a);
    
    printf ("\ n");
    
    Post-post traverse = + D E F B H G I C A postordertraversal (tree_a);
return 0; }



Two or two cross-tree traversal non-recursive traversal algorithm

The basic idea of non-recursive algorithm implementation: using stack

1. When a node is encountered, press it to stack it and go through its left subtree.

2. When the left dial hand tree traversal is complete, eject the node from the top of the stack and access it.

3. Then go through the right sub-tree of the node by its right pointer.

#include <stdio.h> #include <stdlib.h> #define ElementType char typedef struct NODE {int isFirst;
    ElementType data;
    struct Node *lchild;
struct Node *rchild;

}binarytree;
    typedef struct STACKNODE {binarytree* data;
struct stacknode* next;
}stack;
    Initialize stack stack* createstack () {stack* s;
    s = (stack*) malloc (sizeof (Stack));
    if (!s) {printf ("Insufficient space \ n");
    } s->next = NULL;
return s; 
    } int IsEmpty (stack* s) {return (S->next = = NULL);}//into the stack void push (stack* s, binarytree* data) {stack* cell;
    Cell = (stack*) malloc (sizeof (Stack));
    if (!cell) {printf ("Insufficient space \ n");
    } cell->data = data;
    Cell->next = s->next;
S->next = cell;
    }//Out Stack binarytree* Pop (stack* s) {stack* FirstCell;
    Binarytree* Topdata;
        if (IsEmpty (s)) {printf ("Empty stack \ n");
    return NULL;
    } FirstCell = s->next;
    S->next = firstcell->next; Topdata = Firstcell->datA
    Free (FirstCell);
return topdata;
    }//Create two Fork tree node binarytree* createbinarytree (data) {binarytree* T = (binarytree*) malloc (sizeof (BinaryTree)); if (!t) {printf ("insufficient space.")
        \ n ");
    return NULL;
    } t->lchild = NULL;
    T->rchild = NULL;
    T->data = data;
return t;
    }//First-order traversal non-recursive traversal solution void Preordertraversal (binarytree* bt) {binarytree* T = BT; stack* s = createstack (); Create and initialize the stack s while (T | |!
            IsEmpty (s)) {while (T) {//has been left and the nodes along the way are pressed into the stack Push (s, t);
            printf ("%c", t->data);
        T = t->lchild; } if (! IsEmpty (s)) {T = pop (s);//node popup stack T = t->rchild;//Steering Right Subtree}}}//middle sequence traversal non-recursive traversal solution void
    Inordertraversal (binarytree* bt) {binarytree* T = BT; stack* s = createstack (); Create and initialize the stack s while (T | |!
            IsEmpty (s)) {while (T) {//has been left and the nodes along the way are pressed into the stack Push (s, t);
        T = t->lchild; } if (!
    IsEmpty (s)) {        T = Pop (s);
            Node Popup stack printf ("%c", t->data); T = t->rchild;
    Turn right Subtree}}}//post-traversal non-recursive traversal solution void Postordertraversal (binarytree* bt) {binarytree* T = BT; stack* s = createstack (); Create and initialize the stack s while (T | |!
            IsEmpty (s)) {while (T) {//always left and the nodes along the way are pressed into the stack t->isfirst = 1;
            Push (S, T);
        T = t->lchild; } if (! IsEmpty (s)) {T = pop (s);//node pop-up stack if (T->isfirst = = 1) {//indicates first appearance on top of stack T-&GT;ISF
                irst = 0;
                Push (S, T);
            T = t->rchild;
                } else {//second appears on top of the stack printf ("%c", t->data);
            T = NULL;
    
    }}}} int main (int argc, const char * argv[]) {binarytree* tree_a = createbinarytree (' A ');
    binarytree* tree_b = Createbinarytree (' B ');
    
    Tree_a->lchild = Tree_b;
    binarytree* Tree_c = Createbinarytree (' C '); Tree_a-> rchild = Tree_c;
    binarytree* tree_d = Createbinarytree (' D ');
    
    Tree_b->lchild = tree_d;
    binarytree* tree_f = Createbinarytree (' F ');
    
    Tree_b->rchild = Tree_f;
    binarytree* tree_e = Createbinarytree (' E ');
    
    Tree_f->lchild = tree_e;
    binarytree* tree_g = Createbinarytree (' G ');
    
    Tree_c->lchild = Tree_g;
    binarytree* tree_i = Createbinarytree (' I ');
    
    Tree_c->rchild = tree_i;
    binarytree* tree_h = Createbinarytree (' H ');
    
    Tree_g->rchild = Tree_h;
    printf ("First-order traversal non-recursive traversal algorithm:");
    
    First order traversal = = A B D F E C G H I preordertraversal (tree_a);
    printf ("\ n sequence traversal non-recursive traversal algorithm:");
    
    Mid-sequence traversal = D B E F A G H C I inordertraversal (tree_a);
    printf ("\ n post-traversal non-recursive traversal algorithm:");
    
    Post-post traverse = + D E F B H G I C A postordertraversal (tree_a);
return 0; }



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.