Creation and traversal of a two-prong tree (non-recursive traversal left and right, destruction of tree structure)

Source: Internet
Author: User
Tags assert int size

Binary tree creation and traversal (non-recursive traversal left and right, destroy tree structure) create a recursive 3-way traversal of a two-fork tree:

1, first center, then left tree, then right tree
2, first left tree, then center, then right tree
3, first left tree, then right tree, again center

Binary Tree non-recursive 4 kinds of traversal mode:

1, first center, then left tree, then right tree
2, first left tree, then center, then right tree
3, first left tree, then right tree, again center

4, Hierarchical traversal

Binary tree Search, to find the height, the number, to find the parent node, copy the binary tree, release two fork tree

Compile the method, compile with GCC, compile with g++, command as follows:
g++-G nodestack.c nodequeue.c bintree.c bintreemain.c

Bintree.h

#ifndef __bintree__#define __bintree__#include <stdio.h> #include <malloc.h> #include <assert.h>#  Define Elemtype chartypedef struct bintreenode{elemtype data;  struct bintreenode* leftchild; struct bintreenode* rightchild;}  bintreenode;typedef struct bintree{bintreenode* root; Elemtype ref;} Bintree;void Init (bintree* tr, elemtype val), void Createbintree (bintree* bt), void Createbintree_str (bintree* bt, char** STR);//First Center, then left tree, then right tree void Show_clr (bintree* tr);//First left tree, then center, then right tree void SHOW_LCR (bintree* tr);//First Left tree, then right tree, then center void SHOW_LRC ( bintree* tr);//Level traversal void Show_level (bintree* tr);//Two fork Tree lookup method 1int get_size1 (bintree* tr);//Two fork Tree lookup method 2int get_size2 ( bintree* tr);//The height of the binary tree int get_height (bintree* tr);//Find binary Tree bintreenode* search (bintree* tr, elemtype key);// The parent node of a node is bintreenode* get_parent (bintree* tr, bintreenode* p);//Whether the tree is an empty bool Isbintreeempty (bintree* TR);//Copy Lesson two fork tree void copy (bintree* TR1, bintree* tr2);//release two fork tree void Bintree_clear (bintree* tr);//First Center, then left tree, then right tree void Display_clr (bintree* tr);// First left tree, thenCenter, then right tree void DISPLAY_LCR (bintree* tr);//First Left tree, then right tree, then center void DISPLAY_LRC (bintree* tr);//First Left tree, then right tree, then center void Display_lrc1 ( bintree* tr); #endif

Bintree.c

#include "bintree.h" #include "nodequeue.h" #include "nodestack.h" void Init (bintree* tr, elemtype val) {tr->root = NULL  ; Tr->ref = val;}  void Createroot (bintree* bt, bintreenode** t) {Elemtype item;  scanf ("%c", &item);  if (item = = bt->ref) {*t = NULL;    } else{*t = (bintreenode*) malloc (sizeof (Bintreenode));    ASSERT (*t! = NULL);    (*t)->data = Item;    Createroot (BT, & (*t)->leftchild);  Createroot (BT, & (*t)->rightchild); }}void Createbintree (bintree* bt) {Createroot (BT, & (Bt->root));}  void Createnode_str (bintree* bt, bintreenode** T, char** str) {if (**str = = ')} {return;    } if (**str = = bt->ref) {*t = NULL;  *STR = *str + 1;    } else{*t = (bintreenode*) malloc (sizeof (Bintreenode));    (*t)->data = **str;    *STR = *str + 1;    Createnode_str (BT, & (*t)->leftchild), str);  Createnode_str (BT, & (*t)->rightchild), str); }}void Createbintree_str (bintree* bt, char** str) {createnode_str (BT, & (BT->root), str);}  First center, then left tree, then right tree void Show_node_clr (bintreenode* n) {if (NULL = = n) return;    else{printf ("%c", n->data);    SHOW_NODE_CLR (N->leftchild);  SHOW_NODE_CLR (N->rightchild); }}//First Center, then left tree, then right tree void Show_clr (bintree* tr) {show_node_clr (tr->root);}  First left tree, then center, then right tree void Show_node_lcr (bintreenode* n) {if (NULL = = n) return;    else{SHOW_NODE_LCR (N->leftchild);        printf ("%c", n->data);  SHOW_NODE_LCR (N->rightchild); }}//first left tree, then center, then right tree void SHOW_LCR (bintree* tr) {SHOW_NODE_LCR (tr->root);}  First left tree, then right tree, then center void SHOW_NODE_LRC (bintreenode* n) {if (NULL = = n) return;    else{SHOW_NODE_LRC (N->leftchild);    SHOW_NODE_LRC (N->rightchild);  printf ("%c", n->data); }}//first left tree, then right tree, then center void SHOW_LRC (bintree* tr) {SHOW_NODE_LRC (tr->root);}  Non-recursive level traversal//utilization queue, FIFO void Show_node_level (bintreenode* n) {if (NULL = = n) return;  Nodequeue queue;  Init_queue (&queue);  EnQueue (&queue, N);  bintreenode* tmp; while (!isqueueempty (&queue)) {if (GetHead (&Queue) = = NULL) break;    TMP = GetHead (&queue)->data;    DeQueue (&queue);    printf ("%c", tmp->data);    if (tmp->leftchild! = NULL) enQueue (&queue, tmp->leftchild);  if (tmp->rightchild! = NULL) enQueue (&queue, tmp->rightchild); } printf ("\ n");} Hierarchy traversal void Show_level (bintree* tr) {show_node_level (tr->root);} --------------------Dividing line--------------------//Get the number of nodes in the tree void Get_node_size1 (bintreenode* N, int* p) {if (NULL = = N)  Return    else{*p = *p + 1;    Get_node_size1 (N->leftchild, p);  Get_node_size1 (N->rightchild, p);  }}//gets the number of nodes in the tree int get_size1 (bintree* tr) {int size = 0;  Get_node_size1 (Tr->root, &size); return size;}  int get_node_size2 (bintreenode* p) {if (NULL = = p) return 0;  else{return Get_node_size2 (p->leftchild) + get_node_size2 (p->rightchild) + 1; }}int Get_size2 (bintree* tr) {return get_node_size2 (tr->root);} respectively, the height of the tree, which is high, which is the height of the tree void Get_node_height (bintreenode* N, int* p) {if (NULL = = N) reTurn  *p = *p + 1;  int t1 = 0, t2 = 0;  Get_node_height (N->leftchild, &AMP;T1);  Get_node_height (N-&GT;RIGHTCHILD,&AMP;T2);  if (T1 > t2) *p = *p + T1; else *p = *p + t2;}  int Get_height (bintree* tr) {int size = 0;  if (tr->root! = NULL) size++;  int t1 = 0, t2 = 0;  Get_node_height (Tr->root->leftchild, &AMP;T1);  Get_node_height (Tr->root->rightchild, &AMP;T2); return t1 > T2? T1 + size:t2 + size;}     Recursive lookup node, if found on the left node, do not need to find the right node bintreenode* Search_node (bintreenode* N, elemtype key) {if (NULL = = N | | n->data = key) {  return n;    }else{bintreenode* tmp;    TMP = Search_node (N->leftchild, key);    if (NULL = = tmp) {tmp = Search_node (N->rightchild, key);  } return TMP;  }}bintreenode* Search (bintree* tr, elemtype key) {bintreenode* n = NULL;  n = Search_node (tr->root, key); return n;} Recursively finds the parent node, and if the left node of the parent node is already the target node, then it is no longer necessary to judge the right node of the parent node. bintreenode* Get_node_parent (bintreenode* N, bintreenode* target) {if (NULL = = N | | NULL = = target) rEturn NULL;  if (N->leftchild = = Target | | n->rightchild = = target) {return n;    } else{bintreenode* tmp = NULL;    TMP = Get_node_parent (n->leftchild, target);    if (NULL = = tmp) {tmp = Get_node_parent (n->rightchild, target);  } return TMP; }}bintreenode* get_parent (bintree* tr, bintreenode* target) {get_node_parent (tr->root, target);} BOOL Isbintreeempty (bintree* tr) {return NULL = = Tr->root;}    void Copy_node (bintreenode** N1, bintreenode* n2) {if (null = = N2) {*n1 = null;  Return    }else{bintreenode* p = (bintreenode*) malloc (sizeof (bintreenode*));    P->data = n2->data;    *N1 = p;    Copy_node (& (*N1)->leftchild), n2->leftchild);  Copy_node (& (*N1)->rightchild), n2->rightchild); }}void copy (bintree* TR1, bintree* tr2) {Copy_node (& (Tr1->root), tr2->root);}  void Bintree_node_clear (bintreenode** n) {if (NULL = = *n) return;  Bintree_node_clear (& (*n)->leftchild); Bintree_node_clear (& (*n)Rightchild));  Free (*n); *n = NULL;}  void Bintree_clear (bintree* tr) {bintree_node_clear (& (Tr->root));  Init (tr, ' # ');    }//non-recursive first center, then left tree, then right tree//use stack, advanced out void Display_node_clr (bintreenode* n) {if (null = = N) {printf ("is empty tree \ n");  Return  } Nodestack Stack;  Init (&stack);  Push (&stack, N);  node* tmp = NULL;    while (0! = stack.size) {tmp = pop (&stack);    if (NULL = = tmp) continue;    printf ("%c", tmp->data->data);    if (tmp->data->rightchild! = NULL) push (&stack, tmp->data->rightchild);  if (tmp->data->leftchild! = NULL) push (&stack, tmp->data->leftchild); }}//non-recursive first center, then left tree, then right tree void Display_clr (bintree* tr) {display_node_clr (tr->root);}    Non-recursive first left tree, re center, then right tree//use stack, advanced out void Display_node_lcr (bintreenode* n) {if (null = = N) {printf ("is empty tree \ n");  Return  } Nodestack Stack;  Init (&stack);  Push (&stack, N);  node* tmp = NULL;    while (0! = stack.size) {tmp = pop (&stack); if (NULL! = TMP-&GT;DATA-&GT;LEFTCHild) {if (NULL! = tmp->data->rightchild) {push (&stack, tmp->data->rightchild);      }//Center node push (&stack, tmp->data);    Push (&stack, tmp->data->leftchild);      } else{//Then pop out the center node if (NULL = = tmp->data->rightchild) {printf ("%c", tmp->data->data);    The pop comes out of TMP as the center node TMP = POP (&stack);    if (NULL = = tmp) continue;      printf ("%c", tmp->data->data);    } else{printf ("%c", tmp->data->data);      Push (&stack, tmp->data->rightchild); }}}}//non-recursive first left tree, then center, then right tree void DISPLAY_LCR (bintree* tr) {DISPLAY_NODE_LCR (tr->root);}    Non-recursive first left tree, then right tree, again center (although implemented traversal, but destroys the structure of the tree) void Display_node_lrc (bintreenode* n) {if (null = = N) {printf ("is empty tree \ \");  Return  } Nodestack Stack;  Init (&stack);  Push (&stack, N);  node* tmp = NULL;    while (0! = stack.size) {tmp = pop (&stack);    if (NULL! = tmp->data->leftchild) {//Center node push (&stack, tmp->data);  if (NULL! = tmp->data->rightchild) {push (&stack, tmp->data->rightchild);      } push (&stack, tmp->data->leftchild);    Set the pointer of the left and right node of the center node to null tmp->data->rightchild = Tmp->data->leftchild = null;      } else{if (NULL = = tmp->data->rightchild) {printf ("%c", tmp->data->data);    } else{//center node push (&stack, tmp->data);    Push (&stack, tmp->data->rightchild);      Set the pointer of the left and right node of the center node to null tmp->data->rightchild = Tmp->data->leftchild = null; }}}}//non-recursive first left tree, then right tree, then center (although implemented traversal, but destroyed the structure of the tree) void DISPLAY_LRC (bintree* tr) {DISPLAY_NODE_LRC (tr->root);}

Bintreemain.c

#include "bintree.h"int main(){  BinTree tr;  init(&tr, ‘#‘);  //ABC##DE##F##G##H##                                                            //createBinTree(&tr);                                                           char* a = "ABC##DE##F##G#H##";  //char* a = "AB##C##";                                                          BinTree tr1;  init(&tr1, ‘#‘);  createBinTree_str(&tr1, &a);  show_clr(&tr1);  printf("\n");  show_lcr(&tr1);  printf("\n");  show_lrc(&tr1);  printf("\n");  show_level(&tr1);  //非递归遍历,中 左 右  display_clr(&tr1);  printf("\n");  //非递归遍历,左 中 右  display_lcr(&tr1);  printf("\n");  //非递归遍历,左 右 中  display_lrc(&tr1);  printf("\n");      return 0;}

Nodequeue.h

#ifndef __NODEQUEUE__#define __NODEQUEUE__#include <stdio.h>#include <malloc.h>#include <assert.h>#include <memory.h>#include <stdbool.h>struct BinTreeNode;#define ElemType1 BinTreeNode*typedef struct Node{  ElemType1 data;  struct Node* next;}Node;typedef struct NodeQueue{  Node*  front;  Node*  tail;  size_t size;}NodeQueue;void init_queue(NodeQueue*);void enQueue(NodeQueue*, ElemType1);void deQueue(NodeQueue*);void show_list(NodeQueue*);int length(NodeQueue*);void clear(NodeQueue*);void destroy(NodeQueue*);Node* getHead(NodeQueue*);bool isQueueEmpty(NodeQueue*);#endif

Nodequeue.c

#include "nodequeue.h" void Init_queue (nodequeue* queue) {Queue->front = Queue->tail = (node*) malloc (sizeof (Node)  );  Queue->tail->next = NULL; queue->size = 0;}   Queue (end plug) void EnQueue (nodequeue* queue, ElemType1 val) {  node* p = (node*) malloc (sizeof (Node));  P->data = val;  if (Queue->front->next = = NULL) {queue->front->next = P;  } else{Queue->tail->next = p;  } queue->tail = P;  P->next = NULL; queue->size++;} Out of the team (header delete) void DeQueue (nodequeue* queue) {if (queue->  Size = = 0) return;  node* tmp = queue->front->next;  Queue->front->next = queue->front->next->next;  Free (TMP); queue->size--;} NT Length (nodequeue* queue) {return queue->size;}  void Show_list (nodequeue* queue) {node* p = queue->front;    while (p->next! = NULL) {printf ("%d\n", p->next->data); p = P>next;  }}void Clear (nodequeue* queue) {if (queue->size = = 0) return;  node* p = queue->front;  node* tmp;    while (p->next! = NULL) {tmp = p->next;    p = p->next;  Free (TMP);  } Queue->tail = queue->front;  Queue->tail->next = NULL; queue->size = 0;}  void Destroy (nodequeue* queue) {clear (queue); Free (queue->front);} node* gethead (nodequeue* queue) {return queue->front->next;} BOOL Isqueueempty (nodequeue* queue) {return Queue->front = = Queue->tail;}

Nodestack.h

#ifndef __NODESTACK__#define __NODESTACK__#include <stdio.h>#include <malloc.h>#include <assert.h>#include <memory.h>#include <stdbool.h>struct BinTreeNode;typedef BinTreeNode* ElemType2;typedef struct Node{  ElemType2 data;  struct Node *next;}Node;typedef struct nodestack{  int size;  Node *base;  Node *top;}nodestack;void init(nodestack*);void push(nodestack*, ElemType2);void show_list(nodestack*);Node* pop(nodestack*);int length(nodestack*);void clear(nodestack*);void destroy(nodestack*);#endif

Nodestack.c

#include "nodestack.h" node* CreateNode (ElemType2 val) {node* n = (node*) malloc (sizeof (Node));  N->data = val;  N->next = NULL; return n;}  void init (nodestack* stack) {stack->size = 0;  Stack->top = NULL; Stack->base = NULL;}  void push (nodestack* stack, ElemType2 x) {node* n = createnode (x);    When the stack is empty if (stack->base = = NULL) {stack->top = Stack->base = N;    N->next = NULL;    stack->size++;  Return  } N->next = stack->top;  Stack->top = n; stack->size++;}  void Show_list (nodestack* stack) {node* top = stack->top;    while (top! = NULL) {printf ("%d\n", top->data);  top = top->next;  }}node* pop (nodestack* stack) {if (stack->size = = 0) return NULL;  node* n = stack->top;  Stack->top = stack->top->next;  stack->size--; return n;} int length (nodestack* stack) {return stack->size;}  void Clear (nodestack* stack) {node* top = stack->top;  node* tmp;    while (top! = NULL) {tmp = top; top = top->next;    Free (TMP);  } stack->top = Stack->base = NULL; stack->size = 0;} void Destroy (nodestack* stack) {clear (stack);}

Creation and traversal of a two fork tree (non-recursive traversal left and right, destruction of tree structure)

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.