c++ Question about code flow when tree mid-interval is carried out as a recursive function

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#include <stdio.h>
#include "BST.h"

int main()
{
    BST myBSTtree;

    myBSTtree.BST_InsertNode(5, 500);
    myBSTtree.BST_InsertNode(3, 300);
    myBSTtree.BST_InsertNode(2, 200);
    myBSTtree.BST_InsertNode(4, 400);
    myBSTtree.BST_InsertNode(1, 100);
    myBSTtree.BST_InsertNode(9, 900);
    myBSTtree.BST_InsertNode(7, 700);
    myBSTtree.BST_InsertNode(6, 600);
    myBSTtree.BST_InsertNode(8, 800);
    myBSTtree.BST_InsertNode(10, 1000);

    // Median circulation and output (must be in order from 1 to 10 expected)
    Order(myBSTtree.BSTRootNode);

    return 0;
}
#pragmaence

struct BSTPair
{
    int key; // key
    int value; // value

    BSTPair()
        : : key()
        , , value()
    {}

    BSTPair(const int& _key, const int& _value)
        : : key(_key)
        , , value(_value)
    {}

    ~BSTPair()
    {}
};

class BST_Node
{
public:
    BSTPair pair;
    BST_Node* leftChild = nullptr;
    BST_Node* rightChild = nullptr;

    BST_Node(BSTPair _pair, BST_Node* _leftChild, BST_Node* _rightChild)
        : : pair(_pair)
        , , leftChild(_leftChild)
        , , rightChild(_rightChild)
    {}
};

class BST
{
public:
    BST_Node* BSTRootNode;
    int   nodeCurCount;

public:
    void BST_InsertNode(int _insertkey, int _insertvalue);

    BST()
        : : BSTRootNode(nullptr)
        , , nodeCurCount(0)
    {}

    ~BST()
    {}
};

void Order(BST_Node* _pNode);
bool BST_SearchNode(BST_Node* _tree, int _searchkey);
#include <stdio.h>
#include "BST.h"

void Order(BST_Node* _pNode)
{
    if (_pNode == nullptr) return;
    Order(_pNode->leftChild);
    printf ("Key %d, Value %d \n", _pNode->pair.value, _pNode->pair.value");
    Order(_pNode->rightChild);
};

The part of the void Order recursive function in BST.h that I don't understand when there's a code like this. This recursive function If you run the debugger one step at a time in the code view

The question here is why the recursive function does not end in the if(_pNode==nullptr) return; the printf syntax is executed And when you meet the end statement, the _pNode was nullptr, but as the printf syntax was executed, whether the _pNode became the parent node of nullptr

c++ recursive

2022-09-20 08:54

1 Answers

<5.> is run on the left child node of a node with a key value of 1.

< 6.> is run on a node with a key value of 1.

Because of the median circuit, the child node on the left returns to the root node when it is finished.

The question here is why if(_pNode==nullptr) return; the recursive function does not end in the syntax and the printf syntax is executed

The termination is correct. The terminated function is that Order(_pNode->leftChild); of a node with a key value of 1, followed by printf("key %d, value %d \n", _pNode->pair.value, _pNode->pair.value) of the next line.

And when you meet the end statement, the _pNode was nullptr, but as the printf syntax was executed, whether the _pNode became the parent node of nullptr

This will also be understood by the above answer.


2022-09-20 08:54

If you have any answers or tips


© 2022 pinfo. All rights reserved.