• 请不要在回答技术问题时复制粘贴 AI 生成的内容
kirara
V2EX  ›  程序员

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

  •  
  •   kirara · Jun 22, 2019 · 1802 views
    This topic created in 2541 days ago, the information mentioned may be changed or developed.
    #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 replies    2019-06-24 23:06:37 +08:00
    tsunli
        1
    tsunli  
       Jun 24, 2019
    void view(BSTree *ST)
    {
    if( !ST) return ;

    view(&(*ST)->l);
    cout<<(*ST)->data<<" ";
    view(&(*ST)->l);
    }
    tsunli
        2
    tsunli  
       Jun 24, 2019
    f( ! *ST) return ;
    tsunli
        3
    tsunli  
       Jun 24, 2019   ❤️ 1
    cout<<(*ST)->data<<" ";
    view(&(*ST)->r);
    kirara
        4
    kirara  
    OP
       Jun 24, 2019
    @tsunli 感谢大佬相助!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2864 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:48 · PVG 12:48 · LAX 21:48 · JFK 00:48
    ♥ Do have faith in what you're doing.