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:

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 child node of the % d node:", T-> data); t-> right = creatbtree ();} return t ;}

Ii. Forward traversal of Binary Trees

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

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

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

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

int Nodenum(Node *T){if (T){return 1+Nodenum(T->left)+Nodenum(T->right);}if (T==NULL){return 0;}}

VII. Test Procedures;

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 (" the binary tree is successfully created ");} else if (choice = 2) {printf ("first traverse Binary Tree: \ n"); previsit (t);} else if (choice = 3) {printf ("middle-order traversal Binary Tree: \ n"); middvisit (t);} else if (choice = 4) {printf ("back-order traversal 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 () {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-order traversal of binary tree * \ n"); printf ("* 3 middle-order traversal of binary tree * \ n "); printf ("* 4 Post-order traversal of binary tree * \ n"); printf ("* 5 binary tree leaf knots * \ n "); printf ("* 7 all nodes of the binary tree * \ n"); printf ("* 8 Exit Program * \ n "); printf ("**************************** \ n "); printf ("Enter your choice (1, 2, 3, 4, 5, 6, 7, 8): \ n"); scanf ("% d", & choice); Return choice ;}

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.