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

二叉排序树(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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树Binary Sort TreeBinary Search Tree)是一种用于实现数据快速查找的数据结构。它可以方便地进行数据的插入、删除等操作,并且具有较高的查找效率。下面是一个用C语言实现二叉排序树查找算法的示例: ```c #include <stdio.h> #include <stdlib.h> // 二叉排序树的结点定义 typedef struct BSTNode { int data; // 结点数据 struct BSTNode *left; // 左子树指针 struct BSTNode *right; // 右子树指针 } BSTNode; // 创建新结点 BSTNode* createNode(int data) { BSTNode* newNode = (BSTNode*)malloc(sizeof(BSTNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // 向二叉排序树插入结点 BSTNode* insertNode(BSTNode* root, int data) { if (root == NULL) { return createNode(data); } else if (data < root->data) { root->left = insertNode(root->left, data); } else if (data > root->data) { root->right = insertNode(root->right, data); } return root; } // 在二叉排序树查找结点 BSTNode* searchNode(BSTNode* root, int data) { if (root == NULL || root->data == data) { return root; } else if (data < root->data) { return searchNode(root->left, data); } else { return searchNode(root->right, data); } } // 中序遍历二叉排序树 void inorderTraversal(BSTNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } } int main() { BSTNode* root = NULL; int data; // 插入结点 root = insertNode(root, 50); root = insertNode(root, 30); root = insertNode(root, 20); root = insertNode(root, 40); root = insertNode(root, 70); root = insertNode(root, 60); root = insertNode(root, 80); // 中序遍历二叉排序树 printf("中序遍历结果:"); inorderTraversal(root); printf("\n"); // 查找结点 printf("请输入要查找的结点:"); scanf("%d", &data); BSTNode* result = searchNode(root, data); if (result != NULL) { printf("找到了结点 %d\n", result->data); } else { printf("未找到结点 %d\n", data); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值