Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not duplicate code for printing tree

I have a function that prints tree nodes from left to right.

void PrintTree()
{
...
Print(curentNode);
...
}

But now I want to add a function that prints nodes that meet some condition. For example, print only such nodes, strings of which start with a given string. So it would look like

void PrintTreeByCondition(string a)
{
...
if(IsPrefix(a,curentNode->stringVar))
      Print(curentNode);
...
}

But then I have two functions with the same code, with the difference in one line. How would I avoid code duplication here?

UPD: Traverse code:

void BinTree::TraverseTree()
{
    std::stack<TreeNode*> s;
    s.push(root);
    TreeNode* curentNode = s.top();
    while (curentNode != nullptr|| s.empty() == false)
    {
        while (curentNode != nullptr)
        {
            s.push(curentNode);
            curentNode = curentNode->GetLeft();
        }

        curentNode = s.top();
        s.pop();

        // do stuff

        curentNode = curentNode->GetRight();
    }
}
like image 504
IvanIvan Avatar asked Jan 25 '23 13:01

IvanIvan


1 Answers

The C++ ways is to create an iterator for the traversal and use that for printing as well as filtering or other uses. Traversing a tree can probably be done in a bidirectional fashion which would allow plenty of algorithms to become accessible for the tree, too. In fact, the ordered associative containers (e.g., std::map, std::set) are normally implemented as [balanced] binary trees and their iterators do an in-order traversal.

The details of iteration do depend on the tree representation. The associative containers are normally implemented with a parent pointer. If there is no such pointer each iterator will need to maintain suitable space to represent the path back to the root (the standard library containers can't do that because they have requirements for iterator stability which prevents that). The slightly odd thing about traversal using iterators is that doesn't lend itself directly to an implementation using recursion. In return, control over the iteration is given to the user of the iterators.

like image 112
Dietmar Kühl Avatar answered Jan 29 '23 21:01

Dietmar Kühl