1、序
详细实现了二叉查找树的各种操作:插入结点、构造二叉树、删除结点、查找、 查找最大值、查找最小值、查找指定结点的前驱和后继
2、二叉查找树简介
它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树
3、二叉查找树的各种操作
此处给出代码,注释非常详细,具体操作请参考代码:
-
-
-
-
-
-
-
- #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、附录
参考书籍 《算法导论》
|