Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Make BinTree
//Title of this code #include <iostream> #include <vector> using namespace std; struct BinTree { int data; BinTree *left; BinTree *right; BinTree(int d, BinTree* l, BinTree* r): data(d), left(l), right(r) {} }; int depth(BinTree* root, int d) { if (!root) return d; return max(depth(root->left, d + 1), depth(root->right, d + 1)); } int maxDepth(int nodeCount) { int i = 0; while(nodeCount > 0) { nodeCount >>= 1; ++i; } return i; } void inorder(BinTree* root, vector<BinTree*>& t) { if (!root) return; inorder(root->left, t); cout << root->data << " "; t.push_back(root); inorder(root->right, t); } void BuildNew(vector<BinTree*>& t, BinTree *root, int begin, int end) { //return; if (begin >= end) return; int mid = (end - begin / 2) + begin; root = t[mid]; cout << root->data << " "; BuildNew(t, root->left, begin, mid - 1); BuildNew(t, root->right, mid + 1, end); } void print(vector<BinTree*>& t) { for (int i = 0; i < t.size(); ++i){ cout << t[i]->data << " "; t[i]->left = NULL; t[i]->right = NULL; } cout << endl; } int main() { BinTree *ggg = new BinTree(19, NULL, NULL); BinTree *gg = new BinTree(18, NULL, ggg); BinTree *ddd = new BinTree(1, NULL, NULL); BinTree *dd = new BinTree(2, ddd, NULL); BinTree *hh = new BinTree(13, NULL, NULL); BinTree *j = new BinTree(10, NULL, NULL); BinTree *h = new BinTree(14, hh, NULL); BinTree *g = new BinTree(17, NULL, gg); BinTree *f = new BinTree(12, j, h); BinTree *e = new BinTree(7, NULL, NULL); BinTree *d = new BinTree(3, dd, NULL); BinTree *c = new BinTree(15, f, g); BinTree *b = new BinTree(5, d, e); BinTree *root = new BinTree(9, b, c); vector<BinTree*> t; inorder(root, t); cout << endl; cout << depth(root, 0); cout << endl; print(t); cout << endl; BinTree *r = t[t.size() / 2]; BuildNew(t, r, 0, t.size() - 1); cout << depth(r, 0); //cout << maxDepth(13); }
run
|
edit
|
history
|
help
0
initializer_list example
abraham
cppPyPoly2
https://rextester.com/TXKE53925
Primality test Fermat primality test
New wall
Arithmetic
10 wizards-DFS_vector
GL interview
BubDoubArray