Untitled
unknown
plain_text
2 years ago
3.5 kB
4
Indexable
#include <iostream> using namespace std; struct Node { int info; Node* left; Node* right; }; Node* root; class Tree { public: void Empty_Tree() // create tree { root = NULL; } void CreatNode(Node*& p, int x) { if (p == NULL) { Node* p = new Node; p->info = x; p->left = NULL; p->right = NULL; } else { if (p->info > x) { return CreatNode(p->left, x); } else if (p->info < x) { return CreatNode(p->right, x); } else if (p->info == x) { return; } } } void ProcessLNR(Node* p) { if (p != NULL) { ProcessLNR(p->left); cout << p->info << " "; ProcessLNR(p->right); } } void Input(int& n) { root = NULL; cout << " Input number ele " << endl; cin >> n; cout << " Input ele " << endl; for (int i = 0; i < n; i++) { int num; cin >> num; CreatNode(root, num); } } void min_element(Node* p) { while (p->left != NULL) { p = p->left; } cout << " Mini " << p->info << endl; } }; void Process_List(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } int find(Node* root, int t, int l) { if (!root)return 0; if (root->info == t) return l; int x = find(root->left, t, l + 1); if (x != 0) return x; x = find(root->right, t, l + 1); return x; } int getLevel(struct Node* node, int target) { if (!node)return 0; int l = 1; return find(node, target, l); } int main() { Tree sorted_arr_tree; int arr_n; cout << " Input num of ele in arr"; cin >> arr_n; int* arr = new int[arr_n]; cout << " Input ele of arr" << endl; for (int i = 0; i < arr_n; i++) { cin >> arr[i]; } Process_List(arr, arr_n); int middle = int(arr_n / 2); cout << middle << endl; for (int i = 0; i <= middle; i++) { sorted_arr_tree.CreatNode(root, arr[i]); } for (int i = middle + 1; i < arr_n; i++) { sorted_arr_tree.CreatNode(root, arr[i]); } sorted_arr_tree.ProcessLNR(root); cout << "level of n is :" << " "; cout << getLevel(root, 5); return 0; }
Editor is loading...