typedef struct Node { DataType data; struct Node *leftChild; struct Node *rightChild; }BiTreeNode;//Definition of structure void Initiate(BiTreeNode **root) { *root=(BiTreeNode *)malloc(sizeof(BiTreeNode)); (*root)->leftChild=NULL; (*root)->rightChild=NULL; }//Initialization of binary tree BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x) { BiTreeNode *s, *t; if(curr==NULL) return NULL; t=curr->leftChild; s=(BiTreeNode *)malloc(sizeof(BiTreeNode)); s->data=x; s->leftChild=t; s->rightChild=NULL; curr->leftChild=s; return curr->leftChild; }//Left insertion node of binary tree BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x) { BiTreeNode *s,*t; if (curr==NULL)return NULL; t=curr->rightChild; s=(BiTreeNode *)malloc(sizeof(BiTreeNode)); s->data=x; s->rightChild=t; s->leftChild=NULL; curr->rightChild=s; return curr->rightChild; }//Right insertion node of binary tree void Destroy (BiTreeNode **root) { if ((*root)!=NULL && (*root)->leftChild!=NULL) Destroy (&(*root)->leftChild); if((*root)!=NULL && (*root)->rightChild !=NULL) Destroy (&(*root)->rightChild); free(*root); }//Undo binary tree operation BiTreeNode *DeleteLeftTree(BiTreeNode *curr) { if (curr==NULL||curr->leftChild==NULL)return NULL; Destroy(&curr->leftChild); curr->leftChild=NULL; return curr; }//Left delete node of binary tree BiTreeNode *DeleteRightTree(BiTreeNode *curr) { if(curr==NULL||curr->rightChild==NULL)return NULL; Destroy (&curr->rightChild); curr->rightChild=NULL; return curr; }//Right delete node of binary tree void PreOrder(BiTreeNode *root,void visit(DataType item)) { if(root!=NULL) { visit (root->data); PreOrder (root->leftChild,visit); PreOrder(root->rightChild,visit); } }//The root access operation of the pre order traversal binary tree is the Visit function void InOrder(BiTreeNode *root, void visit(DataType item)) { if(root!=NULL) { InOrder(root->leftChild,visit); visit(root->data); InOrder(root->rightChild,visit); } }//Middle order traversal binary tree root access operation is Visit function void PostOrder(BiTreeNode *root,void visit(DataType item)) { if(root!=NULL) { PostOrder(root->leftChild,visit); PostOrder(root->rightChild,visit); visit(root->data); } }//The root access operation of post order traversal binary tree is Visit function void PrintBiTree(BiTreeNode *root,int n) { int i; if(root==NULL) return; PrintBiTree (root->rightChild,n+1); for (i=0;i<n-1;i++) { printf(" "); } if(n>0) { printf("---"); printf("%c\n",root->data); } PrintBiTree(root->leftChild,n+1); }//Print binary tree BiTreeNode *Search(BiTreeNode *root,DataType x) { BiTreeNode *find=NULL; if(root!=NULL) { if(root->data==x) find=root; else { find=Search(root->leftChild,x); if(find==NULL) find=Search(root->rightChild,x); } } return find; }//Find data elements void Visit(DataType item) { printf("%c ",item); }//Display node information # include<stdlib.h> # include<stdio.h> typedef char DataType; #include"BiTree.h" #include"BiTreeTraverse.h" void main(void) { BiTreeNode *root, *p,*find; char x='E'; Initiate(&root); p=InsertLeftNode(root,'A'); p=InsertLeftNode(p,'B'); p=InsertLeftNode(p,'D'); p=InsertRightNode(p,'G'); p=InsertRightNode(root->leftChild,'C'); InsertLeftNode(p,'E'); InsertRightNode(p,'F'); PrintBiTree(root,0); printf("Preorder traversal:\n"); PreOrder(root->leftChild,Visit); printf("Sequential traversal:\n"); InOrder(root->leftChild,Visit); printf("Post order traversal:\n"); PostOrder(root->leftChild,Visit); find=Search(root,x); if(find!=NULL) printf("\n data elements%c In a binary tree",x); else printf("\n data elements%c Not in a binary tree",x); Destroy(&root); }
This code is the code in our book at that time
At that time, the interface running out of my byte changed BUG has been moving under my own direction. At that time, I checked many original works on the Internet, and found that there was a problem with the fake interface card. The fake interface card said that the cycle in the program was a dead cycle. At last, I found that the reason why my program running interface could appear but could not appear data was that Because print binary tree here ` void PrintBiTree(BiTreeNode *root,int n)
{
int i;
if(root==NULL) return;
PrintBiTree (root->rightChild,n+1);
for (i=0;i<n-1;i++)
{
printf(" ");
}
if(n>0)
{
printf("—");
printf("%c\n",root->data);
}
PrintBiTree(root->leftChild,n+1);
}//Print binary tree
i wrote for loop i + + as n++
When you encounter the problem of fake interface card, you should pay attention to the problem of your own circulation