简体   繁体   English

意外的树遍历

[英]Unexpected Pre-order traversal of Tree

When I am executing my code, I am getting the wrong result for a pre-order traversal of my binary tree. 当我执行我的代码时,对于遍历我的二进制树,我得到了错误的结果。 I am expecting that my program print this pre-order 5 -> 4 -> 3 -> 2-> 6 -> 7 -> 8. I am not able to point out my mistake in code. 我期望我的程序按此顺序打印5-> 4-> 3-> 2-> 6-> 7->8。我无法指出我的代码错误。 Please tell me where I have made a mistake. 请告诉我我哪里做错了。

This is my input and output (a sample run): 这是我的输入和输出(示例运行):

ENTER THE NODE DATA
5
ENTER YOUR CHOICE
ENTER THE NODE DATA
4
ENTER YOUR CHOICE
ENTER THE NODE DATA
6
ENTER YOUR CHOICE
ENTER THE NODE DATA
3
ENTER YOUR CHOICE
ENTER THE NODE DATA
7
ENTER YOUR CHOICE
ENTER THE NODE DATA
2
ENTER YOUR CHOICE
ENTER THE NODE DATA
8
ENTER YOUR CHOICE
5 -> 4 -> 3 -> 2 -> 8 -> 7 -> 6 ->

This is the code for creating the binary tree and printing a pre-order traversal of the tree. 这是用于创建二叉树并打印该树的预遍历的代码。

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct node {
               int data;
               struct node *right;
               struct node *left;
            }  *tmp  = NULL;

typedef struct node NODE;

  NODE *root = NULL;
  NODE *child, *new_node;

 void preorder(NODE *t)
{
     if(t != NULL)
     {
            printf("%d -> ",t->data);
            preorder(t->left);
            preorder(t->right);
     }//IF
 } 
  int main ()
  {
      char choice;
      do{
           new_node = (NODE *)malloc(sizeof(NODE));
           printf("ENTER THE NODE DATA \n");
           scanf("%d",&new_node -> data);

           new_node -> right = NULL;
           new_node -> left  = NULL;

           if(root == NULL)
           {
                   root = new_node;
                   tmp  = new_node;
           }//IF

           else
           {
               child = new_node;

               while(1)
               {
                       if(child -> data < tmp -> data)
                       {
                             if(tmp -> left == NULL)
                              {
                                  tmp -> left = new_node;
                                  break;  
                              }//if2  
                              tmp  = tmp -> left;
                       }//if1

                       if(child -> data > tmp -> data)
                       {
                           if(tmp -> right == NULL)
                           {
                               tmp -> right = new_node;
                               break;
                           }//if4
                           tmp = tmp -> right;
                       }//if3
               }//while
           }//else
           printf("\n ENTER YOUR CHOICE \n");
           choice = getch();
        }//do
         while(choice != 'n');
         preorder(root);
         getch();
         return 0;
  }//main

You didn't reset tmp to be the root in every iteration. 您没有将tmp重置为每次迭代的根。

Note that there is no need for variable child at all, as its value is completely identical to that of new_node . 请注意,根本不需要变量child ,因为它的值与new_node值完全相同。

In do while add the following statement. 在做的同时添加以下语句。

else {
child=new_node;
tmp=root;
...
...
}

After every loop, tmp value will be changed. 在每个循环之后,tmp值将被更改。 We have to check the new value with the root itself. 我们必须用根本身检查新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二叉树的后序/前序遍历 - Post-order/Pre-order Traversal of Binary Tree C递归遍历C二叉搜索树 - C Binary Search Tree pre-order traversal with recursion 来自前序遍历的二叉搜索树和关于左右节点的信息 - Binary search tree from pre-order traversal and information about left and right nodes 我的二进制搜索树的预遍历代码正在工作,但是每个元素都是指向结构的指针的堆栈如何工作? - My code for pre-order traversal of Binary Search Tree is working but how is the stack whose each element is a pointer to the structure working? 给定有序遍历和前序遍历,生成后序遍历 - Generate the post-order traversal given the in-order and pre-order traversal BST预订删除 - BST pre-order deletion 在C中没有递归的二叉搜索树的顺序和前序遍历中? - In Order and Pre Order Traversal of Binary Search Tree without recursion in C? 我试图实现一棵二叉树并按预定顺序遍历它,但它仅在显示 function 中显示 0,并且创建 function 正在工作 - I tried to implement a binary tree and traverse it in pre-order but it is showing 0 only in display function, and the create function is working 试图从存储在二进制文件中的预序遍历中构建一棵树? - Trying to build a tree out of a pre order traversal which is stored in a binary file? 二叉树双阶遍历 - Binary Tree Double Order Traversal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM