int AddItem(const Item * pi, Tree * ptree) { Node * new_node; if(TreeIsFull(ptree)) { fprintf(stderr,"Tree is full.\n"); return FALSE; } if(SeekItem(pi,ptree).child != NULL) { fprintf(stderr,"Attempt to add duplicate item.\n"); return FALSE; } new_node = MakeNode(pi); if(new_node == NULL) { fprintf(stderr,"couldn't create node.\n"); return FALSE; } ptree->size++; if(ptree->root == NULL) ptree->root = new_node; else AddNode(new_node,ptree->root); return TRUE; }
int InTree(const Item * pi, const Tree * ptree) { if (SeekItem(pi,ptree).child == NULL) return FALSE; else return TRUE; }
//删除元素 BOOL DeleteItem(Item * pi,Tree * ptree) { Pair look; look = SeekItem(pi,ptree);//在树中查找目标项目,成功则返回包含目标项目的父节点和当前节点的指针的结构体 if(look.child == NULL) { return FALSE; } if(!DeQueue(pi,look.child->queue) || look.child->queue->end == NULL)//目标节点的队列为空,或删除首元素后目标节点的队列为空 { if(look.parent == NULL) { DeleteNode(&ptree->root); } else if(look.parent->left == look.child) { DeleteNode(&look.parent->left); } else if(look.parent->right == look.child) { DeleteNode(&look.parent->right); } ptree->size--; } else//当前目标节点的队列不为空,删除成功 { puts("Delete pet's inforamtion succesfully!"); } return TRUE; }
//添加元素,即节点 BOOL AddItem(const Item * pi,Tree * ptree) { Node * new_node;//新节点 if(TreeIsFull(ptree))//判断树是否已满 { fprintf(stderr,"Tree is full.\n"); return FALSE; } if(SeekItem(pi,ptree).child != NULL)//在树中查找目标项目 { fprintf(stderr,"Attempted to add duplicate item\n"); return FALSE; } new_node = MakeNode(pi);//创建新节点,成功返回新节点,否则返回NULL if(new_node == NULL)//创建失败,返回false { fprintf(stderr,"Couldn't create node!\n"); return FALSE; } //节点创建成功,向树中添加节点 ptree->size++;//树的大小加1 if(ptree->root == NULL)//如果树为空 { ptree->root = new_node; } else { AddNode(new_node,ptree->root);//找到新节点应该添加到的位置 } return TRUE; }
/** *操作:从树中删除一个项目 *操作前:pi指向一个待删除的项目,ptree指向一个已初始化过的树 *操作后:删除成功返回TRUE,否则返回FALSE **/ int DeleteItem(const Item * pi,Tree * ptree) { Pair look; look = SeekItem(pi,ptree); if((look = SeekItem(pi,ptree)).child == NULL) { fprintf(stderr,"couldn't find the item in the tree.\n"); return FALSE; } else if(look.parent == NULL) DeleteNode(&ptree->root); else if(look.parent->left == look.child) DeleteNode(&look.parent->left); else DeleteNode(&look.parent->right); ptree->size--; return TRUE; }
bool DelItem(const Item *pi, Tree *ptree){ Pair look; look = SeekItem(pi,ptree); if(look.child==NULL) return false; if(look.parent==NULL){ //删除根项目 DeleteNode(&ptree->root); }else if(look.parent->left==look.child){ DeleteNode(&look.parent->left); }else DeleteNode(&look.parent->right); ptree->items--; return true; }
//添加元素,即节点 BOOL AddItem(const Item * pi,Tree * ptree) { Node * new_node;//新节点 Pair look; //初始化查找到的与输入相同的节点look /*look.parent = NULL; look.child = NULL;*/ if(TreeIsFull(ptree))//判断树是否已满 { fprintf(stderr,"Tree is full.\n"); return FALSE; } if((look = SeekItem(pi,ptree)).child != NULL)//在树中查找目标项目 { fprintf(stderr,"Attempted to add duplicate item\n"); //如果树中已经存在相应节点,则将新同名元素添加到已有节点列表的末尾 if(QueueIsFull(look.child->queue))//判断当前节点列表是否已满 { fprintf(stderr,"Current node's queue is full.\n"); return FALSE; } EnQueue(*pi,look.child->queue); } else { new_node = MakeNode(pi);//创建新节点,成功返回新节点,否则返回NULL if(new_node == NULL)//创建失败,返回false { fprintf(stderr,"Couldn't create node!\n"); return FALSE; } //节点创建成功,向树中添加节点 ptree->size++;//树的大小加1 if(ptree->root == NULL)//如果树为空 { ptree->root = new_node; } else { AddNode(new_node,ptree->root);//找到新节点应该添加到的位置 } } return TRUE; }
bool AddItem(const Item *pi,Tree *ptree){ Node * new_node; if(TreeIsFull(ptree)){ fprintf(stderr,"Tree is Full\n"); return false; //提前返回 } if(SeekItem(pi,ptree).child){ fprintf(stderr,"Attempted to add duplicate item\n"); return false; //提前返回 } new_node = MakeNode(pi); //指向新节点 if(!new_node){ fprintf(stderr,"Couldn't create node\n"); return false; } //成功创建一个节点 ptree->items++; if(!ptree->root){//情况1:树为空 ptree->root = new_node; //新节点即为根节点 }else{ //情况2:树非空 AddNode(new_node,ptree->root); //把新节点添加到树中 } return true; }
//删除元素 BOOL DeleteItem(const Item * pi,Tree * ptree) { Pair look; look = SeekItem(pi,ptree);//在树中查找目标项目,成功则返回包含目标项目的父节点和当前节点的指针的结构体 if(look.child == NULL) { return FALSE; } if(look.parent == NULL) { DeleteNode(&ptree->root); } else if(look.parent->left == look.child) { DeleteNode(&look.parent->left); } else if(look.parent->right == look.child) { DeleteNode(&look.parent->right); } ptree->size--; return TRUE; }
//判断目标元素是否存在于树中 BOOL InTree(const Item * pi,const Tree * ptree,void (* pfun)(Item item)) { Pair look; return ((look = SeekItem(pi,ptree)).child == NULL)?FALSE:TraverseQueue(look.child->queue,pfun); }
bool InTree(const Item *pi,const Tree *ptree){ return (SeekItem(pi,ptree).child==NULL)?false:true; }
//判断目标元素是否存在于树中 BOOL InTree(const Item * pi,const Tree * ptree) { return (SeekItem(pi,ptree).child == NULL)?FALSE:TRUE; }