关于二叉树遍历的问题
这其实是一道题目,题目描述如下:
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是配对使用的呀)
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;
}
...全文
相关推荐
二叉树遍历问题二叉树遍历问题
二叉树遍历在实际编程中有着广泛的应用,例如在编译器中解析语法树、构建搜索算法以及数据压缩等领域。熟练掌握这四种遍历方法有助于理解和解决涉及树结构的问题。
二叉树遍历问题-二叉树遍历问题
这些遍历方法为处理复杂的数据结构和问题提供了基础,例如在编译器中解析语法树、构建搜索引擎索引、解决游戏中的路径寻找问题等。
关于二叉树遍历求解问题
遍历二叉树是指按照特定顺序访问二叉树中的所有节点。本篇文章将详细介绍三种基本的二叉树遍历方法:前序遍历、中序遍历和后序遍历,并通过具体例子解释如何利用这些遍历策略解决求解二叉树的问题。1.
二叉树遍历问题-前序, 中序, 后序
二叉树遍历问题 二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉
python先序遍历二叉树问题
在实际应用中,先序遍历常用于复制或打印二叉树的结构,以及某些特定问题的求解。在Python中,我们可以用递归的方式来实现先序遍历。


新手乐园
