用C语言编写程序,创建一个二叉树的二叉链表结构,然后输出从根结点到所有叶子结点的路径。

寻求完整的程序

#include
#include
#include
typedef
struct
node
{
char
data;
struct
node
*lchild;
struct
node
*rchild;
}tnode;
tnode
*createtree()
{
tnode
*t;
char
ch;
ch=getchar();
if(ch=='0')
t=null;
else
{
t=(tnode
*)malloc(sizeof(tnode));
t->data=ch;
t->lchild=createtree();
t->rchild=createtree();
}
return
t;
}
void
listtree(tnode
*t)
{
if
(t!=null)
{
printf("%c",t->data);
if(t->lchild!=null||t->rchild!=null)
{
printf("(");
listtree(t->lchild);
if(t->rchild!=null)
printf(",");
listtree(t->rchild);
printf(")");
}
}
}
void
inorder(tnode
*t)
{
if(t!=null)
{
inorder(t->lchild);
printf("%c\t",t->data);
inorder(t->rchild);
}
}
void
leve(tnode
*t)
{
tnode
*quee[100];
int
front,rear;
front=-1;
rear=0;
quee[rear]=t;
while(front!=rear)
{
front++;
printf("%c\t",quee[front]->data);
if(quee[front]->lchild!=null)
{
rear++;
quee[rear]=quee[front]->lchild;
}
if(quee[front]->rchild!=null)
{
rear++;
quee[rear]=quee[front]->rchild;
}
}
}
main()
{
tnode
*t=null;
printf("请输入二叉树元素:");
t=createtree();
printf("广义表的输出:");
listtree(t);
printf("\n");
printf("二叉树的中序遍历:");
inorder(t);
printf("\n");
printf("二叉树的层次遍历:");
leve(t);
printf("\n");
system("pause");
}
/*
输入:ab00cd00e00f000
输出:a(b,c((d,e))
中序遍历:
b
a
d
c
e
层次遍历:a
b
c
d
e
*/
温馨提示:内容为网友见解,仅供参考
第1个回答  2007-04-25
#include <stdio.h>
#include <stdlib.h>
typedef struct BTnode
{
char data;
struct BTnode *lchild,*rchild;
}BTNode;
#define NodeLen sizeof(BTNode)
BTNODE *Creat_Bt(void);
void Preorder(BTNode *bt);
void Inorder(BTNode *bt);
int count,deep;
main()
{
BTNode *t;
char s[10];
for(;;)
{
printf("1----------建立二叉树
2----------先中根遍历二叉树\n
3----------求叶子数和树深
4----------退出\n);
gets(s);
switch(*s)
{
case'1':t=Creat_Bt();break;
case'2':Preorder(t);printf("\n");break;
case'3':count=deep=0;
Leafs_deep(t,0);printf("\n叶子结点数:%d\n"count);printf("树深:%d\n",deep);break;
case'4':exit(0);
}
}
第2个回答  2012-03-19
typedef int DataType;
typedef struct BNode{
DataType data;
struct BNode *left,*right;
}* LinkTree;
#include <stdio.h>
#include <stdlib.h>
int LinkTreeCreate(LinkTree *T)
{
DataType d;

scanf("%d",&d);
if (d == 0)
*T = NULL;
else
{
if ((*T = (struct BNode*)malloc(sizeof(struct BNode))) == NULL)
return -1;
(*T)->data = d;
if (LinkTreeCreate(&(*T)->left) == -1)
return -1;
if (LinkTreeCreate(&(*T)->right) == -1)
return -1;
}
return 0;
}
struct PNode{
struct BNode *ptr;
int flag;
};
typedef struct{
struct PNode *bottom;
int stacksize;
int top;
} SeqStack;
#define INCREMENT 10
int SeqStackPush(SeqStack *S,struct PNode d)
{
if (S->top == S->stacksize)
{
if ((S->bottom = (struct PNode*)realloc(S->bottom,(S->stacksize+INCREMENT)*sizeof(struct PNode))) == NULL)
return -1;
S->stacksize += INCREMENT;
}
S->bottom[S->top++] = d;
return 0;
}
int SeqStackPop(SeqStack *S,struct PNode *d)
{
if (S->top == 0)
return -1;
*d = S->bottom[--S->top];
return 0;
}
#define STACKSIZE 100
void LinkTreeTraverse(LinkTree T)
{
SeqStack S;
struct PNode p;

S.bottom = (struct PNode*)malloc(STACKSIZE*sizeof(struct PNode));
S.stacksize = STACKSIZE;
S.top = 0;
p.ptr = T;
p.flag = 0;
while (S.top >= 0)
if (p.ptr != NULL)
{
SeqStackPush(&S,p);
p.ptr = p.ptr->left;
p.flag = 0;
}
else
{
if (SeqStackPop(&S,&p) == -1)
break;
if (p.flag == 0)
{
p.flag = 1;
SeqStackPush(&S,p);
p.ptr = p.ptr->right;
p.flag = 1;
}
else
{
printf("%t",p.ptr->data);
p.ptr = NULL;
}
}
}
void main(void)
{
LinkTree T;

LinkTreeCreate(&T);
LinkTreeTraverse(T);
}
输入2 3 0 0 5 8 0 1 0 0 0
输出18532
第3个回答  2007-04-25
meng

用C语言定义二叉树的二叉链表存储结构,完成二叉树的建立,先序中序后...
return s1+s2+1;} } void main(){ TLNode Tree;printf("input 根节点: ");create(&Tree);printf("先序遍历:");print1(Tree);printf("中序遍历");print2(Tree);printf("后序遍历");print3(Tree);printf("\\n深 度:%d \\n",depth(Tree));printf("总结点数:%d \\n",Cnode(Tree...

运用C++如何使用二叉链表存储二叉树,遍历输出叶子节点路径,递归输出...
构造的二叉树结构如下:运行结果如下:代码如下:include <iostream>#include <vector>using namespace std;typedef struct tnode \/\/定义树节点结构{int val;tnode* left;tnode* right;tnode(int x=0):val(x),left(NULL),right(NULL){}\/\/默认构造函数}TreeNode,*pTreeNode;void getPath(TreeNo...

编写程序,用先序递归遍历法建立二叉树的二叉链表存储结构,输出其先序...
BiTNode *bulid() \/*建树*\/ { BiTNode *q;BiTNode *s[20];int i,j;char x;printf("请按顺序输入二叉树的结点以输入0和*号结束\\n");printf("请输入要输入的为第几个结点i=\\n");scanf("%d",&i);printf("请输入你要输入该结点的数为x=");getchar();scanf("%c",&x);while(i!=...

C++: 编写程序,创建一个二叉树。实现统计二叉树叶子结点的个数和二叉...
include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef int ElemType; \/\/数据类型\/\/定义二叉树结构,与单链表相似,多了一个右孩子结点typedef struct BiTNode{ElemType data; \/\/数据域struct BiTNode*lChild, *rChlid; \/\/左右子树域}BiTNode, *BiTree;\/\/先序创建二叉树int CreateBi...

用C语言建立一棵二叉树,使用二杈链表存储,对其进行后续遍历,输出后序...
typedef int datatype;typedef struct node { datatype data;struct node* lchild;struct node* rchild;}BTNode;void CreatBTNode(BTNode *&b,char * str){ BTNode *p,*st[Maxsize];int top=-1;p=NULL;b=NULL;int j=0,k;char ch=str[j];while(ch!='\\0'){ switch(ch){ case '(':...

已知二叉树采用链表存储结构,根结点指针为T,请写出计算二叉树中度为2...
采用深度或者广度遍历就可以,分别采用栈或者队列结构。对于访问到的每个节点,如果度为2,就是所求的。比如用栈的话 push(ST,root)while(not empty(ST)){ node=pop(ST)if(node->left)push(ST,node->left)if(node->right)push(ST,node->right)} 上面的伪代码实际上就是图的深度遍历,二叉树...

以二叉链表为存储结构,分别写出求二叉树结点总数及叶子总数的算法...
【答案】:(1)计算结点总数 int CountNode(BinTree*root){ intnum1,num2;if(root==Null)return(0);else if(root->lchild==Null&&rooot->rchild==Null)return(1);else { num 1=CountNode(root->lchild);num 2=CountNode(root->rchild);return(num1+num2+!);} } (2)计算叶子总数...

以二叉链为存储结构,写一算法求二叉树的叶子结点个数
Status CreateBiTree(BiTree &T) \/\/构造一个二叉链表表示的二叉树T { char ch;cout<<"请输入结点的值(字符型,若空则用'#'): ";cin>>ch;if(ch=='#') T=NULL;else { \/\/if(!(T=(BiTNode*)malloc(sizeof(BiTNode))) exit(1);T=new BiTNode;if(!T) exit(1);T->data =ch; \/...

请问C语言如何创建二叉树???
创建二叉树的源程序如下:include <cstdlib> include <stdio.h> typedef struct node { \/\/树的结点 int data;struct node* left;struct node* right;} Node;typedef struct { \/\/树根 Node* root;} Tree;void insert(Tree* tree, int value)\/\/创建树 { Node* node=(Node*)malloc(sizeof(...

请大神用C语言帮编一个关于家谱的程序!要求:
\/\/一一一一一基本操作的函数原型说明(部分)一一一一一 Status CreateBitree(BiTree &T);\/\/按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树。\/\/构造二叉链表表示的二叉树T.void PreOrder(BiTree T);\/\/采用二叉链表存储结构,先序遍历二叉树T,对每个结点的访问就是输出它的值 vo...

相似回答