V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kirara
V2EX  ›  程序员

谁来帮我看看这个排序二叉树怎么跑不起来?

  •  
  •   kirara · 2019-06-22 18:41:37 +08:00 · 949 次点击
    这是一个创建于 1742 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include<iostream>
    using namespace std;
    typedef int ElemType;
    typedef struct TreeNode{
    	ElemType data;
    	struct TreeNode *l, *r;
    }TreeNode, *BSTree;
    void insert(BSTree *ST, int data)
    {
    	BSTree p;
    	if(*ST == NULL)
    	{
    		p = new TreeNode;
    		p->data = data;
    		p->l = NULL;
    		p->r = NULL;
    		*ST = p;
    	}
    	else if(data<(*ST)->data)
    	insert(&(*ST)->l, data);
    	else if(data>(*ST)->data)
    	insert(&(*ST)->r, data);
    }
    void view(BSTree *ST)
    {
    	view(&(*ST)->l);
    	cout<<(*ST)->data<<" ";
    	view(&(*ST)->l);
    }
    int main(void)
    {
    	int data;
    	BSTree ST = NULL;
    	for(int i=0; i<5; i++)
    	{
    		cin>>data;
    		insert(&ST, data);
    	}
    	view(&ST);
    	return 0;
    }
    

    我找了好久硬是没找出问题来。

    4 条回复    2019-06-24 23:06:37 +08:00
    tsunli
        1
    tsunli  
       2019-06-24 20:12:52 +08:00
    void view(BSTree *ST)
    {
    if( !ST) return ;

    view(&(*ST)->l);
    cout<<(*ST)->data<<" ";
    view(&(*ST)->l);
    }
    tsunli
        2
    tsunli  
       2019-06-24 20:14:01 +08:00
    f( ! *ST) return ;
    tsunli
        3
    tsunli  
       2019-06-24 20:30:01 +08:00   ❤️ 1
    cout<<(*ST)->data<<" ";
    view(&(*ST)->r);
    kirara
        4
    kirara  
    OP
       2019-06-24 23:06:37 +08:00
    @tsunli 感谢大佬相助!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2869 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:10 · PVG 21:10 · LAX 06:10 · JFK 09:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.