Created
June 8, 2017 23:07
-
-
Save PiotrWegrzyn/b787fa866501b86d645177f8bb4667c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <time.h> | |
#include <stdlib.h> | |
//https://gist.github.com/Brejw15/1a1b8cb682577264493098ae90d7e511 | |
//http://eduinf.waw.pl/inf/alg/001_search/0114.php | |
using namespace std; | |
struct TreeNode{ | |
TreeNode *parent = NULL; | |
TreeNode *left = NULL; | |
TreeNode *right = NULL; | |
int val = NULL; | |
}; | |
void grow(TreeNode *& root, int nval){ | |
TreeNode * nnode = new TreeNode; | |
nnode->val = nval; | |
nnode->parent = root; | |
if (root == NULL){ | |
nnode->parent = NULL; | |
root = nnode; | |
return; | |
} | |
else if (root->val > nval) { | |
grow(root->left, nval); | |
} | |
else{ | |
grow(root->right, nval); | |
} | |
} | |
void ShowTree(TreeNode *& node){ | |
if (node){ | |
ShowTree(node->left); | |
cout << node->val << " "; | |
ShowTree(node->right); | |
} | |
} | |
void ShowBigTree(TreeNode *& node){ | |
if (node){ | |
ShowTree(node->right); | |
cout << node->val << " "; | |
ShowTree(node->left); | |
} | |
} | |
void removeNode(TreeNode *& root){ | |
if (root){ | |
TreeNode * tmp = new TreeNode; | |
tmp = root->parent; | |
if (tmp){ | |
if (tmp->left == root)tmp->left = NULL; | |
else tmp->right = NULL; | |
} | |
} | |
} | |
void Autumn(TreeNode *& root){ | |
if (root){ | |
Autumn(root->left); | |
if (root->left == NULL && root->right == NULL){ | |
removeNode(root); | |
// cout << root->val << " "; | |
} | |
Autumn(root->right); | |
} | |
} | |
int main(){ | |
srand((time(NULL))); | |
TreeNode * bonsai = new TreeNode; | |
grow(bonsai, 10); | |
grow(bonsai, 11); | |
grow(bonsai, 1); | |
grow(bonsai, 5); | |
grow(bonsai, 100); | |
grow(bonsai, 2); | |
grow(bonsai, 3); | |
grow(bonsai, 5); | |
grow(bonsai, 10); | |
grow(bonsai, 10); | |
grow(bonsai, 15); | |
grow(bonsai, 6); | |
grow(bonsai, 8); | |
grow(bonsai, 18); | |
grow(bonsai, 12); | |
cout << "Drzewko Bonsai:"<<endl; | |
ShowTree(bonsai); | |
TreeNode * brzoza = new TreeNode; | |
for (int i = 0; i < 40; i++){ | |
grow(brzoza, rand() % 100); | |
} | |
cout << endl<<"Drzewko Brzoza:" << endl; | |
ShowTree(brzoza); | |
cout << endl << endl << endl; | |
Autumn(brzoza); | |
cout <<endl << "Przyszla jesien:"<<endl; | |
ShowTree(brzoza); | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment