『壹』 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語言編寫二叉搜索樹的基本操作
評 分 一句:這花臉好大,好特別!通面赤紅,一雙墨眉,眼角雄俊的吊起,頭上邊突起一塊綠包頭,長巾貼臉垂下,臉下邊是用馬尾做的很長的胡須。