# Simple operation of binary tree

Source: Internet
Author: User

`#define  _crt_secure_no_warnings#define m 100typedef char datatype;typedef struct  node /* Two-fork linked list of structural body */{datatype data;struct node * lchild;struct node *  rchild;} bitnode, *bitree; #define  Queue_Size 100typedef BiTree QueueElement;typedef  struct //defines the structure body of the sequential queue {queueelement elem[queue_size]; //A one-dimensional array that holds the queue elements int top; // The subscript used to store the queue elements, Top=-1 represents the empty team}seqqueue; Bitree createbitree (void);//Create two-fork list void inorder (bitree root);//middle-order recursive traversal of binary tree Void inoeder (bitree   root);  //in-sequence non-recursive traversal of binary tree Int layerorder (BITREE&NBSP;BT);/* Hierarchical traversal of binary tree */int posttreedepth ( BITREE&NBSP;BT);  /* recursive algorithm of traversing binary tree to find the height */int leaf (bitree root);  /* the number of leaf nodes after the post-order traversal */int  Preorder (bitree root);/* First order the number of nodes in the binary tree */void changebit (bitree root);/* Swap the left and right subtree of each node */void  Enterqueue (seqqueue * s, queueelement x);   //order queue function Void deletequeue (Seqqueue * s)   //sequence #include <stdio.h> #include  <stdlib.h>int  Main () {int choose = 0;do{printf ("simple operation of the \t\t\t two fork tree \ n");p rintf ("Establishment of the \t\t1  two cross-tree list \ n");p rintf ("\t\ t2  two the middle sequence recursive traversal \ n ");p rintf (" \t\t3  two fork tree in order non-recursive traversal \ n ");p rintf (" \t\t4  two fork tree hierarchy traversal \ n ");p rintf (" \t\t5  The height of the binary tree \ n ");p rintf (" Number of nodes in the \t\t6  two fork ");p rintf (" Number of leaves of the \t\t7  two tree ");p rintf (" \t\t8  swap the left and right subtree of the binary tree \ n "); printf ("\t\t0  exit \ n"); scanf ("%d",  &choose);switch  (choose) {case 1:{bitree bt  = null;getchar ();//Receive a space in printf ("Use the first-order traversal to extend the input binary tree (. For the empty tree): \ n"); Bt = createbitree (); break;} Case 2:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("middle order recursive Traversal:"), Inorder (BT);p rintf ("\ n"); Case 3:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("middle order non-recursive Traversal:"), Inoeder (BT);p rintf ("\ n"); BreaK;} Case 4:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("Hierarchy traversal Result:"), Int ret = layerorder (BT);p rintf ("\ n"); Case 5:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("The height of the binary tree is%d\n",  posttreedepth (BT)); Case 6:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("Number of nodes:%d\n",  preorder (BT)); Case 7:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree ();p rintf ("Number of leaf nodes:%d\n",  leaf (BT)); Case 8:{bitree bt = null;getchar ();//Receive a space for printf ("Use the first-order traversal to extend the input binary tree (. To indicate the empty tree): \ n"); bt =  createbitree (); Changebit (BT);p rintf ("The result of the sequence traversal after the interchange is:"), Inorder (BT);p rintf ("\ n"); Case 0:{break;} Default:{break;}}}  while  (choose !=&NBSP;0); system ("pause"); return 0;} Void enterqueue (seqqueue * s, queueelement x)   //order queue function {if  (s->top  == queue_size - 1) return;s->top++;s->elem[s->top] = x;} Void deletequeue (seqqueue * s)   //sequential out of queue function {if  (s->top == -1) return; else{for  (int i = 0; i < s->top; i++) {s->elem[i] =  S-&GT;ELEM[I&NBSP;+&NBSP;1];} s->top--;}} Void vist (char p) {printf ("%c ",  p);} Bitree createbitree (void)//Create a two-fork list {char ch; BITREE&NBSP;BT&NBSP;=&NBSP;NULL;SCANF ("%c",  &ch);if  ('. ')  == ch) {bt = null;} else{bt =  (bitree) malloc (sizeof (Bitnode));bt->data = ch;bt->lchild =  Createbitree (); Bt->rchild = createbitree ();} RETURN&NBSP;BT;} Void inorder (Bitree root)//middle order recursive traversal binary tree {/*assert (root==null);*/if  (root != null) {inorder (root->lchild); Vist (Root->data); inorder (Root->rchild);}} Void inoeder (bitree  root)  //middle order non-recursive traversal binary tree {int top = 0; The bitree s[m];//stack stores a maximum of 100 elements m=100bitree p = root;do{while  (p != null) {if  (top > m) Return;top++;s[top] = p;p = p->lchild;} /* Traverse left subtree */if  (top != 0) {p = s[top];top--; Vist (P->data);/* access to the root node */p = p->rchild;} /* Traverse right subtree */} while  (p != null | |  top != 0);} Int layerorder (BITREE&NBSP;BT)/* level traverse binary tree */{//assert (BT); seqqueue * q; q =  (seqqueue*) malloc (sizeof (seqqueue)); q->top = -1; bitree p;if  (bt == null) {return -1;} Enterqueue (Q,&NBSP;BT);while  (q->top > -1) {p = q->elem[0];D eletequeue (Q); Vist (P->data);if  (p->lchild) {enterqueue (q, p->lchild);} if  (p->rchild) {Enterqueue (q, p->rchild);}} return 1;} Int posttreedepth (BITREE&NBSP;BT)  /* recursive algorithm for traversing binary tree to find the height of */{//assert (BT); int hl, hr, max;if   (NULL&NBSP;!=&NBSP;BT) {hl = posttreedepth (bt->lchild);/* Get Zuozi height */hr =  Posttreedepth (Bt->rchild);/* Get the height of the right sub-tree */max = hl > hr ? hl : hr;/* The */return  (max + 1) with large depth angle of left and right subtree was obtained. else{return 0;/* if it is an empty tree, return 0*/}}int leaf (bitree root)  /* post-order traversal statistics the number of leaf nodes */{//assert (root);  int LeafCount = 0;if  (null != root) {leaf (root->lchild); Leaf (root-> Rchild);if  ((null == root->lchild)  &&  (null == root->rchild)) {leafcount++;}} Return leafcount;} Int preorder (bitree root)/* First order to find the number of nodes in the binary tree */{//assert (root); static int bitcount = 0 ;if  (null != root) {preorder (root->lchild); Preorder (Root->rchild); bitcount++;} return  (Bitcount);} void&nbsp Changebit (bitree root)/* swaps the left and right subtree */{//assert (root) of each node; bitree tmp = null;if  (null != root) {tmp = root->lchild;root-> lchild = root->rchild;root->rchild = tmp; Changebit (Root->lchild); Changebit (Root->rchild);}}`

Simple operation of binary tree

Related Keywords:

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.