我创建了一个程序,根据预先排序和顺序遍历构建二叉树,并使用链接反转按顺序遍历二阶树,按顺序和后序。我的程序执行但由于某种原因,遍历不完整。以下是我的前序遍历功能:
void PRE_LINK_INVERT(struct BinaryTreeNode *Root)
{
struct BinaryTreeNode *father; struct BinaryTreeNode *curr_node; struct BinaryTreeNode *child;
if(Root == NULL){
return;
}
father = NULL;
curr_node = Root;
1: VISIT(curr_node);
child = curr_node->left_child;
if(child != NULL)
{
curr_node->left_child = father;
father = curr_node;
curr_node = child;
goto 1;
}
2: child = curr_node->right_child;
if(child != NULL)
{
curr_node->right_child = father;
curr_node->tag = 1;
father = curr_node;
curr_node = child;
goto 2;
}
3: if(father == NULL)
{
return;
}
else if(father->tag == 0)
{
child = father->left_child;
father->left_child = curr_node;
curr_node = father;
father = child;
goto 2;
}
else
{
child = father->right_child;
father->right_child = curr_node;
father->tag = 0;
curr_node = father;
father = child;
goto 3;
}
}
father
表示当前节点的前任,curr_node
是当前节点,child
表示当前节点的左子节点。此函数接受指针Root
,该指针指向二叉树的根节点。结构BinaryTreeNode
被声明:
struct BinaryTreeNode
{
char data;
int tag;
struct BinaryTreeNode * left_child;
struct BinaryTreeNode * right_child;
};
现在,当我使用该函数遍历具有char preorder[] = "ABCDE";
的二阶树进行前序遍历和char inorder[] = "CBADE";
的问题时,对于其前序遍历,出现的问题仅仅是ABC
没有别的。我很肯定问题不在于构造二叉树的函数,因为我在另一个程序中使用相同的函数来执行二叉树遍历的递归实现并且没有遇到任何问题。我应该修改这段代码的哪一部分?