1、序
詳細(xì)實(shí)現(xiàn)了二叉查找樹的各種操作:插入結(jié)點(diǎn)、構(gòu)造二叉樹、刪除結(jié)點(diǎn)、查找、 查找最大值、查找最小值、查找指定結(jié)點(diǎn)的前驅(qū)和后繼
2、二叉查找樹簡介
它或者是一棵空樹;或者是具有下列性質(zhì)的二叉樹: (1)若左子樹不空,則左子樹上所有結(jié)點(diǎn)的值均小于它的根結(jié)點(diǎn)的值; (2)若右子樹不空,則右子樹上所有結(jié)點(diǎn)的值均大于它的根結(jié)點(diǎn)的值; (3)左、右子樹也分別為二叉排序樹
3、二叉查找樹的各種操作
此處給出代碼,注釋非常詳細(xì),具體操作請參考代碼:
-
-
-
-
-
-
-
- #include<stdio.h>
- #include<stdlib.h>
-
-
- typedef int KeyType;
- typedef struct Node
- {
- KeyType key;
- struct Node * left;
- struct Node * right;
- struct Node * parent;
- }Node,*PNode;
-
-
-
- void inseart(PNode * root,KeyType key)
- {
-
- PNode p=(PNode)malloc(sizeof(Node));
- p->key=key;
- p->left=p->right=p->parent=NULL;
-
- if((*root)==NULL){
- *root=p;
- return;
- }
-
- if((*root)->left == NULL && (*root)->key > key){
- p->parent=(*root);
- (*root)->left=p;
- return;
- }
-
- if((*root)->right == NULL && (*root)->key < key){
- p->parent=(*root);
- (*root)->right=p;
- return;
- }
- if((*root)->key > key)
- inseart(&(*root)->left,key);
- else if((*root)->key < key)
- inseart(&(*root)->right,key);
- else
- return;
- }
-
-
- PNode search(PNode root,KeyType key)
- {
- if(root == NULL)
- return NULL;
- if(key > root->key)
- return search(root->right,key);
- else if(key < root->key)
- return search(root->left,key);
- else
- return root;
- }
-
-
- PNode searchMin(PNode root)
- {
- if(root == NULL)
- return NULL;
- if(root->left == NULL)
- return root;
- else
- return searchMin(root->left);
- }
-
-
- PNode searchMax(PNode root)
- {
- if(root == NULL)
- return NULL;
- if(root->right == NULL)
- return root;
- else
- return searchMax(root->right);
- }
-
-
- PNode searchPredecessor(PNode p)
- {
-
- if(p==NULL)
- return p;
-
- if(p->left)
- return searchMax(p->left);
-
- else{
- if(p->parent == NULL)
- return NULL;
-
- while(p){
- if(p->parent->right == p)
- break;
- p=p->parent;
- }
- return p->parent;
- }
- }
-
-
- PNode searchSuccessor(PNode p)
- {
-
- if(p==NULL)
- return p;
-
- if(p->right)
- return searchMin(p->right);
-
- else{
- if(p->parent == NULL)
- return NULL;
-
- while(p){
- if(p->parent->left == p)
- break;
- p=p->parent;
- }
- return p->parent;
- }
- }
-
-
-
- int deleteNode(PNode* root,KeyType key)
- {
- PNode q;
-
- PNode p=search(*root,key);
- KeyType temp;
-
- if(!p)
- return 0;
-
- if(p->left == NULL && p->right == NULL){
-
- if(p->parent == NULL){
- free(p);
- (*root)=NULL;
- }else{
-
- if(p->parent->left == p)
- p->parent->left=NULL;
- else
- p->parent->right=NULL;
- free(p);
- }
- }
-
-
- else if(p->left && !(p->right)){
- p->left->parent=p->parent;
-
- if(p->parent == NULL)
- *root=p->left;
-
- else if(p->parent->left == p)
- p->parent->left=p->left;
- else
- p->parent->right=p->left;
- free(p);
- }
-
- else if(p->right && !(p->left)){
- p->right->parent=p->parent;
-
- if(p->parent == NULL)
- *root=p->right;
-
- else if(p->parent->left == p)
- p->parent->left=p->right;
- else
- p->parent->right=p->right;
- free(p);
- }
-
-
-
- else{
-
- q=searchSuccessor(p);
- temp=q->key;
-
- deleteNode(root,q->key);
- p->key=temp;
- }
- return 1;
- }
-
-
- void create(PNode* root,KeyType *keyArray,int length)
- {
- int i;
-
- for(i=0;i<length;i++)
- inseart(root,keyArray[i]);
- }
-
- int main(void)
- {
- int i;
- PNode root=NULL;
- KeyType nodeArray[11]={15,6,18,3,7,17,20,2,4,13,9};
- create(&root,nodeArray,11);
- for(i=0;i<2;i++)
- deleteNode(&root,nodeArray[i]);
- printf("%d\n",searchPredecessor(root)->key);
- printf("%d\n",searchSuccessor(root)->key);
- printf("%d\n",searchMin(root)->key);
- printf("%d\n",searchMax(root)->key);
- printf("%d\n",search(root,13)->key);
- return 0;
- }
4、附錄
參考書籍 《算法導(dǎo)論》
|