我只是问一个问题,请大神们和版主帮个忙——————二叉树的实现
关于二叉树的实现,结点的插入和删除,还有结点遍历输出,运行时程序会出现错误,求调试大神帮忙!!!已知会产生的错误时遍历输出时不按照预想,超过两个结点会崩溃,
删除结点时会崩溃,
/*头文件 tree.h */
程序代码:
#ifndef _TREE_H_ #define _TREE_H_ #include <stdbool.h> #define SIZE 15 #define MAXSIZE 15 typedef struct item { char name[SIZE]; char sex[SIZE]; int age; } Item; typedef struct node { Item item; struct node * left; struct node * right; } Node; typedef struct tree { Node *root; unsigned int size; } Tree; typedef struct pair { Node * parent; Node * child; }Pair; void InitializeTree(Tree *ptree); bool TreeIsEmpty(const Tree *ptree); bool TreeIsFull(const Tree *ptree); unsigned int TreeItemCount(const Tree*ptree); bool AddItem(const Item *pitem,Tree *ptree); bool InTree(const Item *pitem,const Tree *ptree); bool DeleteItem(const Item *pitem,Tree *ptree); void Traverse(const Tree *ptree,void(*pun)(Item item)); void DeleteAll(Tree *ptree); #endif // _TREE_H_
/*实现头文件功能函数 tree.c*/
程序代码:
#include <string.h> #include <stdio.h> #include <stdlib.h> #include "tree.h" static Node * MakeNode(const Item *pitem); static bool ToLeft(const Item *pitem1,const Item *pitem2); static bool ToRight(const Item *pitem1,const Item *pitem2); static void AddNode(Node * new_node,Node *root); static void TraverseTree(const Node *root,void (*pfun)(Item item)); static Pair SeekItem(const Item *pitem,const Tree *ptree); static void DeleteNode(Node **pt); static void DeleteAllNode(Node *root); void InitializeTree(Tree *ptree) { ptree->root=NULL; ptree->size=0; } bool TreeIsEmpty(const Tree *ptree) { if(ptree->root==NULL) return true; else return false; } bool TreeIsFull(const Tree *ptree) { if(ptree->size==MAXSIZE) return true; else return false; } unsigned int TreeItemCount(const Tree *ptree) { return ptree->size; } bool AddItem(const Item *pitem,Tree *ptree) { Node *new_node; if(SeekItem(pitem,ptree).child!=NULL) { printf("the item is already exist."); return false; } new_node=MakeNode(pitem); if(new_node==NULL) { printf("FAIED TO CREAT A NODE!"); return false; } ptree->size++; if(ptree->root==NULL) ptree->root=new_node; else AddNode(new_node,ptree->root); } bool InTree(const Item *pitem,const Tree *ptree) { return(SeekItem(pitem,ptree).child==NULL?false:true); } bool DeleteItem(const Item *pitem,Tree *ptree) { Pair temp; temp=SeekItem(pitem,ptree); if(temp.child==NULL) return false; else if(temp.parent==NULL) DeleteNode(&ptree->root); else DeleteNode(&temp.child); //is there a difference between left node and right node; ptree->size--; return true; } void Traverse(const Tree *ptree,void(*pfun)(Item item)) { if(ptree!=NULL) TraverseTree(ptree->root,pfun); } void DeleteAll(Tree *ptree) { if(ptree->root!=NULL) DeleteAllNode(ptree->root); ptree->root=NULL; ptree->size=0; } static Node *MakeNode(const Item *pitem) { Node * new_node; new_node=(Node *)malloc(sizeof(Node)); if(new_node!=NULL) { new_node->item=*pitem; new_node->left=NULL; new_node->right=NULL; } return new_node; } static bool ToLeft(const Item *pitem1,const Item *pitem2) { int flag; if(flag=strcmp(pitem1->name,pitem2->name)<0) return true; else return false; } static bool ToRight (const Item *pitem1,const Item*pitem2) { int flag; if(flag=strcmp(pitem1->name,pitem2->name)<0) return true; else return false; } static void AddNode(Node *new_node,Node *root) { if(ToLeft(&new_node->item,&(root->item))) { if(root->left==NULL) root->left=&new_node; else AddNode(new_node,root->left); } else if(ToRight(&new_node->item,&(root->item))) if(root->right==NULL) root->right=&new_node; else AddNode(new_node,root->right); } static void TraverseTree(const Node *root,void (*pfun)(Item item)) { if(root!=NULL) { TraverseTree(root->left,pfun); (*pfun)(root->item); TraverseTree(root->right,pfun); } else printf("no member!"); } static Pair SeekItem(const Item *pitem,const Tree *ptree) { Pair temp; temp.parent=NULL; temp.child=ptree->root; while(temp.child!=NULL) { if(ToLeft(pitem,&temp.child->item)) { temp.parent=temp.child; temp.child=temp.child->left; } else if (ToRight(pitem,&temp.child->item)) { temp.parent=temp.child; temp.child=temp.child->right; } else break; } return temp; } static void DeleteNode(Node **pt) { Node * temp; if((*pt)->left=NULL) { temp=*pt; *pt=(*pt)->right; free(temp); } else if ((*pt)->right=NULL) { temp=*pt; *pt=(*pt)->left; free(temp); } else { for(temp=(*pt)->left;temp->right==NULL;temp=temp->right) break; temp->right=(*pt)->right; temp=*pct; (*pt)=(*pt)->left; free(temp); } } static void DeleteAllNode(Node * root) { Node *pright; if (root!=NULL) { pright=root->right; DeleteAllNode(root->left); free(root); DeleteAllNode(pright); } }
/*主函数 MY_TREE.c */
程序代码:
#include <stdio.h> #include <string.h> #include <stdbool.h> #include "tree.h" enum allchoices {addone,deleteone,itemcount,search,showall,cleanall}; const char * choices[]={"addone","deleteone","itemcount","search","showall","cleanall"}; #define LEN 15 void menu(void); void AddOne (Tree *ptree); void DeleteOne(Tree *ptree); void ItemCount(const Tree *ptree); void FindOne(const Tree *ptree); void ShowAll(const Tree *ptree); void CleanAll(Tree *ptree); void PrintItem(Item item); int main(void) { Tree classmates; char yourchoice[LEN]; enum allchoices choice; bool the_order_iseffective=false; InitializeTree(&classmates); menu(); while(strcmp(gets(yourchoice),"quit")!=0&&yourchoice[0]!='\0') { for(choice=addone;choice<=cleanall;choice++) { if(strcmp(yourchoice,choices[choice])==0) { the_order_iseffective=true; break; } } if(the_order_iseffective) switch (choice) { case (addone): AddOne(&classmates); break; case (deleteone):DeleteOne(&classmates); break; case(itemcount):ItemCount(&classmates); break; case(search):FindOne(&classmates); break; case(showall):ShowAll(&classmates); break; case(cleanall):CleanAll(&classmates); break; } else printf("i don\'t know what the order mean!\n"); the_order_iseffective=false; while(getchar()!='\n') continue; puts("next order\n"); menu(); } puts("good job\n"); return 0; } void menu(void) { puts("please chioce what do you want to to?\n"); puts("enter \"addone\" to add a classmate.\n"); puts("enter \"deleteone\" to delete a classmate\n"); puts("enter \"itemcount\" to konw the number of your classmatas\n"); puts("enter \"search\" to find your classmate\n"); puts("enter\"showall\"to printf all of your classmates.\n"); puts("enter \"cleanall\"to chean all items. \n"); puts("enter \"quit\" to quit\n"); return 0; } void AddOne (Tree *ptree) { Item temp; if(TreeIsFull(ptree)) puts("no room to accept a new one!\n"); else { puts("please enter your classmate\'s name:\n"); gets(temp.name); puts("please enter sex of your classmate:\n"); gets(temp.sex); puts("please enter age of your classmate:\n"); scanf("%d",&temp.age); AddItem(&temp,ptree); } } void DeleteOne(Tree *ptree) { Item temp; if(TreeIsEmpty(ptree)) { puts("no members to put out!\n"); return; } puts("please enter name of classmate you want to delete:\n"); gets(temp.name); if(DeleteItem(&temp,ptree)) printf("sccessfully\n"); else puts("is not a members\n"); } void ItemCount(const Tree *ptree) { unsigned int number; number=TreeItemCount(ptree); printf("there are %d classmates\n",number); } void FindOne(const Tree *ptree) { Item temp; if(TreeIsEmpty(ptree)) { puts("no information in the tree!"); return; } puts("please enter name of classmate you want to find:\n"); gets(temp.name); if(InTree(&temp,ptree)) puts("is a member in the class!"); else puts("is not a member"); } void ShowAll(const Tree *ptree) { if(TreeIsEmpty(ptree)) { puts("no information in the tree!\n"); return; } else Traverse(ptree,PrintItem); } void CleanAll(Tree *ptree) { DeleteAll(ptree); printf("just clean all\n"); } void PrintItem(Item item) { printf("name:%s,\tsex:%s,\tage:%d",item.name,item.sex,item.age); }