Data structure binary Tree--
Write function implementation: Establish two-fork tree, Middle-order recursive traversal, with the help of the stack to achieve the non-recursive traversal, the use of the queue to achieve hierarchical traversal, height, node number, leaves and the exchange of left and right subtree.
("." Indicates a null tree)
#include <stdio.h> #include <stdlib.h>//*********** two tree linked list node structure typedef char datatype;typedef struct node{ datatype data; struct node*lchild; struct node*rchild;} Bitnode,*bitree;void insert_tree (bitree *t) { char ch; ch = getchar (); if (ch == '. ') //"." Represents a null tree *T = NULL; else { *T = (bitree) malloc (sizeof ( Bitnode); (*t)->data = ch; insert_tree (& ((*t)->lchild)); insert_tree (& (*t)->rchild); }}//************* two fork tree in-order recursive traversal Void inorder (bitree root) { if (root != null) { inorder (root->lchild); printf ("%c ", root->data); inorder (root->rchild); Middle-order non-recursive traversal algorithm for }}//**************** two-fork tree (call stack operation) # define stack_size 50typedef struct //sequential stack Structure { bitree elem[stack_size]; int top; //is used to store the top element of the subscript, top 1 for empty stack}seqstack;void initstack (seqstack *s)// Sequence stack initialization { s->top = -1; //creates an empty stack}int push (seqstack *s, bitree x) //sequential stack { if (s->top == stack_size - 1) return 0; //stack is full s->top++; s->elem[s->top] = x; return 1;} Int pop (SEQSTACK&NBSP;*&NBSP;S,&NBSP;BITREE&NBSP;*X)//sequential out stack { if (s->top == -1) //stack is empty return 0; else { *x = S->elem[S->top]; s->top--; return 1; }}int isempty (seqstack *s) { return s->top == -1 ? 0 : 1;} Void inorder_2 (bitree root) //two fork tree in-sequence non-recursive traversal { bitree p; seqstack s ; initstack (&s); p = root; while (p != null| | isempty (&s) { if (p != null) { push ( &S,&NBSP;P); p = p->lchild; } else { pop (&s,&p); if (p != null) printf ("%c ", p->data); p = p->rchild; } }}//*********************** Hierarchical traversal of two-fork trees with queues #define maxsize 50typedef struct{ bitree element[maxsize]; int front; int rear;} Seqqueue;void initqueue (SEQQUEUE&NBSP;*Q)//cyclic queue initialization { q->front = 0; q->rear = 0;} Int enterqueue (seqqueue *q,bitree x) //cycle team included in the Team { if ((q->rear + 1) % maxsize == q->front) return 0; q->element[q->rear] = x; Q->rear = (q->rear + 1) &NBSP;%&NBSP;MAXSIZE;&Nbsp;return 1;} Int isempty_q (SEQQUEUE&NBSP;*Q)//Determine if empty { return (Q->rear == q->front) ? 0 : 1;} Int eeletequeue (seqqueue *q,bitree *x) //cycle Team List Team { if (Q->front == q->rear) return 0; *x = q->element[q->front]; q->front = (q->front + 1) % maxsize; return 1;} Int layer_order (bitree root) { seqqueue q; bitree p; initqueue (&Q); if (root == null) return 0; enterqueue (&q, root); while (Isempty_q (&q)) { eeletequeue (&q,&p); printf ("%c ", p->data) if (p->lchild) enterqueue (&q,p->lchild); if (P- >rchild) enterqueue (&q,p->rchild); } return 1;} Height of the binary tree (post-secondary traversal) int posttrEedepth (BITREE&NBSP;BT) { int hl, hr, max; if (bt != null) { hl = posttreedepth (Bt->lchild); hr = posttreedepth (Bt->RChild); max = hl > hr ? hl : hr; //using the three mesh operator for easy writing return (max + 1); } else return 0;} Binary tree node number Int order (bitree root) { static int ordercount = 0;//static define static variables, store in static area, program run end release, used to achieve the effect of adding "1" on the original basis to achieve statistics (consistent with the purpose of using global variables below) if (root ! = null) { ordercount++; order (root->lchild); Order (root-> Rchild); } return ordercount;} The number of leaves of a binary tree int leafcount = 0;//defines a global variable whose memory is released after the execution of the program and is used to add "1" to the original base. Achieve the effect of statistics void leaf (bitree root) //post-order traverse statistics leaf node number { //int leafcount = 0;& nbsp;if (root != null) { leaf (root->lchild); leaf (Root->RChild); if (root->lchild == null) && (root->rchild == NULL)) leafcount++; } //return leafcount;} Swap binary tree Each node of the Saozi right subtree Void exchange (bitree root) { bitree temp = NULL; if (root != null) { temp = root->LChild; root->LChild = root->RChild; root->RChild = temp; Exchange (Root->lchild); exchange (Root->rchild); }}void menu () { printf ("******* \ n "); printf (" * MENU &nbsP; *\n "); printf (" ***********************************************\n "); printf ("* 1. Input character sequence, establishing a two-fork-tree two-cross-linked list *\n"); printf ("* 2. Two" recursive traversal *\n "); printf (" * 3. Binary Tree's middle order non-recursive traversal *\n "); printf ("* 4. Two fork Tree Traversal *\n"); printf ("* 5. Finding the height of the binary tree *\n ") printf (" * 6. The number of nodes in the binary tree *\n ") printf (" * 7. Finding the number of leaves in a binary tree *\n "); printf ("* 8. Swap binary tree Each node of the Saozi right subtree *\n "); printf (" * 0. Exit *\n "); printf (" ***********************************************\n "); printf ("***********************************************\n"); printf ("Please enter select serial number:->"); Int main () { int choose = 1; bitree tt = null; while ( Choose) { menu (); scanf_s ("%d", &choose); getchar ();// To enter the input cache without affecting the binary tree input (\ n) out switch (choose) { case 1: insert_tree (&TT); break; case 2: inorder (TT); printf ("\ n"); break; case 3: inorder_2 (TT) ; printf ("\ n"); &nbSp;break; case 4: layer_order (TT); printf ("\ n"); break; case 5: printf ("Height = %d\n", PostTreeDepth (TT)); break; case 6: printf ("Number of nodes = %d \n", Order (TT)); break; case 7: leaf (TT); printf ("Number of leaves = %d\n ", leafcount); break; case 8: exchange (TT); inorder (TT); printf ("\ n"); break; default: break; } } system ("pause"); return 0;}
650) this.width=650; "title=" 1 "style=" Float:none; "alt=" Wkiom1zynovbczauaabbh6tmzuy685.png "src="/HTTP/ S3.51cto.com/wyfs02/m02/76/ad/wkiom1zynovbczauaabbh6tmzuy685.png "/>
650) this.width=650; "title=" 2 "style=" Float:none; "alt=" Wkiom1zynoydpseqaaa4oijslxe546.png "src="/HTTP/ S2.51cto.com/wyfs02/m01/76/ad/wkiom1zynoydpseqaaa4oijslxe546.png "/>
650) this.width=650; "title=" 3 "style=" Float:none; "alt=" Wkiol1zynu2gybeqaaa8vxmrbmk651.png "src="/HTTP/ S2.51cto.com/wyfs02/m02/76/ac/wkiol1zynu2gybeqaaa8vxmrbmk651.png "/>
650) this.width=650; "title=" 4 "style=" Float:none; "alt=" Wkiom1zyno3wdp2naaa2rlmk5y8571.png "src="/HTTP/ S2.51cto.com/wyfs02/m00/76/ad/wkiom1zyno3wdp2naaa2rlmk5y8571.png "/>
650) this.width=650; "title=" 5 "style=" Float:none; "alt=" Wkiom1zyno3dinoiaaaot-rzufg582.png "src="/HTTP/ S1.51cto.com/wyfs02/m01/76/ad/wkiom1zyno3dinoiaaaot-rzufg582.png "/>
650) this.width=650; "title=" 6 "style=" Float:none; "alt=" Wkiom1zyno7bcmj-aaaoiiujhea414.png "src="/HTTP/ S1.51cto.com/wyfs02/m00/76/ad/wkiom1zyno7bcmj-aaaoiiujhea414.png "/>
650) this.width=650; "title=" 7 "style=" Float:none; "alt=" Wkiol1zynu7xlqx4aaaop0jjb98011.png "src="/HTTP/ S3.51cto.com/wyfs02/m02/76/ac/wkiol1zynu7xlqx4aaaop0jjb98011.png "/>
650) this.width=650; "title=" 8 "style=" Float:none; "alt=" Wkiol1zynu_xieh9aaaoun43vma904.png "src="/HTTP/ S3.51cto.com/wyfs02/m02/76/ac/wkiol1zynu_xieh9aaaoun43vma904.png "/>
Data structure binary Tree--build two-fork tree, Middle-order recursive traversal, non-recursive traversal, and hierarchical traversal