简体   繁体   English

C ++二进制搜索树打印和深度计算

[英]C++ Binary Search Tree printing and depth calc

I have created binary search tree. 我已经创建了二进制搜索树。 My functions can add, delete, and find nodes with numbers. 我的函数可以添加,删除和查找带数字的节点。 All of that functions is working fine. 所有这些功能都可以正常工作。 Can You help me with two functions: 1) Printing BST 2) Calculating depth of BST? 您能帮我两个功能吗:1)打印BST 2)计算BST的深度?

I have no idea how to do this in a quick and easy way. 我不知道如何快速简便地执行此操作。 Depth i calculating during adding new Nodes but I want to have function only for doing that. 我在添加新节点的过程中计算深度,但是我只想具有执行此操作的功能。

class Node{
    public:
shared_ptr<Node> right;
shared_ptr<Node> left;
int number;
Node(int number)
{
    this->number=number;
    right=nullptr;
    left=nullptr;
}
~Node()
{
    cout<<"Deleted"<<" "<<number<<endl;
}
};
class BT
{
    public:
    shared_ptr<Node> root;
    int deep;
    BT(int num)
    {
      deep=0;
      root=make_shared<Node>(num);
    }

    void add_number(int num)
    {
        shared_ptr<Node> new_num=make_shared<Node>(num);
        shared_ptr<Node> tmp=root;
        int tmp_deep=0;
        while(tmp!=nullptr)
        {
            tmp_deep++;

            if(tmp->number>num)
            {
            if (tmp->left==nullptr)
            {
                tmp->left=new_num;
                break;
            }

            else
             tmp=tmp->left;
            }

            else if (tmp->number<num)
            {
                if (tmp->right==nullptr)
                 {
                tmp->right=new_num;
                break;
            }
            else
               tmp=tmp->right;
            }


        }
        tmp.reset();
        if (tmp_deep>deep)
            deep=tmp_deep;

    }

    shared_ptr<Node> find_node(int num)
    {
        shared_ptr<Node> tmp=root;

        while (tmp!=nullptr && tmp->number!=num)
        {
           if (tmp->number>num)
            tmp=tmp->left;
            else if (tmp->number<num)
                tmp=tmp->right;

        }

        if (tmp==nullptr)
        {
            cout<<"Not found";
            return nullptr;
        }
        else
            return tmp;

    }

    void delete_ (int num)
    {
        shared_ptr<Node> tmp=root;
        shared_ptr<Node> previous=root;


          while (tmp!=nullptr && tmp->number!=num)
        {
           if (tmp->number>num)
           {
               previous=tmp;
                tmp=tmp->left;

           }

            else if (tmp->number<num)
            {
                previous=tmp;
                tmp=tmp->right;
            }


        }

        if (tmp==nullptr)
        {
            cout<<"Not found";
        }
        else
        {

            if(tmp->left==nullptr && tmp->right==nullptr)
            {

                if (previous->number>tmp->number)
                previous->left=nullptr;
                else
                previous->right=nullptr;


                tmp.reset();
            }
            else if (tmp->left==nullptr && tmp->right!=nullptr)
            {
                if(tmp->right!=nullptr)
                {
                 previous->right=tmp->right;
                }
                else
                    previous->right=tmp->left;
                tmp.reset();
            }
            else if (tmp->left!=nullptr && tmp->right==nullptr)
            {
                if(tmp->right!=nullptr)
                {
                 previous->left=tmp->right;
                }
                else
                    previous->left=tmp->left;
                tmp.reset();
            }
            else if (tmp->left!=nullptr && tmp->right!=nullptr)
            {

                shared_ptr<Node> tmp_left=tmp->right;
                shared_ptr<Node> prev_left=tmp->right;
               while (tmp_left->left!=nullptr)
               {

                        //prev_left=tmp_left;
                        tmp_left=tmp_left->left;


                }
                if (tmp->number<previous->number)
                    previous->left=tmp_left;
                else
                    previous->right=tmp_left;

            prev_left->left=tmp_left->right;
            tmp_left->left=tmp->left;
            tmp_left->right=tmp->right;
            tmp.reset();

            }




        }

        void show_bt()
        {

        }
        void calc_depth()
        {

        }

    }




};

Both of calculating depth and printing can done using tree traversal . 深度计算和打印都可以使用树遍历来完成。 Moreover, tree traversal has O(n) time complexity( n is number of nodes in the tree). 此外,树遍历具有O(n)时间复杂度( n是树中的节点数)。

PS: For calculating tree depth you can use one of three traversal methods . PS:要计算树的深度,可以使用三种遍历方法之一

  1. In each recursion call increase the depth variable 在每个递归调用中,增加depth变量
  2. After that decrease it and 之后减少它和
  3. Save total maximum value(before decreasing it) 保存总最大值(减小前)

This exercise is something every programmer has to do, to learn recursion . 学习递归 ,每个程序员都必须做此练习。 This can also be done by iteration , but that requires to build your stack 也可以通过迭代来完成,但这需要构建堆栈

For recursion, a function has to be created, which calls itself in order to "calculate" a result. 为了进行递归,必须创建一个函数,该函数调用自身以“计算”结果。

You have to think, how the end result can be "calculated" from smaller results. 您必须考虑如何从较小的结果“计算”最终结果。

Let's look at the depth calculation. 让我们看一下深度计算。

This is on a tree. 这是在一棵树上。 Which is constructed from nodes. 从节点构造的。

So how can we calculate something on the nodes to get the end result? 那么,我们如何在节点上计算一些东西以获得最终结果呢?

Every node has a height which is 1 greater than the maximum of height of (the left subtree and, right subtree). 每个节点的高度比(左子树和右子树)的最大高度高1。 If there is no subtree we'll just say it has a height of zero. 如果没有子树,我们只说它的高度为零。

BTW: never look for the quick and easy in the beginning. 顺便说一句:永远不要一开始就寻求快速和轻松 Always the first step is: make it work. 第一步始终是:使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM