data); inordertraverse(t->rc">data">
当前位置:   article > 正文

二叉排序树查找c语言算法,二叉排序树/二叉查找树 (binary sort tree/ binary search tree)的C语言实现...

{ inordertraverse(t->lchild); printf("%d ", t->data); inordertraverse(t->rc

二叉排序树(Binary Sort Tree)又称二叉查找树,亦称二叉搜索树。 它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树;

我们假设用二叉链表进行存储;二叉链表结点。有三个域:一个数据域,两个分别指向左右子结点的指针域。

#include

#include

#include

#define N 9

typedef int ElemType;

typedef struct BSTNode

{

ElemType data ;

struct BSTNode *Lchild, *Rchild;

}BSTNode;

//查找关键字key,迭代版本

BSTNode* SearchBST(BSTNode* &T, ElemType key)

{

BSTNode* p=T;

while( p )

{

if( key == p->data) return p; //查找成功,返回p

if( key < p->data)

p=p->Lchild; //在左子树中继续查找

else

p=p->Rchild; //在右子树中继续查找

}

return NULL; //T为空

}

//查找关键字key,递归版本

BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key)

{

if (NULL==T || key==T->data)

return T;

if ( key< T->data)

return Recur_SearchBST(T->Lchild,key);

else

return Recur_SearchBST(T->Rchild,key);

}

//插入关键字key

void InsertBST(BSTNode* &T,ElemType key)

{

if(T==NULL)

{

T=(BSTNode*)malloc(sizeof(BSTNode));

T->Lchild=T->Rchild=NULL;

T->data=key;

return;

}

if(key < T->data )

InsertBST(T->Lchild,key);

else

InsertBST (T->Rchild, key );

}

//n个数据在数组d中,tree为二叉排序树根

void CreateBiTree(BSTNode* &T,ElemType d[ ] ,int n)

{

T=NULL;

for(int i=0;i

InsertBST(T,d[i]);

}

BSTNode* DeleteNode(BSTNode* &T)

{

BSTNode* p=T;

if (p->Lchild)

{

BSTNode* L = p->Lchild; //L指向其左子树;

BSTNode* prer = p->Lchild; //prer指向其左子树;

while(L->Rchild != NULL)//搜索左子树的最右边的叶子结点L

{

prer = L;

L = L->Rchild;

}

p->data = L->data;

if(prer != L)//若L不是p的左孩子,把L的左孩子作为L的父亲的右孩子

prer->Rchild = L->Lchild;

else

p->Lchild = L->Lchild; //否则结点p的左子树指向L的左子树

free(L);

return p;

}

else

{

BSTNode *q = p->Rchild; //q指向其右子树;

free(p);

return q;

}

}

int DeleteBST(BSTNode* &T,ElemType key)

{

//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素,并返回TRUE;否则返回FALSE

if(!T) return -1; //不存在关键字等于key的数据元素

else

{

if(key == T->data) // 找到关键字等于key的数据元素

{

T = DeleteNode(T);

return 1;

}

else if(key < T->data)

return DeleteBST(T->Lchild, key);

else

return DeleteBST(T->Rchild, key);

}

}

void InorderTraverse(BSTNode* &T) //中序遍历

{

if (T!=NULL)

{

InorderTraverse(T->Lchild) ;

printf("%d ",T->data);

InorderTraverse(T->Rchild) ;

}

}

int main()

{

int arr[N] = {75, 95, 15, 78, 84, 51, 24, 120,24};

BSTNode* head=NULL;

CreateBiTree(head,arr,N);

printf("%d\n",head->data);

InorderTraverse(head); printf("\n");

InsertBST(head,63);

InsertBST(head,54);

InorderTraverse(head); printf("\n");

printf("%d\n",SearchBST(head,78));

printf("%d\n",SearchBST(head,55));

DeleteBST(head,63);

InorderTraverse(head);

printf("\n");

DeleteBST(head,95);

InorderTraverse(head);

printf("\n");

DeleteBST(head,75);

InorderTraverse(head);

printf("\n");

free(head);

return 0;

}

用三叉链表存储:

#include

#include

#include

#define N 10

typedef int ElemType;

typedef struct BSTNode

{

ElemType data ;

struct BSTNode *Lchild, *Rchild, *Parent;

}BSTNode;

void InorderTraverse(BSTNode* &T) //中序遍历

{

if (T!=NULL)

{

InorderTraverse(T->Lchild) ;

printf("%d ",T->data);

InorderTraverse(T->Rchild) ;

}

}

//查找关键字key,迭代版本

BSTNode* SearchBST(BSTNode* &T, ElemType key)

{

BSTNode* p=T;

while( p )

{

if( key == p->data) return p; //查找成功,返回p

if( key < p->data)

p=p->Lchild; //在左子树中继续查找

else

p=p->Rchild; //在右子树中继续查找

}

return NULL; //T为空

}

//查找关键字key,递归版本

BSTNode* Recur_SearchBST(BSTNode* &T, ElemType key)

{

if (NULL==T || key==T->data)

return T;

if ( key< T->data)

return Recur_SearchBST(T->Lchild,key);

else

return Recur_SearchBST(T->Rchild,key);

}

//插入关键字key

void InsertBST(BSTNode* &T,ElemType key)

{

BSTNode* p= T;

BSTNode* father=NULL;

while( p!=NULL )

{

if (p->data==key) return; //防止插入重复键值

father=p;

p=(key < p->data)? p->Lchild :p->Rchild;

}

p=(BSTNode*)malloc(sizeof(BSTNode));

p->Lchild=p->Rchild=NULL;

p->Parent=father;

p->data=key;

if (T==NULL)

T=p;

else

{

if (key < father->data)

father->Lchild=p;

else

father->Rchild=p;

}

}

//n个数据在数组d中,tree为二叉排序树根

void CreateBiTree(BSTNode* &T,ElemType d[] ,int n)

{

T=NULL;

for(int i=0;i

{

InsertBST(T,d[i]);

}

}

BSTNode* DeleteNode(BSTNode* &T)

{

BSTNode* p=T;

if (p->Lchild)

{

BSTNode* L = p->Lchild; //L指向其左子树;

BSTNode* prer = p->Lchild; //prer指向其左子树;

while(L->Rchild != NULL)//搜索左子树的最右边的叶子结点L

{

prer = L;

L = L->Rchild;

}

p->data = L->data;

if(prer != L)//若L不是p的左孩子,把L的左孩子作为L的父亲的右孩子

prer->Rchild = L->Lchild;

else

p->Lchild = L->Lchild; //否则结点p的左子树指向L的左子树

free(L);

return p;

}

else

{

BSTNode *q = p->Rchild; //q指向其右子树;

free(p);

return q;

}

}

int DeleteBST(BSTNode* &T,ElemType key)

{

//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素,并返回TRUE;否则返回FALSE

if(!T) return -1; //不存在关键字等于key的数据元素

else

{

if(key == T->data) // 找到关键字等于key的数据元素

{

T = DeleteNode(T);

return 1;

}

else if(key < T->data)

return DeleteBST(T->Lchild, key);

else

return DeleteBST(T->Rchild, key);

}

}

void PrintRouth(BSTNode* &T, ElemType key)

{

BSTNode* q=SearchBST(T,key);

while(q)

{

printf("%d ",q->data);

q=q->Parent;

}

printf("\n");

}

int main()

{

int arr[N] = {75, 95, 15, 78, 84, 51, 24, 120,25,24};

BSTNode* head=NULL;

CreateBiTree(head,arr,N);

//printf("%d\n",head->data);

InorderTraverse(head); printf("\n");

PrintRouth(head,24);

PrintRouth(head,84);

InsertBST(head,63);

InsertBST(head,54);

InorderTraverse(head); printf("\n");

printf("%d\n",SearchBST(head,78));

printf("%d\n",SearchBST(head,55));

DeleteBST(head,63);

InorderTraverse(head);

printf("\n");

DeleteBST(head,95);

InorderTraverse(head);

printf("\n");

DeleteBST(head,75);

InorderTraverse(head);

printf("\n");

free(head);

return 0;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/709629
推荐阅读
相关标签
  

闽ICP备14008679号