irapilguy

Untitled

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