Implement various Binary Tree Traversal Algorithms

Source: Internet
Author: User

/* Exp7-2.cpp */

# Include <stdio. h>
# Include <malloc. h>
# Define Max size 100
Typedef char elemtype;
Typedef struct Node
{
Elemtype data;/* Data Elements */
Struct node * lchild;/* points to left child */
Struct node * rchild;/* points to the right child */
} Btnode;
Extern void createbtnode (btnode * & B, char * Str);/* in the algo7-1.cpp file */
Extern void dispbtnode (btnode * B );
Void preorder (btnode * B)/* recursive algorithm for first-order traversal */
{
If (B! = NULL)
{
Printf ("% C", B-> data);/* access the root node */
Preorder (B-> lchild);/* recursively access the left subtree */
Preorder (B-> rchild);/* recursively access the right subtree */
}
}

Void preorder1 (btnode * B)/* Non-recursive algorithm for first-order traversal */
{
Btnode * P;
Struct
{
Btnode * PT;
Int tag;
} St [maxsize];
Int Top =-1;
Top ++;
St [Top]. Pt = B; ST [Top]. Tag = 1;
While (top>-1)/* loop when the stack is not empty */
{
If (ST [Top]. Tag = 1)/* cannot be accessed directly */
{
P = sT [Top]. PT;
Top --;
If (P! = NULL)
{
Top ++;/* stack the right child */
St [Top]. Pt = p-> rchild;
St [Top]. Tag = 1;
Top ++;/* stack the left child */
St [Top]. Pt = p-> lchild;
St [Top]. Tag = 1;
Top ++;/* stack the root node */
St [Top]. Pt = P;
St [Top]. Tag = 0;
}
}
If (ST [Top]. Tag = 0)/* direct access */
{
Printf ("% C", St [Top]. Pt-> data );
Top --;
}

}
}

Void preorder2 (btnode * B)/* Non-recursive algorithm 2 for first-order traversal */
{
Btnode * st [maxsize], * P;
Int Top =-1;
If (B! = NULL)
{
Top ++;/* Add the root node to the stack */
St [Top] = B;
While (top>-1)/* loop when the stack is not empty */
{
P = sT [Top];/* roll back the stack and access this node */
Top --;
Printf ("% C", p-> data );
If (p-> rchild! = NULL)/* right child into Stack */
{
Top ++;
St [Top] = p-> rchild;
}
If (p-> lchild! = NULL)/* left child into Stack */
{
Top ++;
St [Top] = p-> lchild;
}
}
Printf ("\ n ");

}
}

Void inorder (btnode * B)/* recursive algorithm for sequential traversal */
{
If (B! = NULL)
{
Inorder (B-> lchild);/* recursively access the left subtree */
Printf ("% C", B-> data);/* access the root node */
Inorder (B-> rchild);/* recursively access the right subtree */
}
}

Void inorder1 (btnode * B)/* Non-recursive algorithm 1 for sequential traversal */
{
Btnode * P;
Struct
{
Btnode * PT;
Int tag;
} St [maxsize];
Int Top =-1;
Top ++;
St [Top]. Pt = B; ST [Top]. Tag = 1;
While (top>-1)/* loop when the stack is not empty */
{
If (ST [Top]. Tag = 1)/* cannot be accessed directly */
{
P = sT [Top]. PT;
Top --;
If (P! = NULL)
{
Top ++;/* stack the right child */
St [Top]. Pt = p-> rchild;
St [Top]. Tag = 1;
Top ++;/* stack the root node */
St [Top]. Pt = P;
St [Top]. Tag = 0;
Top ++;/* stack the left child */
St [Top]. Pt = p-> lchild;
St [Top]. Tag = 1;
}
If (ST [Top]. Tag = 0)/* direct access */
{
Printf ("% C", St [Top]. Pt-> data );
Top --;
}
}
}

}

Void inorder2 (btnode * B)/* Non-recursive algorithm 2 for sequential traversal */
{
Btnode * st [maxsize], * P;
Int Top =-1;
If (B! = NULL)
{
P = B;
While (top>-1 | P! = NULL)
{
While (P! = NULL)
{
Top ++;
St [Top] = P;
P = p-> lchild;
}
If (top>-1)
{
P = sT [Top];
Top --;
Printf ("% C", p-> data );
P = p-> rchild;
}
}
Printf ("\ n ");
}
}

Void postorder (btnode * B)/* recursive algorithm for post-sequential traversal */
{
If (B! = NULL)
{
Postorder (B-> lchild);/* recursively access the left subtree */
Postorder (B-> rchild);/* recursively access the right subtree */
Printf ("^ % C", B-> data);/* access the root node */
}
}

Void postorder1 (btnode * B)/* Non-recursive algorithm 1 for post-sequential traversal */
{
Btnode * P;
Struct
{
Btnode * PT;
Int tag;
} St [maxsize];
Int Top =-1;
Top ++;
St [Top]. Pt = B; ST [Top]. Tag = 1;
While (top>-1)/* loop when the stack is not empty */
{
If (ST [Top]. Tag = 1)/* cannot be accessed directly */
{
P = sT [Top]. PT;
Top --;
If (P! = NULL)
{
Top ++;
St [Top]. Pt = P;/* stack the root node */
St [Top]. Tag = 0;
Top ++;/* stack the right child */
St [Top]. Pt = p-> rchild;
St [Top]. Tag = 1;
Top ++;/* stack the left child */
St [Top]. Pt = p-> lchild;
St [Top]. Tag = 1;
}
}
If (ST [Top]. Tag = 0)/* direct access */
{
Printf ("% C", St [Top]. Pt-> data );
Top --;
}
}
}

Void postorder2 (btnode * B)/* Non-recursive algorithm 2 for post-sequential traversal */
{
Btnode * st [maxsize];
Btnode * P;
Int flag, Top =-1;/* initial values of the stack pointer */
If (B! = NULL)
{
Do
{
While (B! = NULL)/* add all the left nodes of T to the stack */
{
Top ++;
St [Top] = B;
B = B-> lchild;
}
P = NULL;/* P points to the previous accessed node of the current node */
Flag = 1;/* set the access flag of B to "accessed */
While (top! =-1 & flag)
{
B = sT [Top];/* retrieve the top element of the current stack */
If (B-> rchild = P)/* The right subtree does not exist or has been accessed */
{
Printf ("% C", B-> data);/* access * B node */
Top --;
P = B;
}
Else
{
B = B-> rchild;/* t points to the right subtree */
Flag = 0;/* set the inaccessible flag */
}
}
} While (top! =-1 );
Printf ("\ n ");
}
}

Void travlevel (btnode * B)/* hierarchical traversal */
{
Btnode * Qu [maxsize];/* defines the ordered cyclic queue */
Int front, rear;/* define the frontend and backend pointers */
Front = rear = 0;/* set the queue to an empty queue */
If (B! = NULL)
Printf ("% C", B-> data );
Rear ++;/* the node pointer enters the queue */
Qu [rear] = B;
While (rear! = Front)/* the queue is not empty */
{
Front = (front + 1) % maxsize;
B = Qu [Front];
If (B-> lchild! = NULL)/* output the left child and enter the queue */
{
Printf ("% C", B-> lchild-> data );
Rear = (Rear + 1) % maxsize;
Qu [rear] = B-> lchild;
}
If (B-> rchild! = NULL)/* output the right child to the queue */
{
Printf ("% C", B-> rchild-> data );
Rear = (Rear + 1) % maxsize;
Qu [rear] = B-> rchild;
}
}
Printf ("\ n ");
}

Void main ()
{
Btnode * B;
Createbtnode (B, "A (B (D, E (H (j, k (L, M (, n), C (f, g (, I )))");
Printf ("Binary Tree B:"); dispbtnode (B); printf ("\ n ");
Printf ("hierarchical traversal sequence :");
Travlevel (B );
Printf ("\ n ");
Printf ("first-order traversal sequence: \ n ");
Printf ("recursive algorithm:"); preorder (B); printf ("\ n ");
Printf ("non-recursive algorithm 1:"); preorder1 (B); printf ("\ n ");
Printf ("non-recursive algorithm 2:"); preorder2 (B); printf ("\ n ");
Printf ("sequential traversal sequence: \ n ");
Printf ("recursive algorithm:"); inorder (B); printf ("\ n ");
Printf ("non-recursive algorithm 1:"); inorder1 (B); printf ("\ n ");
Printf ("non-recursive algorithm 2:"); inorder2 (B); printf ("\ n ");
Printf ("post-order traversal sequence: \ n ");
Printf ("recursive algorithm:"); postorder (B); printf ("\ n ");
Printf ("non-recursive algorithm 1:"); postorder1 (B); printf ("\ n ");
Printf ("non-recursive algorithm 2:"); postorder2 (B); printf ("\ n ");

}

 

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.