Traversing a binary tree to list all permutations of Fibonacci values

I continue to work on this project as an interest and keep coming back to it ...

What I'm trying to create is an algorithm for listing the sets of values ​​of a binary Fibonacci value tree:

Fibonacci tree values

An algorithm that I would use to print permutations of this tree:

  • The print root value (Result: ([root 0] = 5))
  • go down left [left 1]
  • Type the new left node [left 1] and the right number sibling node (Result: ([left 1] 3, [right 1] 2))
  • If the right brother node [right 1] has child nodes, go right node [right 1], listing its values ​​with it sibling left node [left 1] (Result: [left 1] 3, [left 3] 1, [right 3] 1)
  • Go down to the left child [left 2] as step 2
  • node [left 2] 2 sibling node [right 2] 1 [left 1]; node. , , , , : ([left 2] 2, [right 2] 1, [right 1] 2), ([left 2] 2, [right 2] 1, [left 3] 1, [right 3] 1))
  • , ,

root. , - .

, , , :

([root 0] 5), ([left 1] 3, [right 1] 2), ([left 1] 3, [left 3] 1, [right 3] 1), ([left 2] 2, [right 2] 1, [right 1] 2), ([left 2] 2, [right 2] 1, [left 3] 1, [right 3] 1)

, , . , , , .

- ? .

`FibTree`` Class`:

FibTree ( ):

class FibTree {

public:
    class Node {
    public:
    int data;
        Node const* left;
        Node const* right;
        Node const* parent;
        int n;
        int level;
        int index;

        Node (void);

    };

    Node const* root; // 'root' pointer to constant Node
    FibTree (int);
    Node const* getRoot(void);

    void startWriteSets(Node const* root); // Write all sets of tree

private:
    static Node* buildTree( int n, int level = 0, int i = 1, Node* parent = NULL );
    // Used by startWriteSets
    void writeSets(std::vector<Node const*> &setsList, Node const* cur);

CPP FibTree ( ):

// FibTree Constructor
FibTree::FibTree(int n) {
    this->root = buildTree( n );
};

// Getters
FibTree::Node const* FibTree::getRoot(void) {
    return this->root;
}

// Write sets of tree
void FibTree::startWriteSets(Node const* root) {
    std::vector<Node const*> setsList;
    std::cout << root->data;
    writeSets(setsList, root);
}

// Private FibTree methods
FibTree::Node* FibTree::buildTree( int n, int level, int i, Node* parent ) { // Build Tree structure
    Node* thisNode = new Node();
    thisNode->n = n;
    thisNode->level = level;
    thisNode->index = i;
    thisNode->parent = parent;
    if (n < 2) {
         thisNode->left = NULL;
         thisNode->right = NULL;
         thisNode->data = n;
         return thisNode;
    } else {
         thisNode->left = buildTree( n - 1 , level + 1, i*2, thisNode );
         thisNode->right = buildTree( n - 2, level + 1, i*2+1, thisNode );
         thisNode->data = thisNode->left->data + thisNode->right->data;
         return thisNode;
    }
}

void FibTree::writeSets(std::vector<Node const*> &setsList, Node const* cur) {
    std::vector<Node const*>::iterator nodeIterator;

    // Displays all preceding left values
    for (nodeIterator = setsList.begin();
        nodeIterator != setsList.end(); nodeIterator++) {
        std::cout << *nodeIterator->data;
    }   
    std::cout << cur->left->data;
    std::cout << cur->right->data;

    setsList.push_back(cur->left);
    writeSets(setsList,cur->right);
    setsList.pop_back();
}


// FibTree Node constructor
FibTree::Node::Node()
: data( 0 ),
left( NULL ),
right( NULL ),
parent( NULL ),
n( 0 ),
level( 0 ),
index( 0 )
{
};

std::cout << *nodeIterator->data; void FibTree::writeSets:

_error: '' * nodeIterator. __gnu_cxx:: __ normal_iterator < _Iterator, _Container > :: operator- > _Iterator = const FibTree:: Node **, Container = std::vector > ', non-class' const FibTree:: Node *

, , !

+4
3

node. Node :

Node(int val,Node* left,Node* right)

:

Node* Tree(int n){
    return new Node(fib(n),Tree(n-1),Tree(n-2));
}

, , .

, , , 5 = 3 + 2. 3 2 . 3 , 5.

vector < vector <int> > SetOfSets(Node * root){
    vector < vector <int> > leftSets = SetOfSets(root.left);
    vector < vector <int> > rightSets = SetOfSets(root.right);
    vector < vector <int> > ans;
    for(int i=0;i<leftSets.size();i++){
        for(int j=0;i<rightSets.size();j++){
            ans.push_back(leftSets[i].insert(leftSet[i].end(),rightSet[j].begin(),rightSet[j].end()));
        }
    }
    return ans;
}

(root.val == 1), .

+1

, , , B-Tree. . , . , , , [left 1] = 3.

( ). ....

void start(void) {
  vector<NODE*> list;
  cout << head->val;
  visit(list, head);
}

void visit(vector<NODE*> &list, NODE *cur) {
  // Displays all preceding left values.
  for (vector<NODE*> it = list.begin(); it != list.end(); it++) {
    cout << *it->val;
  }
  cout << cur->left->val;
  cout << cur->right->val;

  list.push_back(cur->left);
  visit(list, cur->right);
  list.pop_back();
}

, . , .

+1

given the structure of node:

struct SNode
{
    int m_iValue;
    SNode* m_psLeft;
    SNode* m_psRight;
};

I think the closest thing to what you want:

//returns true if there are still not visited children
bool Traverse(SNode* psNode, int iDepth)
{
    if(iDepth > 0)
    {
        bool bLeftRes;
        if(psNode->m_sLeft != NULL)
        {
            bLeftResult = Traverse(psNode->m_psLeft, iDepth - 1);
        }
        else
        {
            bLeftResult = false;
        }
        bool bRightRes;
        if(psNode->m_sLeft != NULL)
        {
            bRightRes =  Traverse(psNode->m_psRight, iDepth - 1);
        }
        else
        {
            bRightRes = false;
        }
        return bLeftRes || bRightRes;
    }
    else
    {
        printf("%d ", psNode->m_iValue);
        return psNode->m_psLeft || psNode->m_psRight;
    }
}

so that the bypass call looks like this:

int i = 0;
printf("(");
while(Traverse(psRoot, i))
{
    printf(")\n(");
    ++i;
}
printf(")");

the output will be with your tree: (5) (3 2) (2 1 1 1)

0
source

Source: https://habr.com/ru/post/1540947/


All Articles