Create and traverse a binary tree (forward, middle, and backward). Calculate the number of leaf nodes and the number of nodes.

Source: Internet
Author: User

Binary Tree is one of the most frequently used data structures during the test interview. It mainly includes creating a binary tree by using a program, traversing the binary tree in three order, returning the number of leaf nodes, and finding the total number of Binary Tree nodes. Create a Data Structure of a binary tree node

Typedef struct Node
{
Int data;
Struct Node * left, * right;
} Node; the structure body contains data, left subtree, and sub-tree;

1. The program code for building a binary tree is as follows:


[Cpp] Node * CreatBTree () // create a binary tree
{
Node * t;
Int x;
Scanf ("% d", & x );
If (x = 0)
{
T = NULL;
}
Else
{
T = (Node *) malloc (sizeof (Node ));
T-> data = x;
Printf ("\ n enter the left subnode of the % d node:", t-> data );
T-> left = CreatBTree ();
Printf ("\ n enter the right sub-node of the % d node:", t-> data );
T-> right = CreatBTree ();
}
Return t;
}

Node * CreatBTree () // create a binary tree
{
Node * t;
Int x;
Scanf ("% d", & x );
If (x = 0)
{
T = NULL;
}
Else
{
T = (Node *) malloc (sizeof (Node ));
T-> data = x;
Printf ("\ n enter the left subnode of the % d node:", t-> data );
T-> left = CreatBTree ();
Printf ("\ n enter the right sub-node of the % d node:", t-> data );
T-> right = CreatBTree ();
}
Return t;
}

Ii. Forward traversal of Binary Trees


[Cpp] void preVisit (Node * T)
{
If (T = NULL) return;
Else
{
Printf ("% 3d", T-> data );
PreVisit (T-> left );
PreVisit (T-> right );
}
}

Void preVisit (Node * T)
{
If (T = NULL) return;
Else
{
Printf ("% 3d", T-> data );
PreVisit (T-> left );
PreVisit (T-> right );
}
}
3. Traverse Binary Trees in a central order


[Cpp] void middVisit (Node * T)
{
If (T = NULL) return;
Else
{
MiddVisit (T-> left );
Printf ("% 3d", T-> data );
MiddVisit (T-> right );
}
}

Void middVisit (Node * T)
{
If (T = NULL) return;
Else
{
MiddVisit (T-> left );
Printf ("% 3d", T-> data );
MiddVisit (T-> right );
}
}
Iv. Post-order traversal of Binary Trees


[Cpp] void lastVisit (Node * T)
{
If (T = NULL) return;
Else
{
LastVisit (T-> left );
LastVisit (T-> right );
Printf ("% 3d", T-> data );
}
}

Void lastVisit (Node * T)
{
If (T = NULL) return;
Else
{
LastVisit (T-> left );
LastVisit (T-> right );
Printf ("% 3d", T-> data );
}
}
5. Number of returned leaf nodes


[Cpp] int leafnum (Node * T)
{
If (! T)
{
Return 0;
}
Else if ((! T-> left )&&(! T-> right ))
{
Return 1;
}
Else
{
Return (leafnum (T-> left) + leafnum (T-> right )));
}
}

Int leafnum (Node * T)
{
If (! T)
{
Return 0;
}
Else if ((! T-> left )&&(! T-> right ))
{
Return 1;
}
Else
{
Return (leafnum (T-> left) + leafnum (T-> right )));
}
}
6. Total number of returned nodes


[Cpp] view plaincopyprint? Int Nodenum (Node * T)
{
If (T)
{
Return 1 + Nodenum (T-> left) + Nodenum (T-> right );
}
If (T = NULL)
{
Return 0;
}
 
}

Int Nodenum (Node * T)
{
If (T)
{
Return 1 + Nodenum (T-> left) + Nodenum (T-> right );
}
If (T = NULL)
{
Return 0;
}

}
VII. Test Procedures; [cpp] int menu ();
Void main ()
{
Node * T = NULL;
Int choice;
Do {
Choice = menu ();
If (choice = 1)
{
Printf ("Create a binary tree, input" 0 "to end :! \ N ");
Printf ("Enter the root node: \ n ");
T = CreatBTree ();
Printf ("successful establishment of Binary Tree ");
}
Else if (choice = 2)
{
Printf ("first-order traversal of a binary tree: \ n ");
PreVisit (T );
}
Else if (choice = 3)
{
Printf ("sequential binary tree traversal: \ n ");
MiddVisit (T );
}
Else if (choice = 4)
{
Printf ("suborder traversal of Binary Tree: \ n ");
LastVisit (T );
}
Else if (choice = 5)
{
Int ct = 10;
Ct = leafnum (T );
Printf ("the number of leaf nodes in a binary tree is \ n ");
Printf ("% d \ n", ct );

}
Else if (choice = 7)
{
Int count = Nodenum (T );
Printf ("this binary tree has a total of % d nodes. \ N ", count );
}
Else if (choice = 8)
Exit (0 );
} While (choice <= 8 );
}

Int menu ();
Void main ()
{
Node * T = NULL;
Int choice;
Do {
Choice = menu ();
If (choice = 1)
{
Printf ("Create a binary tree, input" 0 "to end :! \ N ");
Printf ("Enter the root node: \ n ");
T = CreatBTree ();
Printf ("successful establishment of Binary Tree ");
}
Else if (choice = 2)
{
Printf ("first-order traversal of a binary tree: \ n ");
PreVisit (T );
}
Else if (choice = 3)
{
Printf ("sequential binary tree traversal: \ n ");
MiddVisit (T );
}
Else if (choice = 4)
{
Printf ("suborder traversal of Binary Tree: \ n ");
LastVisit (T );
}
Else if (choice = 5)
{
Int ct = 10;
Ct = leafnum (T );
Printf ("the number of leaf nodes in a binary tree is \ n ");
Printf ("% d \ n", ct );

}
Else if (choice = 7)
{
Int count = Nodenum (T );
Printf ("this binary tree has a total of % d nodes. \ N ", count );
}
Else if (choice = 8)
Exit (0 );
} While (choice <= 8 );
}
[Cpp] int menu ()
{
Int choice;
Printf ("\ n ");
Printf ("binary tree \ n ");
Printf ("*************************** \ n ");
Printf ("** \ n ");
Printf ("* Main Menu * \ n ");
Printf ("* 1 create a binary tree * \ n ");
Printf ("* 2 first traverse binary tree * \ n ");
Printf ("* 3 traversing a binary tree in ascending order * \ n ");
Printf ("* 4 suborder traversal of binary tree * \ n ");
Printf ("* 5 binary tree leaf knots * \ n ");
Printf ("* 7 all nodes of a binary tree * \ n ");
Printf ("* 8 Exit Program * \ n ");
Printf ("**************************** \ n ");
Printf ("Enter your options (1, 2, 3, 4, 5, 6, 7, 8): \ n ");
Scanf ("% d", & choice );
Return choice;
}

Int menu ()
{
Int choice;
Printf ("\ n ");
Printf ("binary tree \ n ");
Printf ("*************************** \ n ");
Printf ("** \ n ");
Printf ("* Main Menu * \ n ");
Printf ("* 1 create a binary tree * \ n ");
Printf ("* 2 first traverse binary tree * \ n ");
Printf ("* 3 traversing a binary tree in ascending order * \ n ");
Printf ("* 4 suborder traversal of binary tree * \ n ");
Printf ("* 5 binary tree leaf knots * \ n ");
Printf ("* 7 all nodes of a binary tree * \ n ");
Printf ("* 8 Exit Program * \ n ");
Printf ("**************************** \ n ");
Printf ("Enter your options (1, 2, 3, 4, 5, 6, 7, 8): \ n ");
Scanf ("% d", & choice );
Return choice;
}


 

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.