二分搜索树

一、二分搜索树的查找

 

二、二分搜索树的遍历(深度遍历<前、中、后>,广度遍历)

#include 
#include 
#include 
#include 

using namespace std;

template 
class BST {
 private:
  struct Node {
    Key key;
    Value value;
    Node* left;
    Node* right;

    Node(Key key, Value value) {
      this->key = key;
      this->value = value;
      this->left = this->right = NULL;
    }
  };

  Node* root;
  int count;

  Node* insert(Node* node, Key key, Value value) {
    if (node == NULL) {
      count++;
      return new Node(key, value);
    }

    if (key == node->key)
      node->value = value;
    else if (key < node->key)
      node->left = insert(node->left, key, value);
    else
      node->right = insert(node->right, key, value);

          return node;
  }

  bool contain(Node* node, Key key) {
    if (node == NULL) return false;

    if (key == node->key)
      return true;
    else if (key < node->key)
      return contain(node->left, key);
    else
      return contain(node->right, key);
  }

  Value* search(Node* node, Key key) {
    if (node == NULL) return NULL;

    if (key == node->key)
      return &(node->value);
    else if (key < node->key)
      return search(node->left, key);
    else
      return search(node - right, key);
  }
  //前序遍历
  void preoder(Node* node) {
    if (node != NULL) {
      cout << node->key << endl;
      preoder(node->left);
      preoder(node->right);
    }
  }
  //中序遍历
  void inoder(Node* node) {
    if (node != NULL) {
      inoder(node->left);
      cout << node->key << endl;
      inoder(node -> right);
    }
  }
  //后序遍历
  void postoder(Node* node) {
    if (node != NULL) {
      postoder(node->left);
      postoder(node->right);
      cout << node->key << endl;
    }
  }
  //释放空间
  void destory(Node* node) {
    if (node != NULL) {
      destory(node->left);
      destory(node->right);

      delete node;
      count--;
    }
  }

 public:
  BST() {
    root = NULL;
    count = 0;
  }

  ~BST() { destory(root); }

  int size() { return count; }

  int isEmpty() { return count == 0; }

  int insert(Key key, Value value) { root = insert(root, key, value); }

  bool contain(Key key) { return contain(root, key); }

  Value* search(Key key) { return search(root, key); }

  void preoder() { preoder(root); }

  void inoder() { inoder(root); }

  void postoder()  { postoder(root); }
  // 层序遍历
  void leveloder(){
      
      queue q;
      q.push(root);

      while(!q.empty()){
          Node *node = q.front();
          q.pop();

          cout<key<left)
              q.push(node->left);
           if(node->right)
              q.push(node->right);

      }
  }
};

int main() {
  srand(time(NULL));
  BST bst = BST();

  int N = 10, M = 100;
  for (int i = 0; i < N; i++) {
    int key = rand() % M;
    int value = key;
    cout << key << " ";
    bst.insert(key, value);
  }
  cout << endl;

  cout << "szie:" << bst.size() << endl;

  cout << "preoder:" << endl;
  bst.preoder();
  cout << endl;

  cout << "inoder:" << endl;
  bst.inoder();
  cout << endl;

  cout << "postoder:" << endl;
  bst.postoder();
  cout << endl;
  
  cout << "leveloder:" << endl;
  bst.leveloder();
  cout << endl;

  return 0;
}

三、二分搜索树删除最大值最小值

 

四、

 

五、

 

你可能感兴趣的:(二分搜索树)