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

    • 结点定义:
    struct Node{
            char val;
            Node *l = NULL, *r = NULL, *p = NULL;
    };
    
    • 建树:
    Node* build(Node *p /*...根据实际情况而定*/)
    {
            Node *root = new Node;
            root->val = data;
            root->p = p;
            root->l = build(root /*...根据实际情况而定*/);
            root->r = build(root /*...根据实际情况而定*/);
            return root;
    }
    
    • 遍历
    1. 递归实现
      • 先序遍历
      void pre_order(Node* root)
      {
              if(root != NULL){
                  cout<<root->val;
                  pre_order(root->l);
                  pre_order(root->r);
              }
      }
      
      • 中序遍历
      void in_order(Node* root)
      {
              if(root != NULL){
                  in_order(root->l);
                  cout<<root->val;
                  in_order(root->r);
              }
      }
      
      • 后序遍历
      void post_order(Node* root)
      {
              if(root != NULL){
                  post_order(root->l);
                  post_order(root->r);
                  cout<<root->val;
              }
      }
      
    2. 非递归实现
      • 先序遍历
      void pre_order2(Node* root)
      {
              Node *pre = NULL;
              Node *node = root;
              while(node != NULL){
                  if(pre == node->p){
                      cout<<node->val;
                      if(node->l != NULL){
                          pre = node;
                          node = node->l;
                      }
                      else{
                          if(node->r != NULL){
                              pre = node;
                              node = node->r;
                          }
                          else{
                              pre = node;
                              node = node->p;
                          }
                      }
                  }
                  else if(pre == node->l){
                      if(node->r != NULL){
                          pre = node;
                          node = node->r;
                      }
                      else{
                          pre = node;
                          node = node->p;
                      }
                  }
                  else if(pre == node->r){
                      pre = node;
                      node = node->p;
                  }
              }
      }
      
      • 中序遍历
      void in_order2(Node* root)
      {
              Node *pre = NULL;
              Node *node = root;
              while(node != NULL){
                  if(pre == node->p){
                      if(node->l != NULL){
                          pre = node;
                          node = node->l;
                      }
                      else{
                          cout<<node->val;
                          if(node->r != NULL){
                              pre = node;
                              node = node->r;
                          }
                          else{
                              pre = node;
                              node = node->p;
                          }
                      }
                  }
                  else if(pre == node->l){
                      cout<<node->val;
                      if(node->r != NULL){
                          pre = node;
                          node = node->r;
                      }
                      else{
                          pre = node;
                          node = node->p;
                      }
                  }
                  else if(pre == node->r){
                      pre = node;
                      node = node->p;
                  }
              }
      }
      
      • 后序遍历
      void post_order2(Node* root)
      {
              Node *pre = NULL;
              Node *node = root;
              while(node != NULL){
                  if(pre == node->p){
                      if(node->l != NULL){
                          pre = node;
                          node = node->l;
                      }
                      else{
                          if(node->r != NULL){
                              pre = node;
                              node = node->r;
                          }
                          else{
                              cout<<node->val;
                              pre = node;
                              node = node->p;
                          }
                      }
                  }
                  else if(pre == node->l){
                      if(node->r != NULL){
                          pre = node;
                          node = node->r;
                      }
                      else{
                          cout<<node->val;
                          pre = node;
                          node = node->p;
                      }
                  }
                  else if(pre == node->r){
                      cout<<node->val;
                      pre = node;
                      node = node->p;
                  }
              }
      }
      
    • 查找
    1. 递归实现
      Node* tree_search(Node *x, char k)
      {
          if(x == NULL || k == x.val)
              return x;
          if(k < x.val)
              return tree_search(x->l, k);
          else
              return tree_search(x->r, k);
      }
      
      
    2. 非递归实现
      Node* tree_search2(Node *x, char k)
      {
          while(x != NULL && k != x->val){
              if(k < x->val)
                  x = x->l;
              else
                  x = x->r;
          }
          return x;
      }
      
      
    • 最值元素
    1. 最小关键字元素
      Node* tree_minimum(Node *x)
      {
          while(x->l != NULL)
              x = x->l;
          return x;
      }
      
    2. 最大关键字元素
      Node* tree_maximum(Node *x)
      {
          while(x->r != NULL)
              x = x->r;
          return x;
      }
      
    • 后继和前驱(中序遍历)
    1. 后继
      Node* tree_successor(Node *x)
      {
          if(x->r != NULL)
              return tree_minimum(x->r);
          Node *y = x->p;
          while(y != NULL && x == y->r){
              x = y;
              y = y->p;
          }
          return y;
      }
      
    2. 前驱
      Node* tree_predecessor(Node *x)
      {
          if(x->l != NULL)
              return tree_maximum(x->l);
          Node *y = x->p;
          while(y != NULL && x == y->l){
              x = y;
              y = y->p;
          }
          return y;
      }
      
    • 插入和删除
    1. 插入
      Node* tree_insert(Node *root, Node *z)  //插入结点z
      {
          Node *x = root;
          Node *y = NULL;
          while(x != NULL){
              y = x;
              if(z->val < x->val)
                  x = x->l;
              else
                  x = x->r;
          }
          z->p = y;
          if(y == NULL){
              //树为空,此时z为根节点
          }
          else if(z->val < y->val)
              y->l = z;
          else
              y->r = z;
      }
      
      
    2. 删除
      待续。。。
  • 相关阅读:
    Hive性能分析和优化方法
    浅谈MySQL中优化sql语句查询常用的30种方法
    spark 源码阅读博客
    spark shell学习笔记
    用shell 实现对MySQL数据库分页
    hive 1.2 配置
    spark1.4配置安装
    HBase学习
    【转】解密饿了么大前端团队
    【转】我心目中的支付宝架构
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5966728.html
Copyright © 2011-2022 走看看