Home c++ The binary search tree

The binary search tree

Author

Date

Category

All the good time of day! Please help us to add the program and it needs two methods of search and removal of objects from a binary search tree. Adding somehow mastered, but the removal and search can not. The essence is such, there are two classes, one class – the node, the second class of the tree, that the tree needs to be done within the two methods that I can not do that.
In advance thank you all

# include & lt; iostream & gt;
Using Namespace STD;
class Node {
 Private:
  int _value;
  Node * left;
  Node * right;
 Public:
   Node (int value) {
    _value = value;
    left = NULL;
    right = NULL;
  } Node (): _ value (0), left (NULL), right (NULL) {
  }
  void SetLeft (Node * ptr) {
    left = ptr;
  }
  void SetRight (Node * ptr) {
    right = ptr;
  }
  void SetValue (int iValue) {
    _value = iValue;
  }
  int GetValue () {
    return _value;
  }
  Node * GetLeft () {
    return left;
  }
  Node * GetRight () {
    return right;
  }
};
class Tree {
 Private:
  Node * head;
 Public:
  Tree (): head (NULL) {
  } Tree (int value) {
    Node * node = new Node (value);
    head = node;
  }
  Tree (Node * node) {
    head = node;
  }
  bool Add (Node * node, int value) {
    If (Head == NULL) {
      Node * node = new Node (value);
      head = node;
      RETURN TRUE;
    }
    if (value & gt; = node- & gt; GetValue ()) {
      if (node- & gt;! GetRight () = NULL)
        return Add (node- & gt; GetRight (), value);
      ELSE {
        Node * newNode = new Node (value);
        node- & gt; SetRight (newNode);
        RETURN TRUE;
      }
    } else {
      if (node- & gt;! GetLeft () = NULL)
        Add (node- & gt; GetLeft (), value);
      ELSE {
        Node * newNode = new Node (value);
        node- & gt; SetLeft (newNode);
        RETURN TRUE;
      }
    }
  }
  bool Add (int value) {
    If (Head == NULL) {
      Node * node = new Node (value);
      head = node;
      RETURN TRUE;
    }
    if (value & gt; = head- & gt; GetValue ()) {
      if (head- & gt;! GetRight () = NULL)
        return Add (head- & gt; GetRight (), value);
      ELSE {
        Node * newNode = new Node (value);
        head- & gt; SetRight (newNode);
        RETURN TRUE;
      }
    } else {
      if (head- & gt;! GetLeft () = NULL)
        return Add (head- & gt; GetLeft (), value);
      ELSE {
        Node * newNode = new Node (value);
        head- & gt; SetLeft (newNode);
        RETURN TRUE;
      }
    }
  }
  bool search (int ivalue) {
    if (ivalue == head- & gt; GetValue ()) {
      RETURN TRUE;
    } else {
      RETURN FALSE;
    }
  }
};

Answer 1, Authority 100%

Node * Tree :: RemoveNode (Node * root, int x)
{
  Node * t = new Node;
  if (root == NULL)
    RETURN NULL;
  if (x == root- & gt; _value) {
    if (root- & gt; left == NULL) {
      t = root- & gt; right;
      delete root;
      RETURN T;
    }
    t = root- & gt; left;
    while (t- & gt; right) {
      t = t- & gt; right;
    }
    t- & gt; right = root- & gt; right;
    return root- & gt; left;
  }
  if (x & lt; root- & gt; _value)
    root- & gt; left = RemoveNode (root- & gt; left, x);
  ELSE.
    root- & gt; right = RemoveNode (root- & gt; right, x);
  return root;
}

Answer 2

bool Tree :: search (int value, Node * node) {
  while (node! = NULL) {
  if (value == node- & gt; getValue ()) {
    RETURN TRUE;
  }
  ELSE {
    if (value & lt; = node- & gt; GetValue ()) return search (value, node- & gt; GetLeft ());
    else return search (value, node- & gt; GetRight ());
  }
  }
  RETURN FALSE;
}
bool Tree :: search (int value) { 
While (Head! = NULL) {
   If (Value == Head- & gt; getvalue ()) {
     RETURN TRUE;
   }
   ELSE {
     if (Value & lt; = head- & gt; getvalue ()) Return Search (Value, Head- & gt; getleft ());
     ELSE RETURN SEARCH (VALUE, HEAD- & GT; GETRIGHT ());
   }
   }
   RETURN FALSE;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions