preorder tree construction
This commit is contained in:
102
puzzles/interviews/training/preorder_tree_construction.cpp
Normal file
102
puzzles/interviews/training/preorder_tree_construction.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
struct node {
|
||||
node * root = nullptr;
|
||||
node * left = nullptr;
|
||||
node * right = nullptr;
|
||||
double data = 0;
|
||||
|
||||
node( double d, node * r = nullptr )
|
||||
: root(r)
|
||||
, data(d)
|
||||
{}
|
||||
};
|
||||
|
||||
node * prefix_to_tree( const std::vector<double>& v ){
|
||||
|
||||
if ( !v.size() ){
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
node * root = new node(v[0]);
|
||||
node * current = root;
|
||||
node * max_node = nullptr;
|
||||
|
||||
for ( int i = 1; i < v.size(); ) {
|
||||
if ( v[i] < current->data ){
|
||||
max_node = current;
|
||||
current = new node(v[i++],current);
|
||||
current->root->left = current;
|
||||
} else if ( !max_node
|
||||
|| v[i] < max_node->data ){
|
||||
current = new node(v[i++],current);
|
||||
current->root->right = current;
|
||||
} else if ( current->root ){
|
||||
current = current->root;
|
||||
while ( max_node
|
||||
&& max_node->data <= current->data ) {
|
||||
node * tmp = max_node;
|
||||
max_node = max_node->root;
|
||||
if ( max_node && tmp == max_node->left ){
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
std::cout << "error" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
if ( !current || !root ) {
|
||||
std::cout << "errrrrrror" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << root << std::endl;
|
||||
return root;
|
||||
}
|
||||
|
||||
void print( node * root, const std::string& level = "" ){
|
||||
if ( root ) {
|
||||
print( root->left, level + " ");
|
||||
std::cout << level << "- " << root->data << std::endl;
|
||||
print( root->right, level + " ");
|
||||
}
|
||||
}
|
||||
|
||||
void traverse( node * root, std::vector<double>& v ){
|
||||
if ( root ) {
|
||||
v.push_back(root->data);
|
||||
traverse(root->left, v);
|
||||
traverse(root->right, v);
|
||||
}
|
||||
}
|
||||
|
||||
void test( const std::vector<double>& v ){
|
||||
auto r = prefix_to_tree( v );
|
||||
//print( r );
|
||||
std::vector<double> w;
|
||||
traverse( r, w );
|
||||
if ( v.size() != w.size() ) {
|
||||
std::cout << "v.size() != w.size() => " << v.size() << " != " << w.size() << std::endl;
|
||||
}
|
||||
for (int i = 0; i < v.size(); ++i ){
|
||||
if (v[i]!=w[i]){
|
||||
std::cout << "error at index " << i << " -> "<< v[i] << " != " << w[i] << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main( void ) {
|
||||
|
||||
test( {1} );
|
||||
test( {2, 1, 3} );
|
||||
test( {4, 2, 1, 3, 6, 5, 7} );
|
||||
test( {8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
|
||||
test( {8, 4, 2, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
|
||||
test( {8, 4, 6, 5, 7, 12, 10, 9, 14, 13, 15} );
|
||||
test( {8, 12, 10, 9, 11, 14, 13, } );
|
||||
test( {8, 4, 2, 1, 6, 12, 10, 11, 14, 15} );
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user