c 语言将二叉树转化为链表,将二叉树转化为双向链表

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

typedef char elemtype;

struct bnode

{

elemtype data;

int top;

struct bnode *lchild,*rchild;

struct bnode *llink,*rlink;

};

const N=10;

void push(struct bnode *p)

{

struct bnode *q;

new(q);

q=p;

if(top!=0)

{

}

struct bnode *set_tree()

{

struct bnode *t,*p,*q;

int i;

char d;

t=new(bnode);

t->data=rand() % 26 + 'A';

t->lchild=NULL; t->rchild=NULL;

for (i=1;i

{

p=t; d=rand() % 26 + 'A';

while (p!=NULL)

if (d>p->data) {q=p; p=p->rchild;}

else { q=p; p=p->lchild;}

p=new(bnode);

p->data=d; p->lchild=NULL; p->rchild=NULL;

if (d>q->data)   q->rchild=p;

else q->lchild=p;

}

return t;

}

void pre_order(struct bnode *t)

{

if (t!=NULL)

{

cout<data<

pre_order(t->lchild);

pre_order(t->rchild);

}

}

void mid_order(struct bnode *t)

{

if (t!=NULL)

{

mid_order(t->lchild);

cout<data<

mid_order(t->rchild);

}

}

void write(struct bnode *h)

{

struct bnode *p;

p=h;

while(p->rlink!=h)

{

cout<data;

p=p->rchild;

}

cout<data;

}

struct bnode *Conver(struct bnode *t)

{

struct bnode *p,*q,top;

q=p=t;

top=0;

while(p!=NULL||top!=0)

{

if(p!=NULL)

{

q->rlink=p;

p->llink=q;

q=q->rlink;

push(p);

p=p->lchild;

}

else

{

pop(p);

p=p->rchild;

}

}

q->llink->rlink=t;

t->llink=q->llink;

return t;

}

void main()

{   struct bnode *t;

//srand((unsigned)time(NULL));

t=set_tree();

cout<

pre_order(t);

cout<

mid_order(t);

cout<

}

首先栈的定义不会,此外帮我看下COVER过程,能实现嘛?这块不是很懂,忘谁能给个正确的程序 谢谢 不盛感激

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值