binary_search_tree

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
namespace CLRS
{
  // recursive version of search
  template <typename T>
  TreeNode<T> *tree_search(TreeNode<T> *x, T k)
  {
    if(x == nullptr || k == x->get_value())
      return x;
    if(k < x->get_value())
      return tree_search(x->get_left(), k);
    else
      return tree_search(x->get_right(), k);
  }

  // iterative version of serach
  template <typename T>
  TreeNode<T> *iterative_tree_search(TreeNode<T> *x, T k)
  {
    while(x != nullptr && k != x->get_value())
      {
	if(k < x->get_value())
	  x = x->get_left();
	else
	  x = x->get_right();
      }
    return x;
  }

  template <typename T>
  TreeNode<T> *tree_minimum(TreeNode<T> *x)
  {
    while(x->get_left() != nullptr)
      x = x->get_left();
    return x;
  }

  template <typename T>
  TreeNode<T> *tree_maximum(TreeNode<T> *x)
  {
    while(x->get_right() != nullptr)
      x = x->get_right();
    return x;
  }

  template <typename T>
  TreeNode<T> *tree_successor(TreeNode<T> *x)
  {
    if(x->get_right() != nullptr)
      return tree_minimum(x->get_right());
    TreeNode<T> *y = x->get_parent();
    while(y != nullptr && x == y->get_right())
      {
	x = y;
	y = y->get_parent();
      }
    return y;
  }

  template <typename T>
  TreeNode<T> *tree_predecessor(TreeNode<T> *x)
  {
    if(x->get_left() != nullptr)
      return tree_maximum(x->get_left());
    TreeNode<T> *y = x->get_parent();
    while(y != nullptr && x == y->get_left())
      {
	x = y;
	y = y->get_parent();
      }
    return y;
  }

  template <typename T>
  void tree_insert(Tree<T> *t, TreeNode<T> *z)
  {
    TreeNode<T> *y = nullptr;
    TreeNode<T> *x = t->get_root();
    while(x != nullptr)
      {
	y = x;
	if(z->get_value() < x->get_value())
	  x = x->get_left();
	else
	  x = x->get_right();
      }
    z->set_parent(y);
    if(y == nullptr)
      t->set_root(z);
    else if(z->get_value() < y->get_value())
      y->set_left(z);
    else
      y->set_right(z);
  }

  // update of left and right of v is caller's duty.
  template <typename T>
  void transplant(Tree<T> *t, TreeNode<T> *u, TreeNode<T> *v)
  {
    if(u->get_parent() == nullptr)
      t->set_root(v);
    else if(u == u->get_parent()->get_left())
      u->get_parent()->set_left(v);
    else
      u->get_parent()->set_right(v);
    if(v != nullptr)
      v->set_parent(u->get_parent());
  }

  template <typename T>
  void tree_delete(Tree<T> *t, TreeNode<T> *z)
  {
    if(z->get_left() == nullptr)
      transplant(t, z, z->get_right());
    else if(z->get_right() == nullptr)
      transplant(t, z, z->get_left());
    else
      {
	TreeNode<T> *y = tree_minimum(z->get_right());
	if(y->get_parent() != z)
	  {
	    transplant(t, y, y->get_right());
	    y->set_right(z->get_right());
	    y->get_right()->set_parent(y);
	  }
	transplant(t, z, y);
	y->set_left(z->get_left());
	y->get_left()->set_parent(y);
      }
  }
}