zoukankan      html  css  js  c++  java
  • 二分搜索树

    一、二分搜索树的查找

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

    #include <iostream>
    #include <queue>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    template <typename Key, typename Value>
    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<Node*> q;
          q.push(root);
    
          while(!q.empty()){
              Node *node = q.front();
              q.pop();
    
              cout<<node->key<<endl;
    
               if(node->left)
                  q.push(node->left);
               if(node->right)
                  q.push(node->right);
    
          }
      }
    };
    
    int main() {
      srand(time(NULL));
      BST<int, int> bst = BST<int, int>();
    
      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;
    }

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

    四、

    五、

  • 相关阅读:
    美团面试,360面试 ,滴滴面试,阿里面试,百度面试,京东面试,搜狗面试:
    Maven 3-Maven依赖版本冲突的分析及解决小结 (阿里,美团,京东面试)
    maven snapshot和release版本的区别
    Maven 生命周期 和插件
    Maven pom 文件解释
    Zookeeper原理架构
    sublime 支持PHP语法提示
    Zen Coding 用法
    让浏览器屏蔽js
    淘宝设计师入门:设计师SDK环境配置
  • 原文地址:https://www.cnblogs.com/darklights/p/11718786.html
Copyright © 2011-2022 走看看