求教C++程序停止正常工作,是什么问题呢?

qq_40874183 2017-11-14 04:10:36
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

typedef struct node
{
char data; //数据域
struct node *lchild,*rchild; //左指针域,右指针域
}BTNode;


void createbitree(BTNode *&T)
{
char ch;
scanf("%c",ch);
if(ch==' ')
T=NULL;
else
{
T=new BTNode;
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
}



void PreOrder(BTNode *p) //按先序访问操作
{
if(p!=NULL)
{
printf("%c",p->data);
PreOrder(p->lchild); //按先序遍历左子树
PreOrder(p->rchild); //按先序遍历右子树
}
}



void InOrder(BTNode *p) //按中序访问操作
{
if(p!=NULL)
{
InOrder(p->lchild); //按中序遍历左子树
printf("%c",p->data);
InOrder(p->rchild); //按中序遍历右子树
}
}



void PostOrder(BTNode *p) //按后序访问操作
{
if(p!=NULL)
{
PostOrder(p->lchild); //按后序遍历左子树
PostOrder(p->rchild); //按后序遍历右子树
printf("%c",p->data);
}
}




void DispLeaf(BTNode *p)
{
if(p!=NULL)
{
if(p->lchild==NULL&&p->rchild==NULL)
printf("%c",p->data);
else
{
DispLeaf(p->lchild);
DispLeaf(p->rchild);
}
}
}




int BiTreeDepth (BTNode *p)
{
int ldep,rdep;
if(p==NULL)
return 0; //空树的高度为0
else
{
ldep=BiTreeDepth(p->lchild); //求左子树的高度为ldep
rdep=BiTreeDepth(p->lchild); //求右子树的高度为rdep
if(ldep>rdep)
return ldep+1;
else
return rdep+1;
}
}



int CountLeaf(BTNode *p) //二叉树中所有叶子的结点个数
{
int m;
if(p==NULL)
m=0;
else
if(p->lchild==NULL&&p->rchild==NULL)
m=1;
else
m=CountLeaf(p->lchild)+CountLeaf(p->rchild);
return m;
}

int Count(BTNode *p) //二叉树中所有结点个数
{
int m,n;
if(!p)
return 0;
if(!p->lchild&&p->rchild)
return 1;
else
{
m=Count(p->lchild);
n=Count(p->rchild);
return (m+n+1);
}
}



BTNode *SearchNode(BTNode *p,char x)
{
BTNode *b;
if(p==NULL)
return NULL; //查找失败
else if(p->data==x)
return p; //查找成功
else
{
b=SearchNode(p->lchild,x);
if(b!=NULL)
return b;
else
return SearchNode(p->rchild,x);
}
}




BTNode *DeleteLeft(BTNode *p)
{
BTNode *q;
if(p==NULL||p->lchild==NULL)
return NULL;
q=p->lchild;
p->lchild=NULL;
free (q);
return p;
}

BTNode *DeleteRight(BTNode *p)
{
BTNode *q;
if(p==NULL||p->rchild==NULL)
return NULL;
q=p->rchild;
p->rchild=NULL;
free (q);
return p;
}


int main()
{
int num,a,b,c,high=0,x,y,z;
BTNode *M,*T,*A,*B,*p;
printf("建立二叉树M:\n");
createbitree(M);
printf("请选择你想进行的操作;\n");
printf("1.遍历二叉树\n");
printf("2.输出一颗给定二叉树的所有叶子结点的值\n");
printf("3.求二叉树的高度\n");
printf("4.求二叉树的结点个数和叶子结点个数\n");
printf("5.查找值为x的结点\n");
printf("6.删除左,右子树\n");
printf("0.退出程序,不进行操作\n");
scanf("%d",&num);
switch (num)
{
case 1:printf("遍历二叉树\n");
printf("请选择如何遍历二叉树\n");
printf("11.先序遍历\n");
printf("12.中序遍历\n");
printf("13.后序遍历\n");
scanf("%d",&a);
switch(a)
{
case 11:printf("先序遍历\n");
PreOrder(p);
break;
case 12:printf("中序遍历\n");
InOrder(p);
break;
case 13:printf("后序遍历\n");
PostOrder(p);
break;
}
case 2: printf("输出一颗给定二叉树的所有叶子结点的值\n");
DispLeaf(p);
break;
case 3: printf("求二叉树的高度\n");
high=BiTreeDepth(p);
break;
case 4: printf("求二叉树的结点个数和叶子结点个数\n");
printf("请选择是求二叉树的结点个数还是叶子结点个数\n");
printf("41.二叉树结点个数\n");
printf("42叶子结点个数\n");
scanf("%d",&b);
switch(b)
{
case 41:printf(".二叉树结点个数\n");
x=Count(p);
break;
case 42:printf("叶子结点个数\n");
y=CountLeaf(p);
break;
}
case 5: printf("查找值为x的结点\n");
printf("请输入x:\n");
scanf("%c",&z);
T=SearchNode(p,z);
break;
case 6: printf("删除左,右子树\n");
printf("61.删除左子树\n");
printf("62.删除右子树\n");
scanf("%d",&c);
switch(c);
{
case 61:printf("删除左子树\n");
A=DeleteLeft(p);
break;
case 62:printf("删除右子树\n");
B=DeleteRight(p);
break;
}

case 0: printf("退出程序,不进行操作\n");
break;

}
return 0;
}





...全文
190 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-11-15
  • 打赏
  • 举报
回复
if(ch==' ')
T=NULL;
这句不是退出递归的条件吗? 输入空格,表示不在递归
赵4老师 2017-11-15
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <stack>
#include <queue>
#include <locale.h>
using namespace std;
typedef struct BiTNode {//二叉树结点
    char data;                      //数据
    struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int CreateBiTree(BiTree &T) {//按先序序列创建二叉树
    char data;
    scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
    if (data == '#') {
        T = NULL;
    } else {
        T = (BiTree)malloc(sizeof(BiTNode));
        T->data = data;         //生成根结点
        CreateBiTree(T->lchild);//构造左子树
        CreateBiTree(T->rchild);//构造右子树
    }
    return 0;
}
void Visit(BiTree T) {//输出
    if (T->data != '#') {
        printf("%c ",T->data);
    }
}
void PreOrder(BiTree T) {//先序遍历
    if (T != NULL) {
        Visit(T);               //访问根节点
        PreOrder(T->lchild);    //访问左子结点
        PreOrder(T->rchild);    //访问右子结点
    }
}
void InOrder(BiTree T) {//中序遍历
    if (T != NULL) {
        InOrder(T->lchild);     //访问左子结点
        Visit(T);               //访问根节点
        InOrder(T->rchild);     //访问右子结点
    }
}
void PostOrder(BiTree T) {//后序遍历
    if (T != NULL) {
        PostOrder(T->lchild);   //访问左子结点
        PostOrder(T->rchild);   //访问右子结点
        Visit(T);               //访问根节点
    }
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
    stack<BiTree> stack;
    BiTree p = T;//p是遍历指针
    while (p || !stack.empty()) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack.push(p);          //存入栈中
            printf("%c ",p->data);  //访问根节点
            p = p->lchild;          //遍历左子树
        } else {
            p = stack.top();        //退栈
            stack.pop();
            p = p->rchild;          //访问右子树
        }
    }
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
    stack<BiTree> stack;
    BiTree p = T;//p是遍历指针
    while (p || !stack.empty()) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack.push(p);          //存入栈中
            p = p->lchild;          //遍历左子树
        } else {
            p = stack.top();        //退栈,访问根节点
            printf("%c ",p->data);
            stack.pop();
            p = p->rchild;          //访问右子树
        }
    }
}

typedef struct BiTNodePost{
    BiTree biTree;
    char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
    stack<BiTreePost> stack;
    BiTree p = T;//p是遍历指针
    BiTreePost BT;
    while (p != NULL || !stack.empty()) {//栈不空或者p不空时循环
        while (p != NULL) {//遍历左子树
            BT = (BiTreePost)malloc(sizeof(BiTNodePost));
            BT->biTree = p;
            BT->tag = 'L';//访问过左子树
            stack.push(BT);
            p = p->lchild;
        }
        while (!stack.empty() && (stack.top())->tag == 'R') {//左右子树访问完毕访问根节点
            BT = stack.top();
            stack.pop();//退栈
            printf("%c ",BT->biTree->data);
        }
        if (!stack.empty()) {//遍历右子树
            BT = stack.top();
            BT->tag = 'R';//访问过右子树
            p = BT->biTree;
            p = p->rchild;
        }
    }
}

void LevelOrder(BiTree T) {//层次遍历
    if (T == NULL) return;
    BiTree p = T;
    queue<BiTree> queue;//队列
    queue.push(p);//根节点入队
    while (!queue.empty()) {    //队列不空循环
        p = queue.front();      //对头元素出队
        printf("%c ",p->data);  //访问p指向的结点
        queue.pop();            //退出队列
        if (p->lchild != NULL) {//左子树不空,将左子树入队
            queue.push(p->lchild);
        }
        if (p->rchild != NULL) {//右子树不空,将右子树入队
            queue.push(p->rchild);
        }
    }
}
int main() {
    BiTree T;

    setlocale(LC_ALL,"chs");
    CreateBiTree(T);

    printf("先序遍历        :");PreOrder  (T);printf("\n");
    printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
                                               printf("\n");
    printf("中序遍历        :");InOrder   (T);printf("\n");
    printf("中序遍历(非递归):");InOrder2  (T);printf("\n");
                                               printf("\n");
    printf("后序遍历        :");PostOrder (T);printf("\n");
    printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
                                               printf("\n");
    printf("层次遍历        :");LevelOrder(T);printf("\n");

    return 0;
}
//ABC##DE#G##F###
//先序遍历        :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历        :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历        :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历        :A B C D E F G
//

///       A
///      /
///     B
///    / \
///   C   D
///      / \
///     E   F
///      \
///       G
赵4老师 2017-11-15
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 “给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
CT8100 2017-11-14
  • 打赏
  • 举报
回复
没有具体满足条件用eof
CT8100 2017-11-14
  • 打赏
  • 举报
回复
嘛-goto用法很简单、但是一般不提倡使用、最好的方法放到函数中用return 方便简单
qq_40874183 2017-11-14
  • 打赏
  • 举报
回复
我好想谢谢你,可是我不会用goto语句,我们的题目又没有一个具体的满足条件,绝望
destory27 2017-11-14
  • 打赏
  • 举报
回复
#include "head.h" typedef int DataType; typedef struct tree{ DataType num; struct tree *left_child; struct tree *right_child; }node; node * init_tree(void) { node *root = (node *)malloc(sizeof(node)); (node *)memset(root, 0x00, sizeof(node)); root->left_child = NULL; root->right_child = NULL; return root; } void insert_tree(node **root) { static node *last_node = NULL; if(*root != NULL){ fprintf(stderr, "%s\012", "Here is not empty !"); return; } node *rt = (node *)calloc(1, sizeof(node)); *root = rt; if(!last_node){ system("color 0C"); fprintf(stderr, "%s\012", "\"The data inserted into the root node .\""); Sleep(2000); system("color 0A"); } printf("Enter to insert data :"); while(scanf("%d", &rt->num) == 0){ fflush(stdin); fprintf(stderr, "%s", "Illegal input and input again :"); } rt->left_child = NULL; rt->right_child = NULL; system("color 0C"); fprintf(stderr, "%s", "\n\t-----------------------------------------\n"); fprintf(stderr, "%s\n", "\t1.insert left_child 2.insert right_child\n\ 3.insert brother 0.end_insert"); fprintf(stderr, "%s", "\t-----------------------------------------\n\n"); Sleep(3000); system("color 0A"); int choose; lb: fprintf(stderr, "%s", "Please select a :"); while(scanf("%d", &choose) == 0){ fflush(stdin); fprintf(stderr, "%s", "Illegal input and input again :"); } if(choose > 3 || choose < 0){ fprintf(stderr, "%s", "There is no this option.\012"); goto lb; } switch(choose){ case 1:{ last_node = rt; insert_tree(&rt->left_child); }break; case 2:{ last_node = rt; insert_tree(&rt->right_child); }break; case 3:{ if(last_node != NULL){ if(last_node->left_child != NULL && last_node->right_child != NULL) fprintf(stderr, "%s", "Have no place to insert .\012"); else if(last_node->left_child == NULL) insert_tree(&last_node->left_child); else insert_tree(&last_node->right_child); } }break; case 4:{ }break; case 0: return; } } void preorder(node **p) { if(*p == NULL) return; fprintf(stderr, "%d ",(*p)->num); preorder(&(*p)->left_child); preorder(&(*p)->right_child); } void inorder(node **p) { if(*p == NULL) return; inorder(&(*p)->left_child); fprintf(stderr, "%d ",(*p)->num); inorder(&(*p)->right_child); } void postorder(node **p) { if(*p == NULL) return; inorder(&(*p)->left_child); inorder(&(*p)->right_child); fprintf(stderr, "%d ",(*p)->num); } void show(node **root) { node *p = *root; char order; fflush(stdin); lb2:system("color 0C"); fprintf(stderr, "%s", "\n\t--------------------------------------\n"); fprintf(stderr, "%s", "\tb:PreOrder i:Inorder a:PostOrder e:end\012"); fprintf(stderr, "%s", "\t--------------------------------------\n\n"); Sleep(3000); system("color 0A"); fprintf(stderr, "%s", "Choose the way to traverse :"); while(scanf("%c", &order) != '\n'){ fflush(stdin); switch(order){ case 'b': fprintf(stderr, "%s", "For the former sequence traversal results:"); preorder(&p); fprintf(stderr, "%c", '\012'); goto lb2; break; case 'i': fprintf(stderr, "%s", "In order to traverse the results :"); inorder(&p); fprintf(stderr, "%c", '\012'); goto lb2; break; case 'a': fprintf(stderr, "%s", "After the sequence traversal results for: \ "); postorder(&p); fprintf(stderr, "%c", '\012'); goto lb2; break; case 'e': return; default:{ fprintf(stderr, "%s", "There is no this option.\012"); goto lb2; } } } } node * creat_tree(void) { node *root = init_tree(); insert_tree(&root->left_child); show(&root->left_child); return root; }
CT8100 2017-11-14
  • 打赏
  • 举报
回复
引用 3 楼 qq_40874183 的回复:
非常谢谢你们,现在可以运行了,但是因为我用的是递归输入,能请问你们如何停止输入吗?
啥意思?停止输入么?对输入参数加一个判断如果是指定字符就退出。 退出方式:1.可以将输部分用函数单独拿出来,满足直接return 2。定义全局变量A,当满足条件A赋值1,依次跳出。 3.goto语句
qq_40874183 2017-11-14
  • 打赏
  • 举报
回复
非常谢谢你们,现在可以运行了,但是因为我用的是递归输入,能请问你们如何停止输入吗?
CT8100 2017-11-14
  • 打赏
  • 举报
回复
scanf("%c",ch);
改成 scanf("%c",&ch);
FoolCarpe 2017-11-14
  • 打赏
  • 举报
回复
先解决&ch,然后调试下能否运行。
scanf("%c", &ch);

33,311

社区成员

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

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