C语言二叉排序数算法

#include<stdio.h>
#define ARRAYLEN 10
int source[]={12,56,78,92,71,35,82,49,37,10};//增加到二叉排序树中的数据
typedef struct bst//4~9定义二叉排序树
{
int data;
struct bst *left;
struct bst *right;
}BSTree;
void InsertBST(BSTree *t,int key)//在二叉排序树中插入查找关键字key
{
BSTree *p,*parent,*head;
if(!(p=(BSTree *)malloc(sizeof(BSTree *))))//申请内存空间
{
printf("申请内存出错\n");
exit(0);
}
p->data=key;//保存结点数据
p->left=p->right=NULL;//左右子树置空
head=t;
while(head)//查找需要添加的父结点
{
parent=head;
if(key<head->data)//若关键字小于结点的数据
head=head->left;//在左子树上查找
else//若关键字大于结点的数据
head=head->right;//在右子树上查找
}
//判断添加到左子树还是右子树
if(key<parent->data)//小于父节点
parent->left=p;//添加到左子树
else//大于父结点
parent->right=p;//添加到右子树
}
void CreateBST(BSTree *t,int data[],int n)//将数组中的数据插入二叉排序树
{
int i;
t->data=data[0];//为根结点赋值
t->left=t->right=NULL;
for(i=1;i<n;i++)//循环处理,将数组元素添加到排序树中
InsertBST(t,data[i]);//调用函数,将一个元素添加到排序树中
}
void BST_LDR(BSTree *t)//中序遍历
{
if(t)//树不为空,则执行以下操作
{
BST_LDR(t->left);//中序遍历左子树
printf("%d ",t->data);//输出结点数据
BST_LDR(t->right);//中序遍历右子树
}
return ;
}
int main()
{
int key,i,pos;
BSTree bst;//保存二叉排序树根节点
CreateBST(&bst,source,ARRAYLEN);//创建二叉排序树
printf("遍历二叉排序树结果:");
BST_LDR(&bst);//遍历二叉排序树
getch();
return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树(Binary Sort Tree或 Binary Search Tree)是一种用于实现据快速查找的据结构。它具有以下特点:左子树上的元素都小于树根,右子树上的元素都大于树根,所有的子树也满足这个性质。在二叉排序树上查找某个节点,可以使用以下算法: 1. 从树根开始遍历二叉树。 2. 如果待查找元素小于树根元素,则向左遍历。 3. 如果待查找元素大于树根元素,则向右遍历。 4. 如果待查找元素等于树根元素,则结束遍历,找到了目标节点。 5. 如果遍历到叶子节点仍未找到目标节点,则说明目标节点不存在于二叉排序树中。 下面是一个具体的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); } 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; } if (data < root->data) { return searchNode(root->left, data); } else { return searchNode(root->right, data); } } // 销毁二叉排序树 void destroyTree(BSTNode* root) { if (root != NULL) { destroyTree(root->left); destroyTree(root->right); free(root); } } int main() { BSTNode* root = NULL; root = insertNode(root, 50); insertNode(root, 30); insertNode(root, 20); insertNode(root, 40); insertNode(root, 70); insertNode(root, 60); insertNode(root, 80); int target = 40; BSTNode* result = searchNode(root, target); if (result != NULL) { printf("找到了目标节点:%d\n", result->data); } else { printf("未找到目标节点:%d\n", target); } destroyTree(root); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值