‘壹’ c语言实现二叉搜索
这东西很多的这里给你一个#include
#include
typedef struct np{
int dat;
struct np *left,*right;
} node;
node *create(void)
{
return (malloc(sizeof(node)));
}
node *t(node *a,int d)
{
if (a==NULL) {
a=create();
a->left =a->right =NULL;
a->dat=d;
}
else if (d>=a->dat) {
a->right =t(a->right,d);
}
else if (ddat) {
a->left =t(a->left ,d);
}
return a;
}
void inorder(node *r)
{
if (r!=NULL) {
inorder(r->left );
printf("%d ",r->dat );
inorder(r->right );
}
}
int ser(node *so,int a)
{
if (so==NULL)
return 0;
else if (so->dat==a)
return 1;
else if (a>so->dat)
return ser(so->right,a);
else if (adat)
return ser(so->left ,a);
}
int main(int argc, char* argv[])
{
node *bst=NULL;
FILE *fp;
int i;
fp=fopen("c:\\dat.txt","r"); /*假设数据文件是c:\dat.txt*/
while (!feof(fp)){
fscanf(fp,"%d",&i);
bst=t(bst,i); /*生成二叉排序树*/
}
fclose(fp);
inorder(bst); /*输出二叉排序树*/
putchar('\n');
scanf("%d",&i); /*输入需要查找的数字*/
if (ser(bst,i)) printf("YES"); /*如果找到,则输出yes,否则输出no*/
else printf("NO");
return 0;
}
//-
‘贰’ 从一棵二叉树中查找出所有结点的最大值。
#include <stdio.h>//头文件
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
}
BiTNode,*BiTree;//定义结点类型
int max=-100;//把max定义得足够小
BiTree CreateBiTree()//先序递归创建树
{
int p;BiTree T;
scanf("%d",&p);//注意每输入两个值的时候用空格各隔开
if(p==0)
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));//为结点开辟空间
T->data=p;
T->lchild=CreateBiTree();
T->rchild=CreateBiTree();
}
return (T);
}
int Max(BiTree T)//求最大(递归算法)
{
if(T==NULL)
return 0;
if(T!=NULL)
{
if(T->data>max)
max=T->data;
Max(T->lchild);
Max(T->rchild);
}
return max;
}
void main()//主函数
{
BiTree Ta;
Ta=CreateBiTree();
printf("最大值是:\n");
printf("%d",Max(Ta));
}
原理很简单,随便通过一种遍历(我用的是先序),先把根节点的值给max,然后在访问其他节点的时候判断那个值是否更大,如果是就赋值给max,最后就可以找到最大值了。
想了一下,如果用递归的话就不要用到栈了,这样更简单,如果你需要非递归的话可以联系我。你不会创建树可以联系。
‘叁’ C语言 二叉树遍历求最值 编译不成功 不知哪里错了 求大神改下 谢谢
typedef struct tnode /*类型定义*/
{ KeyType key; //关键字域
ElemType data;//其他数据域
struct tnode *lchild,*rchild;//指针
} BSTNode;
ElemType 在C语言里面不是数据类型,你需要的是把他替换成你需要的数据类型,比如int,float之类的;
tnode *lchild,*rchild;//指针 这样就可以了,不需要加struct;
‘肆’ C语言二叉树求最大值求指点
问题出在max这个变量上。临时变量么有返回。可以将这个函数
int getmax(btnode*t,int max)
{
if(t!=NULL)
{
if(max<t->data)
max=t->data;
getmax(t->lchild,max);
getmax(t->rchild,max);
}
return max;
}
修改为:
intgetmax(btnode*t,int*max)
{
if(t!=NULL)
{
if(*max<t->data)
*max=t->data;
getmax(t->lchild,max);
getmax(t->rchild,max);
}
return*max;
}
另外,你要注意你的编码格式了。需要按照一定的格式来编写,这样可以让别人看的时候更清晰。
‘伍’ 二叉搜索树
哈哈,居然有人来提问了,城院的
我刚奋斗了2小时做了下,你看看对不?
Test5.cpp:
#include <iostream.h>
#include <stdlib.h>
typedef double ElemType;
#define Maxsize 100
#include "BSTree.h"
void main()
{
int i,n;
ElemType x;
ElemType a[Maxsize];
BTreeNode *bst;
InitBSTree(bst);
cout<<"请输入你所要测试的二叉搜索树的结点的个数:";
cin>>n;
cout<<"请输入你要测试的二叉搜索树:"<<endl;
for(i=0;i<n;i++){
cin>>a[i];
}
CreateBSTree(bst,a,n);
cout<<"你输入的二叉搜索树的广义表形式为:";
PrintBSTree(bst);
cout<<endl;
cout<<"输入一个待插入结点的整数值:";
cin>>x;
Insert(bst,x);
cout<<"插入结点后的二叉搜索树的广义表形式为:";
PrintBSTree(bst);
cout<<endl;
cout<<"输入一个待删除结点的值:";
cin>>x;
if(Delete(bst,x)) cout<<"删除元素"<<x<<"成功!"<<endl;
else cout<<"删除元素"<<x<<"失败!"<<endl;
PrintBSTree(bst);
cout<<endl;
cout<<"该二叉搜索树中的最大值为:"<<MaxBSTree(bst)<<endl;
}
BSTree.h:
struct BTreeNode{
ElemType data;
ElemType count;
BTreeNode *left;
BTreeNode *right;
};
void InitBSTree(BTreeNode *&bst) //初始化二叉搜索树
{
bst=NULL;
}
void PrintBSTree(BTreeNode *bst) //以广义表形式输出二叉搜索树
{
if(bst!=NULL){
cout<<bst->data;
cout<<'['<<bst->count<<']';
if(bst->left!=NULL||bst->right!=NULL){
cout<<'(';
PrintBSTree(bst->left);
if(bst->right!=NULL)
cout<<',';
PrintBSTree(bst->right);
cout<<')';
}
}
}
void Insert (BTreeNode *&bst, ElemType item)
{
BTreeNode*t=bst,*parent=NULL;
while(t!=NULL){
parent=t;
if(item<t->data) t=t->left;
else if(item>t->data) t=t->right;
else{
t->count++;
break;
}
}
if((t==NULL)||item!=parent->data){
BTreeNode*p=new BTreeNode;
p->data=item;
p->count=1;
p->left=p->right=NULL;
if(parent==NULL) bst=p;
else if(item<parent->data) parent->left=p;
else parent->right=p;
}
}
void CreateBSTree(BTreeNode *&bst, ElemType a[], int n) //建立二叉搜索树(用非递归算法实现)
{
bst=NULL;
for(int i=0;i<n;i++)
Insert(bst,a[i]);
}
bool Delete (BTreeNode *&bst , ElemType item)
{
BTreeNode *t=bst,*parent=NULL,*m,*n;
while((t!=NULL)&&(t->data!=item)){
if(item==t->data) break;
else{
if(item<t->data){
parent=t;
t=t->left;
}
else{
parent=t;
t=t->right;
}
}
}
if(t->count>1){
t->count--;
return true;
}
else if(t==NULL){
cout<<"没有找到待删除结点!"<<endl;
return false;
}
else{
if((t->left==NULL)&&(t->right==NULL)){
if(t==bst)
bst=NULL;
else{
if(t==parent->left)
parent->left=NULL;
else
parent->right=NULL;
}
}
else if((t->left==NULL)||(t->right==NULL)){
if(t==bst){
if(t->left==NULL)
bst=t->right;
else
bst=t->left;
}
else{
if((t==parent->left)&&(t->left!=NULL))
parent->left=t->left;
else if((t==parent->left)&&(t->right!=NULL))
parent->left=t->right;
else if((t==parent->right)&&(t->left!=NULL))
parent->right=t->left;
else if((t==parent->right)&&(t->right!=NULL))
parent->right=t->right;
}
}
else{
if((t->left!=NULL)&&(t->right!=NULL)){
m=t;
n=t->left;
while(n->right!=NULL){
m=n;
n=n->right;
}
t->data=n->data;
if(m==t)
t->left=n->left;
else
m->right=n->left;
t=n;
}
}
free(t);
return true;
}
}
ElemType MaxBSTree(BTreeNode *bst) //求二叉搜索树的最大结点值(用非递归算法实现)
{
ElemType max;
if(bst==NULL){
cout<<"该二叉搜索树为空!"<<endl;
exit(1);
}
max=bst->data;
while(bst!=NULL){
if(max<bst->data)
max=bst->data;
bst=bst->right;
}
return max;
}
你试试吧,应该对的,选我做答案哈~
‘陆’ 二叉排序树的查找(C语言)代码
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define INFMT "%d"
#define OUTFMT "%d "
/* #define NULL 0L */
#define BOOL int
#define TRUE 1
#define FALSE 0
#define LEN 10000
typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;
/* 插入新节点 */
void Insert(BSTree *tree, ElemType item)
{
BSTree node = (BSTree)malloc(sizeof(BSTNode));
node->data = item;
node->lchild = node->rchild = NULL;
if (!*tree)
*tree = node;
else
{
BSTree cursor = *tree;
while (1)
{
if (item < cursor->data)
{
if (NULL == cursor->lchild)
{
cursor->lchild = node;
break;
}
cursor = cursor->lchild;
}
else
{
if (NULL == cursor->rchild)
{
cursor->rchild = node;
break;
}
cursor = cursor->rchild;
}
}
}
return;
}
/* 查找指定值 */
BSTree Search(BSTree tree, ElemType item)
{
BSTree cursor = tree;
while (cursor)
{
if (item == cursor->data)
return cursor;
else if ( item < cursor->data)
cursor = cursor->lchild;
else
cursor = cursor->rchild;
}
return NULL;
}
/* 中缀遍历 */
void Inorder(BSTree tree)
{
BSTree cursor = tree;
if (cursor)
{
Inorder(cursor->lchild);
printf(OUTFMT, cursor->data);
Inorder(cursor->rchild);
}
}
/* 回收资源 */
void Cleanup(BSTree tree)
{
BSTree cursor = tree, temp = NULL;
if (cursor)
{
Cleanup(cursor->lchild);
Cleanup(cursor->rchild);
free(cursor);
}
}
/* 产生一组随机数 */
void randnum(int *a, int s)
{
int i, j, mod = s * 10;
srand(time(NULL));
for (i = 0; i < s; ++i)
{
a[i] = rand() % mod + 1;
for (j = 0; j < i; ++j)
{
if (a[i] == a[j])
{
a[i] = rand() % mod + 1;
j = -1;
continue;
}
}
}
}
void main()
{
ElemType item;
char choice;
BSTree root = NULL, ret; /* 必须赋予NULL值,否则出错 */
BOOL finish = FALSE;
printf("***欢迎使用二叉排序树演示程序***\n\n");
printf("请选择创建树的方式:\n");
printf("1. 手动输入数据创建二叉排序树\n");
printf("2. 自动产生数据创建二叉排序树\n");
do
{
scanf("%c", &choice);
getchar();
if (choice == '1' || choice == '2')
finish = TRUE;
} while (FALSE == finish);
switch (choice)
{
case '1':
{
printf("请输入数据(-10000结束):\n");
while (1)
{
scanf(INFMT, &item);
if (-10000 != item)
Insert(&root, item);
else
break;
}
break;
}
case '2':
{
int ia[LEN], i = 0, loop = LEN;
randnum(ia, LEN);
while (loop--)
{
Insert(&root, ia[i++]);
}
break;
}
}
printf("\n\n创建完成...\n");
Inorder(root);
printf("\n\n");
/* 二叉排序树的查找测试 */
do
{
printf("\n请输入查找数据:");
scanf("%d", &item);
getchar();
printf("Searching...\n");
ret = Search(root, item);
if (NULL == ret)
printf("查找失败!");
else
printf("查找成功!");
printf("\n继续测试按y,退出按其它键。\n");
choice = getchar();
} while (choice=='y'||choice=='Y');
Cleanup(root);
}
‘柒’ 求C程序 用二叉树结构求出最大值和最小值
先建一棵空的二叉排序树,以次插入,构建二叉排序树,先序遍历,第1个即是最小值,最后一个即是最大值。
‘捌’ C语言,求二叉树的最大值下面有代码,求指点哪里错了
这里的max是一个局部变量,当然不对了,其实最后递归之后返回的结果,仅仅是第一次调用得到的max的值。
intgetmax(btnode*t,intmax)
{
if(t!=NULL)
{if(max<t->data)
max=t->data;
getmax(t->lchild,max);
getmax(t->rchild,max);
}
returnmax;
}
有两种修改的思路
1.让每次调用,都修改同一个max,修改参数为指针
intgetmax(btnode*t,int*max)
{
if(t!=NULL)
{if(*max<t->data)
*max=t->data;
getmax(t->lchild,max);
getmax(t->rchild,max);
}
returnmax;
}
2.修改逻辑,个人更喜欢下面的这个逻辑
intgetmax(btnode*t){
intcur_max=t->data;
if(t->left!=NULL){
intcur_value=getmax(t->left);
if(cur_value>cur_max)
cur_max=cur_value;
}
if(t->right!=NULL){
intcur_value=getmax(t->right);
if(cur_value>cur_max)
cur_max=cur_value;
}
}
returncur_max;
‘玖’ 用c语言编写二叉搜索树的基本操作
评 分 一句:这花脸好大,好特别!通面赤红,一双墨眉,眼角雄俊的吊起,头上边突起一块绿包头,长巾贴脸垂下,脸下边是用马尾做的很长的胡须。