石峻 2019-01-19 16:42 采纳率: 0%
浏览 314

C++ 二叉搜索树 根节点的值运行过程中改变了 代码很短

二叉搜索树的插入函数 对照和网上的一模一样 实际运行出了问题 根节点的值运行过程中改变了 。

#include <iostream>
#include <iomanip>
#define NULL 0
using namespace std;

struct Tree_Node
{
    int Information;
    Tree_Node *Parent;
    Tree_Node *Left_Child;
    Tree_Node *Right_Child;
    Tree_Node(int Informatio); 
}; 

Tree_Node::Tree_Node(int Informatio)
{
    Parent=NULL;
    Left_Child=NULL;
    Right_Child=NULL;
    Information=Informatio;
}

class Binary_Search_Tree
{
    public: 
    void Inorder(Tree_Node *Node);
    void Preorder();
    void Postorder();
    void Insert(Tree_Node NewNode);
    Binary_Search_Tree();
    void See();
    public:
    Tree_Node *RootNode;
};

void Binary_Search_Tree::See() // 查看RootNode变化情况。
{
    cout<<"RootNode:"<<RootNode->Information<<endl;
}

Binary_Search_Tree::Binary_Search_Tree(){RootNode=NULL;}

void Binary_Search_Tree::Insert(Tree_Node NewNode) //
{
    if(RootNode!=0){See();} //查看RootNode变化情况 
    Tree_Node *Trail=NULL;
    Tree_Node *Temp=RootNode;
    while(Temp!=NULL)
    {
        Trail=Temp;
        if(NewNode.Information<Temp->Information)
        {Temp=Temp->Left_Child;}
        else{Temp=Temp->Right_Child;} 
    }
    NewNode.Parent=Trail;
    if(Trail==NULL){RootNode=&NewNode;}
    else
    {
        if(NewNode.Information<Trail->Information)
        {Trail->Left_Child=&NewNode;}
        else{Trail->Right_Child=&NewNode;}  
    }
}

int main()
{
    Binary_Search_Tree T;
    Tree_Node t1(10);
    Tree_Node t2(39);
    Tree_Node t3(5);
    T.Insert(t1);
    T.See();
    T.Insert(t2);
    return 0;
}

输出结果 RootNode 10
RootNode 39

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 21:39
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序