C ++ implements the establishment of Binary Trees and four traversal methods

Source: Internet
Author: User

# Include "stdio. H"

# Include <iostream>

# Include "string. H"

# Include <stdlib. h>

Using namespace STD;

# Define max 20 // maximum number of nodes

Typedef struct node {

Char data;

Struct node * lchild, * rchild;

} Bintnode; // The Node Type of the custom Binary Tree

Typedef bintnode * bintree; // defines the binary tree pointer.

Int nodenum, leaf; // nodenum indicates the number of nodes, and leaf indicates the number of leaves.

// ============= Create a binary tree based on the first-order traversal algorithm ==================

// ====== Enter the first sequence, and add the virtual node "#" to indicate the position of the NULL pointer ========

Bintree createbintree (){

Bintree T;

Char ch;

Ch = getchar ();

If (CH = '#')

Return (null); // read #, return a null pointer
 
Else {
 
T = (bintnode *) malloc (sizeof (bintnode); // generate a node

If (! T) Exit (1 );
 
T-> DATA = CH;
 
T-> lchild = createbintree (); // construct the left subtree
 
T-> rchild = createbintree (); // construct the right subtree

Return (t );

}

} // Createbintree

// ========= NLR first-order traversal ====================

Void preorder (bintree t ){

If (t ){

Printf ("% C", T-> data); // Access Node

Preorder (t-> lchild); // first traverse the left subtree
 
Preorder (t-> rchild); // first traverse the right subtree

}

} // Preorder

// ========= LNR sequential traversal ============================

Void inorder (bintree t ){

If (t ){
 
Inorder (t-> lchild); // traverse the left subtree in the middle order
 
Printf ("% C", T-> data); // Access Node
 
Inorder (t-> rchild); // traverse the right subtree in the middle order

}

} // Inorder

// ============= LRN post-sequential traversal ====================

Void postorder (bintree t ){
 
If (t ){

Postorder (t-> lchild); // traverse the left subtree in descending order

Postorder (t-> rchild); // traverse the right subtree in descending order

Printf ("% C", T-> data); // Access Node

}

} // Postorder

// ====== Recursive algorithms used to calculate the depth, number of knots, and number of leaves of a binary tree by means of post-sequential traversal ========

Int treedepth (bintree t ){

Int HL, HR, Max;

If (t ){

Hl = treedepth (t-> lchild); // evaluate the left depth
 
HR = treedepth (t-> rchild); // evaluate the right depth

Max = HL> HR? Hl: HR; // obtain the maximum left and right depth.

Nodenum = nodenum + 1; // calculate the number of nodes
 
If (HL = 0 & HR = 0) leaf = leaf + 1; // If the left and right depths are, they are leaves.
 
Return (MAX + 1 );

}

Else return (0 );

} // Treedepth

// ==== Use the "first-in-first-out" (FIFO) queue to traverse the binary tree through layers ============

Void levelorder (bintree t ){

Int front = 0, rear = 1;

Bintnode * CQ [Max], * P; // defines the pointer array of the node CQ

CQ [0] = T; // enter the root

While (front! = Rear ){

P = CQ [Front]; // leaves the team

Front = front = Max-1? 0: (front + 1 );

Printf ("% C", p-> data); // output node Value
 
If (p-> lchild! = NULL ){

CQ [rear] = p-> lchild; // enter the left subtree

Rear = rear = Max-1? 0 :( rear + 1 );

}

If (p-> rchild! = NULL ){

CQ [rear] = p-> rchild; // enter the right subtree

Rear = rear = Max-1? 0 :( rear + 1 );
 
}

}

} // Levelorder

// =========== Main function ======================

Void main ()

{

Bintree root;

Int I, depth;

Printf ("/N ");

Printf ("creat bin_tree; input preorder:"); // the first sequence of the input Complete Binary Tree,
// Use # To represent virtual nodes, such as Abd ### ce ## F ##

Root = createbintree (); // create a binary tree and return the root node
 


Do {// select the traversal mode from the menu and enter the serial number.

Printf ("/T *********** select ************/N ");
 
Printf ("/T1: preorder traversal/N ");
 
Printf ("/T2: iorder traversal/N ");
 
Printf ("/T3: postorder traversal/N ");

Printf ("/T4: Level Depth/N"); // select the number of knots in the tree before traversing by hierarchy.

Printf ("/T0: Exit/N ");
 
Printf ("/T ******************************/N ");

Scanf ("% d", & I); // enter the menu number (-4)
Switch (I ){
 
Case 1: printf ("Print bin_tree preorder :");
 
Preorder (Root); // preorder traversal

Break;

Case 2: printf ("Print bin_tree inorder :");

Inorder (Root); // traverse in the middle order

Break;
 
Case 3: printf ("Print bin_tree postorder :");

Postorder (Root); // post-order traversal
 
Break;
 

Case 4: printf ("leveprint bin_tree :");
 
Levelorder (Root); // traverse by Hierarchy

Break;
 
Default: exit (1 );

}
 
Printf ("/N ");

} While (I! = 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.