Chained storage for binary trees

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE typedef char ELEMTYPE  ;      typedef struct NODE {elemtype data;      struct Node *lchild;  struct Node *rchild;  }*bittree,bitnode; /* Two fork Tree basic operation */void Initbittree (Bittree *t);//two fork Tree initialization operation void Destroybittree (Bittree *t);//destruction of binary tree void Createbittree (bittr EE *t);//recursive creation of two-fork-tree void CreateBitTree2 (Bittree *t,char str[]);//non-recursive creation of two-fork-tree int insertleftchild (bittree p,bittree c);//Two fork-tree left Insert operation//If the binary tree C is present and not empty, then C is inserted into the left subtree pointed to by P, so that the left subtree of the node of P refers to the right subtree int insertrightchild (Bittree p,bittree c) of C;//two fork tree right insert action// If the binary tree C exists and is not empty, then C is inserted into the right subtree pointed to by P, so that the right subtree of the node of P refers to the right subtree of C Bittree Point (Bittree t,elemtype e);//returns the binary tree node pointer operation Elemtype Leftchild ( Bittree t,elemtype e);//Return the node of the binary tree to the left child element value Operation Elemtype Rightchild (Bittree t,elemtype e);//Return Binary tree node right child element value operation int Deleteleftchild (Bittree p);//two fork tree left delete operation int deleterightchild (Bittree p);//two fork tree right delete operation void Preordertraverse (Bittree T); /sequential Traversal binary tree recursive implementation of void Inordertraverse (Bittree T);//recursion of binary tree in sequential traversalnow void Postordertraverse (Bittree t);//The recursive implementation of the sequential traversal of the binary tree is void PostOrderTraverse2 (Bittree t);//non-recursive implementation of sequential traversal of binary tree void Preordertra Verse2 (Bittree t);//non-recursive implementation of the first-order traversal binary tree void InOrderTraverse2 (Bittree t);//non-recursive implementation of middle sequence traversal binary tree #include "LinkBiTree.h" void Initbi  Ttree (Bittree *t)//Two fork tree initialization operation {*t = NULL; } void Destroybittree (Bittree *t)//Destroy binary tree {if (*t) {if ((*t)->lchild) {Destr          Oybittree (& (*t)->lchild);          } if ((*t)->rchild) {Destroybittree (& ((*t)->rchild));          } free (*t);      *t = NULL;      }} void Createbittree (Bittree *t)//recursive Create two fork tree {elemtype ch;      scanf ("%c", &ch);      if (ch = = ' # ') {*t = NULL;          } else {*t = (bittree) malloc (sizeof (Bitnode)); if (! (          *t)) {exit (-1);              } else {(*t)->data = ch;         Createbittree (& (*t)->lchild);     Createbittree (& (*t)->rchild);      }}}} void CreateBitTree2 (Bittree *t,char str[])//recursively create two fork tree {char ch;      Bittree Stack[maxsize];      int top =-1;      int flag,k;      Bitnode *p;      *t = null,k = 0;      ch = str[k];                  while (ch! = ') ' {switch (ch) {case ' (': stack[++top] = p;                  flag = 1;          Break              Case ') ': top--;          Break              Case ', ': flag = 2;          Break              Default:p = (bittree) malloc (sizeof (Bitnode));              P->data = ch;              P->lchild = NULL;              P->rchild = NULL;              if (*t = = NULL) {*t = P;                      } else {switch (flag) {Case 1:                      Stack[top]->lchild = p;                  Break Case 2: Stack[top]->rchild = p;                  Break      }}} ch = str[++k];          }} int Insertleftchild (Bittree p,bittree C)//two fork tree left insert operation {if (p) {c->rchild = p->lchild;          P->lchild = C;      return 1;  } return 0;          } int Insertrightchild (Bittree p,bittree C)//two fork tree right insert operation {if (p) {c->rchild = P->rchild;          P->rchild = C;      return 1;  } return 0;      } bittree Point (Bittree t,elemtype e)//Returns the binary tree node pointer operation {Bittree q[maxsize];      int front = 0,rear = 0;      Bitnode *p;          if (t) {q[rear] = T;          rear++;              while (front! = rear) {p = Q[front];              front++;              if (P->data = = e) {return p;                  } if (P->lchild) {q[rear] = P->lchild;  rear++;            } if (P->rchild) {q[rear] = P->rchild;              rear++;  }}} return NULL;      } elemtype leftchild (Bittree t,elemtype E)//The Left child element value operation that returns the node of the binary tree {Bittree p;          if (T) {p = point (T,e);          if (P && p->lchild) {return p->lchild->data;  }} exit (-1);      } elemtype rightchild (Bittree t,elemtype e)//Return binary tree node right child element value operation {Bittree p;          if (T) {p = point (T,e);          if (P && p->rchild) {return p->rchild->data;  }} exit (-1);          } int Deleteleftchild (Bittree p)//two fork tree left delete operation {if (p) {Destroybittree (& (P->lchild));      return 1;  } return 0;          } int Deleterightchild (Bittree p)//two fork tree right delete operation {if (p) {Destroybittree (& (P->rchild));      return 1;  } return 0; } voidPreordertraverse (Bittree t)//recursive implementation of the binary tree recursively {if (t) {printf ("%2c", t->data);          Preordertraverse (T->lchild);      Preordertraverse (T->rchild);          }} void Inordertraverse (Bittree t)//recursive implementation of the middle sequence traversal binary tree {if (t) {inordertraverse (t->lchild);          printf ("%2c", t->data);      Inordertraverse (T->rchild);          }} void Postordertraverse (Bittree t)//post-traversal binary tree recursive implementation {if (t) {postordertraverse (t->lchild);          Postordertraverse (T->rchild);      printf ("%2c", t->data); }} void PreOrderTraverse2 (Bittree T)//post-traversal binary tree non-recursive implementation {Bittree stack[maxsize];//defines a stack for storing the node pointer int top;//Defining the top of the stack      Pointer Bitnode *p;//defines a node pointer top = 0;//initialization stack p = T; while (P! = NULL | | Top > 0) {while (P! = NULL)//If the stack is not empty to access the root node, traverse the left subtree {printf ("%2c", p-&         Gt;data);//access root node stack[top++] = p;//p into the stack p = p->lchild;//traverse left subtree} if (Top > 0)//If stack is not empty {p = stack[--top];//stack top element out of stack p = p->rchild;//Traverse right subtree} }} void InOrderTraverse2 (Bittree T)//non-recursive implementation of the first-order traversal binary Tree {Bittree stack[maxsize];//defines a stack for storing the node pointer int top;/      /define stack top pointer bitnode *p;//define node pointer top = 0;//initialization stack p = T; while (P! = NULL | | Top > 0) {while (P! = NULL)//If the stack is not empty to access the root node, traverse the left subtree {stack[top++] = P ;//P into the stack p = p->lchild;//traverse left subtree} if (Top > 0)//If stack is not empty {p = stack   [--top];//stack top element out of stack printf ("%2c", p->data);//access root node p = p->rchild;//Traverse Right Subtree}}}      void PostOrderTraverse2 (Bittree T)//non-recursive implementation of the middle sequence traversal binary tree {bittree stack[maxsize];//define a stack for holding the node pointer int top;//define stack top pointer      Bitnode *p,*q;//defines a node pointer top = 0;//initialization stack p = T;      Q = NULL; while (P! = NULL | | Top > 0) {while (P! = NULL)//If the stack is not empty to access the root node, traverse the left subtree {stack[top++] = p;//p into the stack p = p->lchild;//traverse left subtree} if (Top > 0)//If stack is not empty {p = stack[top-1];//stack top element out of stack if (p->rchild = = NULL | | p->rchild = = q) {printf ("                  %2c ", p->data);//access root node q = p;                  p = NULL;              top--;   } else {p = p->rchild;//Traverse Right Subtree}}}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Chained storage for binary trees

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.