/* 删除策略,在ADT树中: * 1. 如果要删除的节点是叶子节点, 则可以立即删除。 * 2. 如果该节点有1个儿子,则可以通过父节点的绕过该节点删除。 * 3. 如果有两个儿子,用其右子树中最小的代替该节点的数据并递归的删除那个节点。 * */ BTree * delete_from_tree(int n, BTree *T) { BTree *Temp; if(T == NULL) return T; else if(n > T->data) T->right = delete_from_tree(n, T->right); else if(n < T->data) T->left = delete_from_tree(n, T->left); else{ printf("%d\n", T->data); if(T->left && T->right){ Temp = find_min_tree(T->right); T->data = Temp->data; T->right = delete_from_tree(T->data, T->right); }else{ Temp = T; if(T->left == NULL) T = T->right; if(T->right == NULL) T = T->left; free(Temp); } } return T; }
int s2s_leave(struct sockaddr_in* fromaddr,char *channelname,int type){ text_leave_s2s buf; buf.txt_type = TXT_LEAVE_S2S; strcpy(buf.channelname,channelname); if(delete_from_tree(fromaddr,channelname)==0){ sendto(sockfd,(char*)&buf,sizeof(text_leave_s2s)+1,0,(struct sockaddr*)fromaddr,sizeof(struct sockaddr_in)); printf("%s:%d %s:%d send S2S Leave %s\n",inet_ntoa(servaddr.sin_addr),ntohs(servaddr.sin_port),inet_ntoa(fromaddr->sin_addr),ntohs(fromaddr->sin_port),channelname); } return 0; }
int main(int argc, const char *argv[]) { BTree *root, *max, *min, *T; root = create_tree(root); //inorder_traversal(root); //max = find_max_tree(root); //min = find_min_tree(root); //printf("max data is : %d, min data is : %d\n", max->data, min->data); //inorder_traversal(insert_to_tree(5, root)); T = delete_from_tree(5, root); return 0; }
int S2S_Handle_Leave(user_t* user,char* buf,struct sockaddr_in *cliaddr) { char* ptr = ((struct text_leave_s2s*)buf)->channelname; delete_from_tree(cliaddr,ptr); printf("%s:%d %s:%d recv S2S Leave %s\n",inet_ntoa(servaddr.sin_addr),ntohs(servaddr.sin_port),inet_ntoa(cliaddr->sin_addr),ntohs(cliaddr->sin_port),ptr); return 0; }