简体   繁体   中英

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?

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).

PS: For calculating tree depth you can use one of three traversal methods .

  1. In each recursion call increase the depth variable
  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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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