关于二叉树遍历的问题

by_gareth_8023 2015-12-21 11:29:48
这其实是一道题目,题目描述如下:
Description:Given a binary tree, every node of which contains one upper case character (‘A’ to ‘Z’); you just need to print all characters of this tree in pre-order.
Input:Input may contain several test data sets.
For each test data set, first comes one integer n (1 <= n <= 1000) in one line representing the number of nodes in the tree. Then n lines follow, each of them contains information of one tree node. One line consist of four members in order: i (integer, represents the identifier of this node, 1 <= i <= 1000, unique in this test data set), c (char, represents the content of this node described as above, ‘A’ <= c <= ‘Z’), l (integer, represents the identifier of the left child of this node, 0 <= l <= 1000, note that when l is 0 it means that there is no left child of this node), r (integer, represents the identifier of the right child of this node, 0 <= r <= 1000, note that when r is 0 it means that there is no right child of this node). These four members are separated by one space.
Input is ended by EOF.
You can assume that all inputs are valid. All nodes can form only one valid binary tree in every test data set.

OutputFor every test data set, please traverse the given tree and print the content of each node in pre-order. Characters should be printed in one line without any separating space.
代码如下= =本人代码菜鸟,各位高手如果愿意花时间找一下代码中的错。。感激不尽
代码问题:输入数据完后程序崩溃(指针,内存问题吗?但我new和delete是配对使用的呀)
#include<iostream>
using namespace std;
//储存每个节点信息的结构体
struct element{
int index,left,right;
char ch;
element *next;
element()
{
next=NULL;
index=0;
left=0;
right=0;
ch=0;
}
};


//用于储存节点信息的结构体
struct mem{
element *head;
element *cur;
element *otheruse;//删除元素时用,获取指定位置的元素时用
int size;
mem()
{
otheruse=NULL;
cur=NULL;
head=NULL;
size=0;
}
void insert(element *ele)//插入数据成员
{
if(head==NULL)
{
head=ele;
cur=head;
otheruse=head;
}
else
{
cur->next=ele;
cur=cur->next;
}
size++;
}
void remove(int i)//i=0表示删除头指针
{
if(i==0)
{
element *temp=head;
head=head->next;
delete temp;
}
else
{
element *prepos;
for(int j=0;j<i;j++)//找到需要删除的节点位置和其之前的位置
{
if(j==i-1) prepos=otheruse;
otheruse=otheruse->next;
}
prepos->next=otheruse->next;//将需要删除的点的前一个点和后一个点连起来
element *temp=otheruse;
otheruse=head;//将otheruse重置为头指针
delete temp;//删除指针
}
size--;
}
element* getPos(int i)
{
if(i==0) return head;
else
{
for(int j=0;j<i;j++)
otheruse=otheruse->next;
element *res=otheruse;
otheruse=head;//将otheruse重置为头指针
return res;
}
}
int getSize()
{
return size;
}
};


struct Node//树的节点
{
char data;
Node* left;
Node* right;
Node(){
data=0;
left=NULL;
right=NULL;
}
};
void preorder(Node* root)
{
if(root!=NULL)
{
cout<<root->data;
preorder(root->left);
preorder(root->right);
}
}
int findRoot(element ele[],int size)
{
bool det2=false;//为真表示已找到root节点
bool det1=true;//为假表示该节点是某节点的儿子,即一定不是root节点
for(int i=0;i<size;i++)//判断下标为i的element是否为根节点
{
for(int j=0;j<size;j++)//将ele[i]与其他所有元素比较
{
if(i!=j)
{
if(((ele+i)->index==(ele+j)->left)||((ele+i)->index==(ele+j)->right))//当ele[i]的坐标是ele[j]的儿子的坐标时
det1=false;
}
if(!det1) break;
else if(det1&&j==size-1) det2=true;//所有节点都找过了,都符合,此时i是根的位置
}
if(!det2)//没找到根节点,重置det1,再次开始
det1=true;
else//找到根节点
return i;
}
}
void creat(mem *memory,Node* &root,int left,int right)
{
if(left!=0)//有左儿子时构建左子树
{
int tl,tr;//记录左儿子的儿子的坐标
for(int i=0;i<memory->getSize();i++)
{
if(left==memory->getPos(i)->index)//找到左儿子对应坐标的节点
{
Node* leftchild=new Node;//新建一个子节点
leftchild->data=memory->getPos(i)->ch;//将数据赋值给子节点
tl=memory->getPos(i)->left;//获取节点的左右儿子坐标
tr=memory->getPos(i)->right;
memory->remove(i);//删除memory里面储存的该节点信息
root->left=leftchild;//将该节点添加为左儿子节点
break;
}
}
creat(memory,root->left,tl,tr);
}
if(right!=0)//有右儿子时构建右子树
{
int tl,tr;
for(int i=0;i<memory->getSize();i++)
{
if(right==memory->getPos(i)->index)
{
Node* rightchild=new Node;//新建一个子节点
rightchild->data=memory->getPos(i)->ch;//将数据赋值给子节点
tl=memory->getPos(i)->left;//获取节点的左右儿子坐标
tr=memory->getPos(i)->right;
memory->remove(i);//删除memory里面储存的该节点信息
root->right=rightchild;//将该节点添加为右儿子节点
break;
}
}
creat(memory,root->right,tl,tr);
}
}
int main()
{
int n;
while((cin>>n)&&n!=EOF)
{
Node *head=new Node;
mem *memory=new mem;
element *ele=new element[1002];
for(int i=0;i<n;i++)
{
cin>>(ele+i)->index
>>(ele+i)->ch
>>(ele+i)->left
>>(ele+i)->right;
memory->insert((ele+i));
}
int posOfhead=findRoot(ele,n);//找头指针的位置
head->data=memory->getPos(posOfhead)->ch;
int left,right;
left=memory->getPos(posOfhead)->left;
right=memory->getPos(posOfhead)->right;
memory->remove(posOfhead);//删掉头指针
creat(memory,head,left,right);
preorder(head);
}
return 0;
}
...全文
120 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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