不能用strcmp()函数,因为strcmp函数在遇到\0时会自动停止检测,这就造成1234和1相等的错误结果。
程序代码:
for (i = 0; a[i] == b[i]; i++) if (a[i] == 0) return 0; return a[i] - b[i];
//tree.h #ifndef _tree_h_ #define _tree_h_ #include <stdbool.h> //struct tree #define MAXCHARR 100 typedef struct treeitem{ char charr[MAXCHARR]; int count; }TreeItem; #define MAXTREEITEMS 1000 typedef struct treenode{ TreeItem item; struct treenode * left; struct treenode * right; }TreeNode; typedef struct tree{ TreeNode * root; int total; }Tree; //function declear bool InitializeTree(Tree * *pptree); bool TreeIsFull(const Tree * ptree); bool TreeIsEmpty(const Tree * ptree); int TreeItemCount(const Tree * ptree); bool AddTreeItem(const TreeItem * pitem,Tree * ptree); int InTree(const TreeItem * pitem,const Tree * ptree); bool DeleteTreeItem(const TreeItem * pitem,Tree * ptree); TreeItem MakeTreeItem(const char * pchar); //bool TraverseTree(const TreeTree * ptree,bool (* pfun)(TreeItem * pitem)); //bool EmptyTree(Tree * ptree); #endif
//tree.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "tree.h" //======================================================================= //local struct //result=[0,1] as [false,true] position=[-1,0,1] as [left,itself,right] typedef struct pair{ TreeNode * parent; TreeNode * child; bool result; int position; }Pair; //local function delear static TreeNode * MakeTreeNode(const TreeItem * pitem); static int ToWhere(const TreeItem * pitem1,const TreeItem * pitem2); static bool AddTreeNode(TreeNode * pnode,TreeNode * pparent,int position); static Pair SeekTreeItem(const TreeItem * pitem,const Tree * ptree); //-------------------------------------------------------------------------- //local function static TreeNode * MakeTreeNode(const TreeItem * pitem) { TreeNode * tpnode = NULL; tpnode = (TreeNode *)malloc(sizeof(TreeNode)); if(tpnode!=NULL) { tpnode->item=*pitem; tpnode->item.count=1; tpnode->left=NULL; tpnode->right=NULL; } else fprintf(stderr,"MakeTreeNode() failed because malloc memory failed!"); return tpnode; } static int ToWhere(const TreeItem * pitem1,const TreeItem * pitem2) { return strcmp(pitem1->charr,pitem2->charr); } static bool AddTreeNode(TreeNode * pnode,TreeNode * pparent,int position) { if(position==-1) { pparent->left=pnode; return true; } else if(position==1) { pparent->right=pnode; return true; } else if(position==0) { ((Tree *)pparent)->root=pnode; return true; } else { fprintf(stderr,"Error!AddTreeNode() failed! because parameter position=%d is not in [-1,0,1]!",position); return false; } } static Pair SeekTreeItem(const TreeItem * pitem,const Tree * ptree) { Pair look; look.parent=(TreeNode *)ptree; look.child=ptree->root; look.result=false; look.position=0; while(look.child!=NULL) { if(ToWhere(pitem,&(look.child->item)) < 0) { look.parent=look.child; look.child=look.child->left; look.position=-1; } else if(ToWhere(pitem,&(look.child->item)) > 0) { look.parent=look.child; look.child=look.child->right; look.position=1; } else if(ToWhere(pitem,&(look.child->item)) == 0) { look.result=true; break; } } return look; } //============================================================================= //function by tree.h bool InitializeTree(Tree * *pptree) { *pptree=(Tree *)malloc(sizeof(Tree)); if(*pptree!=NULL) { (*pptree)->root=NULL; (*pptree)->total=0; return true; } else return false; } // bool TreeIsFull(const Tree * ptree) { return (ptree->total<MAXTREEITEMS)?false:true; } // bool TreeIsEmpty(const Tree * ptree) { return (ptree->total==0)?true:false; } // int TreeItemCount(const Tree * ptree) { return ptree->total; } // bool AddTreeItem(const TreeItem * pitem,Tree * ptree) { if(TreeIsFull(ptree)) { fprintf(stderr,"AddTreeItem() failed!tree is full!"); return false; } Pair look=SeekTreeItem(pitem,ptree); if(look.result) { (look.child->item).count++; return true; } else if(!look.result) { TreeNode * pnode=MakeTreeNode(pitem); return AddTreeNode(pnode,look.parent,look.position); } } // int InTree(const TreeItem * pitem,const Tree * ptree) { Pair look=SeekTreeItem(pitem,ptree); return look.result?look.child->item.count:0; } // bool DeleteTreeItem(const TreeItem * pitem,Tree * ptree) { Pair look=SeekTreeItem(pitem,ptree); if(look.result) { TreeNode * tpnode=NULL; for(tpnode=(look.child->left)->right;tpnode == NULL;tpnode=tpnode->right) continue; tpnode=look.child->right; if(look.position==-1 || look.position==1) look.parent->left=look.child->left; else if(look.position==0) ptree->root=look.child->left; else { fprintf(stderr,"DeleteTreeItem failed because look.position=%d is not in [-1,0,1]!",look.position); return false; } free(look.child); return true; } else { fprintf(stderr,"DeleteTreeItem() failed,not found this item!"); return false; } } // TreeItem MakeTreeItem(const char * pchar) { TreeItem titem; int i; char ch; for(i=0;(ch=*(pchar+i))!='\0';i++) titem.charr[i]=ch; titem.charr[i]='\0'; titem.count=1; return titem; } // //bool TraverseTree(const TreeTree * ptree,bool (* pfun)(TreeItem * pitem)); // //bool EmptyTree(Tree * ptree);
//testtree.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include "tree.h" int main() { printf("Please enter the strings to count:\n"); Tree * tptree; InitializeTree(&tptree); TreeItem titem; char * pch; while(scanf("%s",pch),strcmp(pch,"!!")) { titem=MakeTreeItem(pch); AddTreeItem(&titem,tptree); } printf("Which word you want to find?\n"); scanf("%s",pch); titem=MakeTreeItem(pch); printf("%d",InTree(&titem,tptree)); return 0; }