与二叉树链接有关的问题 [英] Issue with binary tree linking

查看:100
本文介绍了与二叉树链接有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "whead.h"
#include <stdio.h>
#include <malloc.h>
#include <iostream>


#define MAX_TREE_SIZE 100


typedef char TElemType;
//typedef TElemType SqBiTree[MAX_TREE_SIZE];

typedef struct BiTreeNode {
    TElemType data;
    struct BiTreeNode *lchild;
    struct BiTreeNode *rchild;
}BiTreeNode, *BiTree;


BiTree tm;
Status visit(TElemType v) {
    printf("node==", v);
    printf("\n");
    return OK;
}

Status createBiTreePre(BiTree t) {
    char ch;
    printf("Input char :");
    scanf("%c" ,&ch);
    printf("\n");
    if(ch == ' ')
        t = NULL;
    else {
        if(!(t = (BiTreeNode*)malloc(sizeof(TElemType))))
            exit(OVERFLOW);
        t->data = ch;
        createBiTreePre(t->lchild);
        createBiTreePre(t->rchild);
    }
    return OK;
}

Status traversePre(BiTree t, Status (*visit)(TElemType e)) {
    if(t){
        if(t->data)
            if(traversePre(t->lchild, visit))
                if(traversePre(t->rchild,visit))
                    return OK;
        return ERROR;
    }
    else
        return OK;
}

#include "BinaryTree.h"

void main() {   
    createBiTreePre(tm);
    traversePre(tm, visit);
}


问题是,我在调用createBiTreePre(t -> lchild)时第一次无法输入任何内容.
但是我可以第二次这样做.再说一次,我第三次不能这样做……为什么?


Problem is, I cann''t type anything at first time while calling the createBiTreePre(t -> lchild).
But I can do that at second time. Then again, I cann''t do that at 3rd time....... Why ?

推荐答案

if(!(t = (BiTreeNode*)malloc(sizeof(TElemType))))


您分配大小为TElemType且类型定义为char的内存,然后将其用作指向BiTreeNode的指针,因此您将立即开始破坏堆.


You allocate memory of size TElemType which is typedef''ed to char and then use it as a pointer to a BiTreeNode, so you will immediately start corrupting your heap.


因为系统-stdin的缓冲区

第一次在DOS中键入此类命令,然后在DOS命令末尾按[Enter]键

because system-Buffer of stdin

first Time you type such command in DOS, and you press [Enter] key at end of DOS-command

1.exe //assume your program called "1.exe"


但是问题来了,Enter键的值仍然留在了SyS-buffer-input中.


but problem comes, the Enter-key''s value still has been left in SyS-buffer-input.

printf("Input char :");
scanf("%c" ,&ch); // first time receive Enter-value from Buffer
printf("\n");


Scanf()将从上次输入的system-Buffer接收Enter值.因此scanf()在输入之前已经执行.
并创建了leftchild-Node.

和代码继续执行以创建下一个左孩子

第二次


Scanf() will receive the Enter-value from system-Buffer,which is inputed last time. so scanf() already executed before you wanted input.
and created leftchild-Node.

and code continue execute to create the next left-child

The Second time

printf("Input char :");
 
//because the last time the enter-value was got by last scanf. 
//the system-buffer was cleared. this time scanf() is waiting for you input
 
scanf("%c" ,&ch);
printf("\n");


解决.您可以在调用scanf之前添加getch(),它可能会收到最后一次输入时遗留的Enter键.

或者您可以调用fflush(stdin),它可以清除stdin的系统缓冲区.

就像下面的代码


The solve. you could add a getch() before call scanf, it could receive Enter-key which has been left at last Inputing

or you could call fflush(stdin), it could clear system-buffer of stdin.

just like following code

printf("Input char :") ;
getch() ;
scanf("%c",&ch) ;
printf("\n") ;


这篇关于与二叉树链接有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆