二叉树构建与遍历,急急急···!!!!!

WEN2222 2008-06-06 12:16:23
题目如下:

Preorder
Submit: 0 Accepted:0
Time Limit: 1000MS Memory Limit: 655536K
Description
Given a rooted binary tree T, output the result of preorder traversals.

Input
The first line is an integer N (N <= 1000), the number of vertex in this tree. Second line is also an integer S (0 < S <=N), root of the tree. Next N-1 line will consist of a sequence of edge descriptions. Each edge description will consist of three integers; the first integer a identifies the node from which the edge begins, and the second integer b identifies the node to which the edge is directed. If the third integer c equals 0, it means b is the left child of a, otherwise b is the right child of a(0 < a , b <=N). Node numbers will always be greater than zero.


Output
One line, the result of preorder traversals


Sample Input

7
1
1 2 0
1 3 1
2 4 0
2 5 1
3 6 0
3 7 1


Sample Output

1 2 4 5 3 6 7

写了个程序,用数组做的,吵了内存,改又改得乱七八糟,不知怎么办,请各位帮帮忙,谢谢,分不够在给
...全文
397 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
IanFang 2008-06-06
  • 打赏
  • 举报
回复
这种基础题还是自己做做的比较好,试着写一下代码,有问题大家再给你指出来。
zjw6861982 2008-06-06
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
typedef struct _node
{
int i;
struct _node *lchild;
struct _node *rchild;
}NODE;


void creatTree(NODE *rootNode, int i)
{
if(rootNode == NULL)
{
return;
}
memset(rootNode, 0, sizeof(NODE));
rootNode->i = i;
}

NODE *FindNode(NODE *rootNode, int i)
{
NODE *node = rootNode;

if(NULL == rootNode)
{
return NULL;
}
if(rootNode->i == i)
{
return node;
}
if(NULL == (node = FindNode(rootNode->lchild, i)))
{
node = FindNode(rootNode->rchild, i);
}
return node;
}

void InsertChildNode(NODE *fatherNode, int i, int opt)
{
NODE *node = NULL;

if(fatherNode == NULL)
{
return;
}
switch(opt)
{
case 0:
if((node = malloc(sizeof(NODE))) == NULL)
{
printf("error");
return;
}
memset(node, 0, sizeof(NODE));
node->i = i;
if(NULL != fatherNode->lchild)
{
printf("error");
free(node);
return;
}
fatherNode->lchild = node;
break;
case 1:
if((node = malloc(sizeof(NODE))) == NULL)
{
printf("error");
return;
}
memset(node, 0, sizeof(NODE));
node->i = i;
if(NULL != fatherNode->rchild)
{
printf("error");
free(node);
return;
}
fatherNode->rchild = node;
break;
default:
break;
}
}

void PrintTree(NODE *root)
{
if(NULL == root)
{
return;
}
printf("%d ", root->i);
PrintTree(root->lchild);
PrintTree(root->rchild);
}
int GetTreeDepth(NODE *root)
{
int l = 0, r = 0;

if(NULL == root)
{
return 0;
}
l = GetTreeDepth(root->lchild) + 1;
r = GetTreeDepth(root->rchild) + 1;
return (l>r) ? l : r;
}
void main()
{
int n;
int i;
int start;
int end;
int opt;
NODE root;
NODE *node = NULL;

if(scanf("%d", &n) > 0)
{
if(n < 1)
{
return;
}
if(scanf("%u", &start) > 0)
{
creatTree(&root, start);
}
for(i=0; i<n-1; i++)
{
if(scanf("%d", &start) > 0
&& scanf("%d", &end) > 0
&& scanf("%d", &opt) > 0)
{
if(NULL != (node = FindNode(&root, start)))
{
InsertChildNode(node, end, opt);
}
}
}
PrintTree(&root);
}
}
zjw6861982 2008-06-06
  • 打赏
  • 举报
回复
楼主要的是能通过测试的程序,楼上的....
laolaoliu2002 2008-06-06
  • 打赏
  • 举报
回复
#include "stdio.h"

typedef char ElemType;
typedef struct BTNode
{
ElemType data; /* data field */
int ltag,rtag;
struct BTNode *lchild;
struct BTNode *rchild;
} BTNode;
#define MAXNUM 150

/* 线索二叉树需要的全局变量 */
BTNode *pre;

/* 辅助构建二叉树的全局变量 */
BTNode *p[MAXNUM+1];

/* 构建二叉树 */
BTNode *Create_BiTree(void)
{
BTNode* t;
int i;
int j;
char ch;

printf("\n enter i, ch :");
scanf("%d,%c", &i, &ch);

while (i != 0 && ch != '#' )
{
BTNode* s = (BTNode*)malloc(sizeof(BTNode));
s->data = ch;
s->lchild = s->rchild = NULL;
s->ltag = s->rtag = 0;

p[i] = s;

if ( i == 1 )
t = s;
else
{
j = i/2;

if ( i%2 == 0 )
p[j]->lchild = s;
else
p[j]->rchild = s;
}
printf("\n enter i, ch :");
scanf("%d,%c", &i, &ch);
}

return t;
}

/* 中序递归遍历二叉树 */
void Inorder(BTNode *bt)
{
if (bt)
Inorder(bt->lchild);
if (bt)
printf("%c ",bt->data); /* 访问根结点 */
if (bt)
Inorder(bt->rchild);
} /* inorder */

/* 中序线索化二叉树 */
void InOrderThr(BTNode *t)
{
if ( t )
{
InOrderThr (t->lchild ); /*线索化左子树 */
if (t ->lchild==NULL)
{
t -> ltag=1;
t -> lchild=pre;
}
if (pre && pre -> rchild==NULL) /*建立pre结点的后继线索*/
{
pre -> rtag=1;
pre -> rchild=t;
}
pre=t;

InOrderThr( t -> rchild); /*线索化右子树*/
}
}

/* 中序遍历线索二叉树 */
void InVisitThr(BTNode *t)
{
BTNode *p=t ;
if( NULL)
{
while(p->lchild !=NULL)
p=p->lchild ;
printf("%d,%c,%d\n", p->ltag, p->data, p->rtag);

while(p->rchild!=NULL)
{
if (p->rtag==1)
p=p->rchild;
else
{
p=p->rchild;
while(p->ltag!=1)
p=p->lchild;
}
printf("%d,%c,%d\n", p->ltag, p->data, p->rtag);
}
}
}

int main(void)
{
BTNode* t;
t = Create_BiTree();

Inorder(t);
printf("\n");

InOrderThr(t);
InVisitThr(t);

getchar();
return 0;
}
laolaoliu2002 2008-06-06
  • 打赏
  • 举报
回复
http://hi.baidu.com/cuily/blog/item/0967a0460c3b8a0d6b63e5b2.html
laolaoliu2002 2008-06-06
  • 打赏
  • 举报
回复
二叉树的前序构造算法
/* 1. 基本思想
 基于先序遍历的构造,即以二叉树的先序序列为输入构造。
注意:
 先序序列中必须加入虚结点以示空指针的位置。
【例】
建立上图所示二叉树,其输入的先序序列是:ABD∮∮CE∮∮F∮∮。

2. 构造算法
 假设虚结点输入时以空格字符表示,相应的构造算法为:
注意:
 调用该算法时,应将待建立的二叉链表的根指针的地址作为实参。
*/
void CreateBinTree (BinTree *T)
{ //构造二叉链表。T是指向根指针的指针,故修改*T就修改了实参(根指针)本身
char ch;
if((ch=getchar())=='')
*T=NULL; //读人空格,将相应指针置空
else{ //读人非空格
*T=(BinTNode *)malloc(sizeof(BinTNode)); //生成结点
(*T)->data=ch;
CreateBinTree(&(*T)->lchild); //构造左子树
CreateBinTree(&(*T)->rchild); //构造右子树
}
}
laolaoliu2002 2008-06-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define NodeNum 20
//struct node type

typedef char DataType;//用户可根据具体应用定义DataType的实际类型
typedef struct node{
DataType data;
struct node *lchild,*rchild; //左右孩子指针
}BinTNode; //结点类型
typedef BinTNode* BinTree;//BinTree为指向BinTNode类型结点的指针类型

main(){

void InOrder(BinTree T);
void PreOrder(BinTree T);
void PostOrder(BinTree T);
BinTree CreateBinTree(char *pre,char *in,int n);//由先序,中序序列构建二叉树
void freeT(BinTree T);

BinTree T = NULL;

char preorder[NodeNum];
char inorder[NodeNum];
printf("enter the preorder:");
gets(preorder);
printf("enter the inorder:");
gets(inorder);
T = CreateBinTree(preorder, inorder, 7);

printf("the preorder is:\n");
PreOrder(T);
printf("\n");

printf("the inorder is:\n");
InOrder(T);
printf("\n");

printf("the postorder is:\n");
PostOrder(T);
printf("\n");

freeT(T);
return 0;
}
//preorder
void PreOrder(BinTree T){

if(T){

printf("%c", T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//inorder
void InOrder(BinTree T){

if(T){

InOrder(T->lchild);
printf("%c", T->data);
InOrder(T->rchild);
}
}
//postorder
void PostOrder(BinTree T){

if(T){

PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c", T->data);
}
}
//create a tree according to the preorder and the inorder
BinTree CreateBinTree(char *pre,char *in,int n)
{
BinTree s;
char *p;
int k;
if( n <= 0 )
return NULL;
s = (BinTree)malloc(sizeof(BinTree));
s->data = *pre;
for( p = in ; p < in + n; p ++)
if(*p == *pre)
break;
k = p - in;
s->lchild = CreateBinTree( pre + 1, in, k);
s->rchild = CreateBinTree( pre + k + 1, p + 1, n - k - 1);
return s;
}
//free all the malloc
void freeT(BinTree T)
{
if(T)
{
freeT(T->lchild);
freeT(T->rchild);
free(T);
}
}
jintianfree 2008-06-06
  • 打赏
  • 举报
回复


void BTree::PreOrder(void)
{
PreOrder(root);
}

void BTree::PreOrder(TreeNode* &preRoot)
{
if(preRoot==NULL)
return;
printf_s("%c",preRoot->data);
PreOrder(preRoot->lchild);
PreOrder(preRoot->rchild);

}



递归的前序遍历。

仅供参考,楼主自己动手写写吧。

自己写一下才能学到更多东西。
evifree 2008-06-06
  • 打赏
  • 举报
回复
1楼的说的对, 自己试着写一下代码才能有提高, 代码有问题大家可以帮你分析, 对你帮助更大.
jmulxg 2008-06-06
  • 打赏
  • 举报
回复
用递归做,把数据结构的书本拿出来看看
liyuzhu_1984 2008-06-06
  • 打赏
  • 举报
回复
随便个数据结构书都有二叉树遍历算法

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧