二叉查找樹 (Binary Search Tree)

今天開始看 數據結構與算法分析C++版本 看到樹了 而後寫了一下本身的實現,node

本身的算法功底太弱了 這對於一個想向底層發展的人來講是難以忍受的,但願ios

過年以後把這本書看完,增強一下本身的功底!算法

//
// binary_search_tree.cc
//

#include <iostream>
using namespace std;

template<class T>
class BinarySearchTree {
 public:
  BinarySearchTree();
  ~BinarySearchTree();
  void insert(T value);
  T search(T value);
  void print_tree();
  void make_empty();
  void remove(T value);
  void after_print_tree();
  void before_print_tree();
 private:
  struct node {
    T value;
    struct node *left;
    struct node *right;
  };
  struct node  *root;
  void insert_node(struct node *&t, T value);
  T search_node(struct node *t, T value);
  void print_node(struct node *t);
  void make_empty(struct node *&t);
  void remove_node(struct node *&t, T value);
  T find_min_node(struct node *t);
  void print_tree_a(struct node *t);
  void print_tree_b(struct node *t);
};

template<class T>
BinarySearchTree<T>::BinarySearchTree()
{
  root = NULL;
}

template<class T>
BinarySearchTree<T>::~BinarySearchTree()
{}

template<class T>
void BinarySearchTree<T>::insert(T value)
{
  insert_node(root, value);
}

template<class T>
void BinarySearchTree<T>::insert_node(struct node *&t, T value)
{
  if (t == NULL) {
    t = new struct node;
    t->value = value;
    t->left = NULL;
    t->right = NULL;
  } else if (value < t->value) {
    insert_node(t->left, value);
  } else {
    insert_node(t->right, value);
  }
}

template<class T>
T BinarySearchTree<T>::search(T value)
{
  return search_node(root, value);
}

template<class T>
T BinarySearchTree<T>::search_node(struct node *t, T value)
{
  if (t == NULL)
    return NULL;

  if (value < t->value) {
    return search_node(t->left, value);
  } else if (value > t->value) {
    return search_node(t->right, value);
  } else if (value == t->value) {
    return t->value;
  }
  return NULL;

}

template<class T>
void BinarySearchTree<T>::print_tree()
{
  print_node(root);
}

template<class T>
void BinarySearchTree<T>::print_node(struct node *t)
{
  if (t)
  {
    print_node(t->left);
    cout<<t->value<<endl;
    print_node(t->right);
  }
}

template<class T>
void BinarySearchTree<T>::make_empty()
{
  make_empty(root);
}

template<class T>
void BinarySearchTree<T>::make_empty(struct node *&t)
{
  if (t) {
    make_empty(t->left);
    make_empty(t->right);
    delete t;
  }
  t = NULL;
}

template<class T>
void BinarySearchTree<T>::remove(T value)
{
  remove_node(root, value);
}

template<class T>
void BinarySearchTree<T>::remove_node(struct node *&t, T value)
{
  if (t == NULL)
    cout<<"can not find remove value"<<endl;
  if (value < t->value) {
    remove_node(t->left, value);
  } else if (value > t->value) {
    remove_node(t->right, value);
  } else if (t->left != NULL && t->right != NULL) {
    t->value = find_min_node(t->right);
    remove_node(t->right, t->value);
  } else {
    struct node *old = t;
    t = (t->left == NULL) ? t->right : t->left;
    delete old;
  }
}

template<class T>
T BinarySearchTree<T>::find_min_node(struct node *t)
{
  while (t->left) {
    t = t->left;
  }
  return t->value;
}

template<class T>
void BinarySearchTree<T>::after_print_tree()
{
  print_tree_a(root);
}

template<class T>
void BinarySearchTree<T>::print_tree_a(struct node *t)
{
  if (t) {
  print_tree_a(t->left);
  print_tree_a(t->right);
  cout<<t->value<<endl;
  }
}

template<class T>
void BinarySearchTree<T>::before_print_tree()
{
  print_tree_b(root);
}

template<class T>
void BinarySearchTree<T>::print_tree_b(struct node *t)
{
  if (t) {
    cout<<t->value<<endl;
    print_tree_b(t->left);
    print_tree_b(t->right);
  }
}

int main(int argc, char* argv[])
{

  BinarySearchTree<int> *bt = new BinarySearchTree<int>;

  bt->insert(5);
  bt->insert(2);
  bt->insert(7);
 
  bt->insert(1);
  bt->insert(4);
  bt->insert(6);
  bt->insert(8);

  bt->insert(3);

  //中序列
  bt->print_tree();

  cout<<"----------------------------------------"<<endl<<endl;

  //後序列
  bt->after_print_tree();
 
  cout<<"----------------------------------------"<<endl<<endl;

  //前序列
  bt->before_print_tree();
  return 0;
}


數據結構

相關文章
相關標籤/搜索