irapilguy

Untitled

Nov 9th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. struct Node {
  2.     int data;
  3.     Node * left;
  4.     Node *right;
  5. };
  6. void show(Node *tree) {
  7.     if (tree == NULL) return;
  8.     show(tree->left);
  9.     cout << tree->data << " ";
  10.     show(tree->right);
  11. }
  12. void newNode(int x, Node *&root) {
  13.     if (root == NULL) {
  14.         root = new Node();
  15.         root->data = x;
  16.         root->left = root->right = NULL;
  17.     }
  18.     if (x < root->data) {
  19.         if (root->left != NULL) newNode(x, root->left);
  20.         else {
  21.             root->left = new Node();
  22.             root->left->data = x;
  23.             root->left->left = root->left->right = NULL;
  24.         }
  25.     }
  26.     if (x > root->data) {
  27.         if (root->right != NULL) newNode(x, root->right);
  28.         else {
  29.             root->right = new Node();
  30.             root->right->data = x;
  31.             root->right->left = root->right->right = NULL;
  32.         }
  33.     }
  34. }
  35. int search(int x, Node *tree) {
  36.     if (tree == NULL) return 0;
  37.     if (tree->data == x) return 1;
  38.     if (tree->data > x) {
  39.         return search(x, tree->left);
  40.     }
  41.     if (tree->data < x) {
  42.         return search(x, tree->right);
  43.     }
  44. }
  45. int max(int a, int b) {
  46.     if (a > b) return a;
  47.     else return b;
  48. }
  49. int hight(Node *tree) {
  50.     if (tree == NULL) return 0;
  51.     return max(hight(tree->left), hight(tree->right)) + 1;
  52. }
Add Comment
Please, Sign In to add comment