Data structure--correlation algorithm implementation of tree

Source: Internet
Author: User

The basic algorithm of binary tree

Including two-fork tree traversal (first, middle, rear), the level of the binary tree, the depth of the binary tree, the binary tree leaf node count. The related algorithm thought can read a book, here just gives the correlation algorithm realization.

Code implementation
#include <stdio.h>#include <stdlib.h>#define MAXSIZEtypedef CharElemtype;typedef structTnode {CharData     Tnode * LCHILD; Tnode * RCHILD;} Tnode, *bitree;intIsempty_bitree (Bitree *t) {if(*t = = NULL)return 1;//If the tree is empty, it enters the dead loop until the data is entered    Else    return 0;}voidCreate_bitree (Bitree *t) {CharCh ch = getchar ();//When the "#" is entered, the subtree is considered empty    if(ch = = ' # ') *t = NULL;//Create tree nodes    Else{*t = (bitree) malloc (sizeof(structtnode)); (*t)->data = ch;//Spanning tree nodes        //Generate left subtreeCreate_bitree (& (*t)->lchild);//Generate right subtreeCreate_bitree (& (*t)->rchild); }}voidTraversebitree (Bitree T) {//First Order traversal    if(T = = NULL)//Pointer is empty, indicating that the node does not exist    return;Else{printf ("%c", T->data);        Traversebitree (T->lchild);    Traversebitree (T->rchild); }    }voidInorderbitree (Bitree T) {//Middle sequence traversal    if(NULL = = T)return;Else{Inorderbitree (t->lchild); printf"%c", T->data);       Inorderbitree (T->rchild); }}voidPostorderbitree (Bitree T) {if(NULL = = T)return;Else{Inorderbitree (t->lchild);        Inorderbitree (T->rchild); printf"%c", T->data); }    }intTreedeep (Bitree T) {intDeep =0;if(T) {intLeftdeep = Treedeep (t->lchild);intRightdeep = Treedeep (t->rchild); Deep = Leftdeep+1> Rightdeep+1? Leftdeep+1: Rightdeep+1; }returnDeep;}//Tree leaves node forintLeafcount (Bitree T,int&num) {//General address that involves change    if(T) {if(T->lchild ==null && t->rchild==null)            {num++; printf"%c", T->data);        } leafcount (T->lchild,num);    Leafcount (T->rchild,num); }returnNum;}//Tree hierarchy display (using queue, FIFO principle can be implemented perfectly)voidLevelorder_bitree (Bitree T) {//Use a queue to hold the node information, where the queue is implemented in an array of sequential queues    intFront =0;intRear =0;    Bitree Biqueue[maxsize]; Bitree Tempnode;if(! Isempty_bitree (&t)) {biqueue[rear++] = T; while(Front! = rear) {//             //Remove the team head element and move the team head pointer back oneTempnode = biqueue[front++];//Determine if the left and right subtree is empty, and if not empty, join the queue            if(! Isempty_bitree (& (Tempnode->lchild))) biqueue[rear++] = tempnode->lchild;if(!                          Isempty_bitree (& (Tempnode->rchild))) biqueue[rear++] = tempnode->rchild; printf"%c", Tempnode->data); }    }}intMainvoid) {Bitree T; Bitree *p = (bitree*) malloc (sizeof(Bitree));intdeepth,num=0; Create_bitree (&t);//General address that involves changeprintf"sequential traversal of binary trees:\ n");    Traversebitree (T); printf"\ n"); printfthe middle sequence traverses the binary tree:\ n");    Inorderbitree (T); printf"\ n"); printf"post-secondary traversal of the binary tree:\ n");    Postorderbitree (T); printf"\ nhierarchy Traversal Result: ");    Levelorder_bitree (T); printf"\ n");    Deepth=treedeep (T); printf"The depth of the tree is:%d", deepth); printf"\ n"); printf"The leaves of the tree are knot-Points:");    Leafcount (T,num); printf"\ nthe number of tree leaf nodes is:%d ", num);return 0;}
Run the Demo

Binary tree structure diagram

Reference documents
    • Data structure-in C language Description (second edition) [Shang]

Data Structure-tree-related algorithm implementation

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.