读书人

B树剔除求C/C++代码

发布时间: 2012-12-15 15:16:03 作者: rapoo

B树删除求C/C++代码
按照严蔚敏的C数据结构的定义写个删除函数出来,B树纠结了很久,就是不会写删除。。。
[最优解释]
该回复于2012-10-12 10:39:36被版主删除
[其他解释]


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("%d",bt->data);
free(bt);
}

[其他解释]
引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("%d",bt->dat……

纳尼,这么简单?
[其他解释]
引用:
引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("……
那你要怎么样的?
[其他解释]
非递归?
[其他解释]
引用:
非递归?

非递归的也发一下吧
[其他解释]
这个我没有 写不出来
但是有这些
#include<iostream>
using namespace std;
int N=2;

.cpp放这段
int main()
{
for(int i=0;i<N;i++)
cout<<"HelloWorld"<<end;
retunrn 0;
}

[其他解释]
上面贴错
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NULL 0
typedef struct tree{
char Data;
struct tree *left,*right;
}*Bitree;
Bitree create(Bitree T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')//输入#号表示空
{
T=NULL;
}
else
{
if(!(T=(Bitree )malloc(sizeof(Bitree))))
{
printf("错误!\n");
}
T->Data=ch;
T->left=create(T->left);
T->right=create(T->right);
}
return T;
}
void xianxu(Bitree T)
{
if(T)
{
printf("%c",T->Data);
xianxu(T->left);


xianxu(T->right);
}
}
void zhongxu(Bitree T)
{
if(T)
{
zhongxu(T->left);
printf("%c",T->Data);
zhongxu(T->right);
}
}
void houxu(Bitree T)
{
if(T)
{
houxu(T->left);
houxu(T->right);
printf("%c",T->Data);
}
}
void main()
{
Bitree T;
T=create(T);
xianxu(T);
printf("\n");
zhongxu(T);
printf("\n");
houxu(T);
printf("\n");
}

非递归先中后的遍历算法
C/C++ code
//1.先序遍历非递归算法
#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;

void PreOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null
[其他解释]
!StackEmpty(s))
{
while (p!=null) //遍历左子树
{
visite(p->data);
push(s,p);
p=p->lchild;
}//endwhile

if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树

遍历
{
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}//PreOrderUnrec
//2.中序遍历非递归算法
#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;

void InOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null
[其他解释]
!StackEmpty(s))
{
while (p!=null) //遍历左子树
{
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s))


{
p=pop(s);
visite(p->data); //访问根结点
p=p->rchild; //通过下一次循环实现右子树遍历
}//endif
}//endwhile
}//InOrderUnrec
//3.后序遍历非递归算法
#define maxsize 100
typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;

typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;

void PostOrderUnrec(Bitree t)
{
SqStack s;
stacknode x;
StackInit(s);
p=t;
do
{
while (p!=null) //遍历左子树
{
x.ptr = p;
x.tag = L; //标记为左子树
push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点


}
if (!StackEmpty(s))
{
s.Elem[s.top].tag =R; //遍历右子树
p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec


[其他解释]
//求二叉树的深度
int Depth(BiTNode *T)
{
int dep1,dep2;
if(T==NULL)
return(0);
else
{
dep1=Depth(T->lchild);
dep2=Depth(T->rchild);
if(dep1>dep2)
return(dep1+1);
else
return(dep2+1);


}
}

//输出二叉树
void Disptree(BiTNode *T)
{
if(T)
{
printf("%c",T->data);
if(T->lchild
[其他解释]
T->rchild)
{
printf("(");
Disptree(T->lchild);
if(T->rchild)
printf(",");
Disptree(T->rchild);
printf(")");
}
}


[其他解释]
引用:
C/C++ code

//求二叉树的深度
int Depth(BiTNode *T)
{
int dep1,dep2;
if(T==NULL)
return(0);
else
{
dep1=Depth(T->lchild);
dep2=Depth(T->rchild);
if(……

谢了,不过我二叉树的4种遍历都写过了...
[其他解释]
引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("%d",bt->dat……

貌似是二叉树的删除吧。。。。
[其他解释]
引用:
引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("……
和平衡树不一样?
[其他解释]
引用:
引用:

引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
Dest……

纳尼,我的是B树....
[其他解释]
引用:
引用:
引用:

引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt &&am……
还不是平衡树啊 中根遍历得到排好序的结点值 懒得和你讲了


[其他解释]

引用:
引用:
引用:

引用:
引用:

引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
Destro……
无能为力了 不知道你的B树是什么树
[其他解释]
引用:
引用:

引用:
引用:

引用:
C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(b……

我要b树删除啊。。。
[其他解释]
引用:
一般B树指的就是二叉树,估计楼主指的是B-树或者B+树
B树就是平衡二叉树 不回了
[其他解释]
一般B树指的就是二叉树,估计楼主指的是B-树或者B+树
[其他解释]
B树分为B+和B—树 不要浪费我时间 一个

void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild);
printf("%d",bt->data);
free(bt);
}
而已
[其他解释]
引用:
一般B树指的就是二叉树,估计楼主指的是B-树或者B+树

B树是B-树,百度很多地方有说
[其他解释]
引用:
B树分为B+和B—树 不要浪费我时间 一个

C/C++ code


void DestroyTree(BTNode *bt)
{
if(bt && bt->lchild)
DestroyTree(bt->lchild);
if(bt && bt->lchild)
DestroyTree(bt->rchild……

注意,有的教科书上把B树写成B-树或者B_树,不要误解成“B减树”,“-”“_”只是连字符。

读书人网 >C语言

热点推荐