-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeTools.h
42 lines (34 loc) · 1.26 KB
/
treeTools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Created by abasiy on ۲۶/۱۱/۲۰۲۳.
//
#ifndef DATASTRUCTURE_TREETOOLS_H
#define DATASTRUCTURE_TREETOOLS_H
#include "BinaryTree.h"
#include "Tree.h"
#include "BinaryNode.h"
#include "TreeNode.h"
#include "LinkList.h"
/**
* Implementing sibling Tree.
* remake a Binary tree from a simple (or general) tree recursively.
* */
template <class T>
void remakeBinaryTreeFromSimpleTreeRecursive(BinaryNode<T> *binaryRoot, TreeNode<T> *simpleTreeRoot){
if (!simpleTreeRoot->isLeaf()) {
TreeNode<T> *leftSimpleChild = simpleTreeRoot->getChildren().getAt(0);
binaryRoot->setLeftChild(leftSimpleChild->getValue());
remakeBinaryTreeFromSimpleTreeRecursive(binaryRoot->getLeftChild(), leftSimpleChild);
}
TreeNode<T> *next_sibling = simpleTreeRoot->nextSibling();
if (next_sibling != nullptr) {
binaryRoot->setRightChild(next_sibling->getValue());
remakeBinaryTreeFromSimpleTreeRecursive(binaryRoot->getRightChild(), next_sibling);
}
}
template<class T>
BinaryTree<T> remakeBinaryTreeFromSimple(Tree<T> simple_tree){
BinaryTree<T> binary_tree(simple_tree.root->getValue());
remakeBinaryTreeFromSimpleTreeRecursive(binary_tree.root, simple_tree.root);
return binary_tree;
}
#endif //DATASTRUCTURE_TREETOOLS_H