Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct _node
- {
- int data;
- struct _node* left;
- struct _node* right;
- struct _node* father;
- };
- typedef struct _node node;
- node *insert_node(node *root, int value)
- {
- node *new_node;
- node *current;
- node *parent;
- new_node = (node *)malloc(sizeof(node));
- new_node->data = value;
- new_node->left = NULL;
- new_node->right = NULL;
- new_node->father=NULL;
- if(root == NULL)
- {return new_node;}
- else
- {
- current = root;
- while(current != NULL)
- {
- parent = current;
- if(current->data > value)
- {current = current->left;}
- else
- {current = current->right;}
- }
- if(parent->data > value)
- {
- parent->left = new_node;
- new_node->father=parent;
- }
- else
- {
- parent->right = new_node;
- new_node->father=parent;
- }
- }
- return root;
- }
- node *find_node(node *ptr, int value)
- {
- while(ptr!=NULL)
- {
- if(ptr->data==value)
- {return ptr;}
- else
- {
- if (ptr->data>value)
- {ptr=ptr->left;}
- else
- {ptr=ptr->right;}
- }
- }
- return NULL;
- }
- void print_inorder(node *ptr)
- {
- if(ptr != NULL)
- {
- print_inorder(ptr->left);
- printf("%d ", ptr->data);
- print_inorder(ptr->right);
- }
- }
- void print_preorder(node *ptr)
- {
- if(ptr != NULL)
- {
- printf("%d ", ptr->data);
- print_preorder(ptr->left);
- print_preorder(ptr->right);
- }
- }
- void print_postorder(node *ptr)
- {
- if(ptr != NULL)
- {
- print_postorder(ptr->left);
- print_postorder(ptr->right);
- printf("%d ", ptr->data);
- }
- }
- void freeall(node *ptr) //要從最後一個往前殺
- {
- if(ptr != NULL)
- {
- freeall(ptr->left);
- freeall(ptr->right);
- free(ptr);
- }
- }
- // 刪除節點
- node *delete_node(node *root, node *ptr)
- {
- node *parent = ptr->father;
- if(ptr->left == NULL) // 情況1: 節點沒有左子樹 如果要刪的是根節點
- {
- if(root == ptr) // 如果要刪的是根節點
- {root = root->right;}
- else // 其他
- {
- if( ptr->data > parent->data )
- {parent->right = ptr->right;}//要刪除的節點在父節點右方,所以將父節點的右節點指向刪除節點的右節點
- else
- {parent->left = ptr->right;}//要刪除的節點在父節點左方,所以將父節點的左節點指向刪除節點的右節點
- }
- free(ptr);
- }
- else if(ptr->right == NULL) // 情況2: 節點沒有右子樹
- {
- if(root!=ptr)
- {
- if( ptr->data > parent->data )
- {parent->right = ptr->left;}//要刪除的節點在父節點右方,所以將父節點的右節點指向刪除節點的左節點
- else
- {parent->left = ptr->left;}//要刪除的節點在父節點左方,所以將父節點的左節點指向刪除節點的左節點
- }
- else
- {root = root->left;}
- free(ptr);
- }
- else // 情況3: 節點有左右子樹
- {
- node* new_node = ptr->left;
- // 找新結點取得ptr的位置, 新結點要最接近ptr, 以維持ptr左邊小於他,右邊大於他
- //所以是從左邊找最大(右),或從右邊找最小(左)
- //此例從左邊找最大
- if(new_node->right!=NULL)
- {
- while(new_node->right != NULL) // 往左子節點之右子樹找最大值當取代點
- {new_node = new_node->right;}
- ptr->right = new_node->left; // 要用new_node取得ptr,所以開始把new_node的子孫灌給ptr
- }
- else
- {ptr->left=new_node->left;}
- ptr->data = new_node->data; // 取代!!
- free(new_node); // 刪除next (注意: 不是刪除ptr)
- }
- return root; // 回傳此樹
- }
- int main()
- {
- int key;
- node *root,*ptr;
- root=NULL;
- int value;
- while(1)
- {
- scanf("%d",&key);
- switch(key)
- {
- case 1:
- scanf("%d",&value);
- root=insert_node(root,value);
- break;
- case 2:
- print_inorder(root);
- printf("\n");
- break;
- case 3:
- print_preorder(root);
- printf("\n");
- break;
- case 4:
- print_postorder(root);
- printf("\n");
- break;
- case 5:
- scanf("%d",&value);
- ptr=find_node(root,value);
- if(ptr==NULL)
- {printf("cannot delete\n");}
- else
- {
- root=delete_node(root,ptr);
- printf("delete %d ok\n",value);//ptr已經刪除,所以要用value來顯示
- }
- break;
- case 6:
- scanf("%d",&value);
- ptr=find_node(root,value);
- if(ptr==NULL)
- {printf("not found\n");}
- else
- {printf("found: %d\n",ptr->data);}
- break;
- case 8:
- freeall(root);
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement