当前位置: 首页 > wzjs >正文

深达网站制作深圳公司搜索引擎优化包括哪些方面

深达网站制作深圳公司,搜索引擎优化包括哪些方面,十大网站建设,自己电脑上做网站怎么使用源码本章重点 二叉搜索树的实现二叉树搜索树应用分析二叉树进阶面试题 1.二叉搜索树的实现 1.1 概念 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树: 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值若…

本章重点

  1. 二叉搜索树的实现
  2. 二叉树搜索树应用分析
  3. 二叉树进阶面试题

1.二叉搜索树的实现

1.1 概念

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

03.01.png

二叉搜索树的性质保证了其中序遍历是有序的同时还天然去重,所以也称为二叉排序树

1.2 基础结构

template<typename K>
struct BSTreeNode
{BSTreeNode(const K& key):_key(key), _left(nullptr), _right(nullptr){}K _key;BSTreeNode<K>* _left;BSTreeNode<K>* _right;
};template<typename K>
class BSTree
{typedef BSTreeNode<K> Node;
public:BSTree():_root(nullptr){ }//拷贝构造BSTree(const BSTree<K>& bst){_root = Copy(bst._root);}//赋值拷贝BSTree<K>& operator=(BSTree<K> bst) 	{swap(_root, bst._root);return *this;}~BSTree(){Destory(_root);}//插入bool Insert(const K& key);//循环bool InsertR(const K& key);//递归//查找bool Find(const K& key);//循环bool FindR(const K& key);//递归//删除bool Erase(const K& key);//循环bool EraseR(const K& key);//递归// 中序遍历void InOrder();private:bool _InsertR(Node*& root, const K& key);bool _EraseR(Node*& root, const K& key);bool _FindR(Node* root, const K& key);void _InOrder(Node* root);//拷贝构造子函数Node* Copy(Node* other){if (other == nullptr)return nullptr;Node* newNode = new Node(other->_key);newNode->_left = Copy(other->_left);newNode->_right = Copy(other->_right);return newNode;}//析构函数子函数void Destory(Node*& root){if (root == nullptr)return;Destory(root->_left);Destory(root->_right);delete root;root = nullptr;}
private:Node* _root;
};

1.3 基础功能

1.3.1 查找

  1. 从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。

  2. 最多查找高度次,走到到空,还没找到,这个值不存在。

//查找
bool Find(const K& key)//循环
{Node* cur = _root;while (cur){if (key < cur->_key){cur = cur->_left;}else if (key > cur->_key){cur = cur->_right;}else{return true;}}return false;
}
//---------------------------------------------------
bool FindR(const K& key)//递归
{return _FindR(_root, key);
}
bool _FindR(Node* root, const K& key)
{if (root == nullptr){return false;}if (key < root->_key){return _FindR(root->_left, key);}else if (key > root->_key){return _FindR(root->_right, key);}else{return true;}
}

1.3.2 插入

  1. 树为空,则直接新增节点,赋值给root指针

  2. 树不空,按二叉搜索树性质查找插入位置,插入新节点

//插入
bool Insert(const K& key)//循环
{if (_root == nullptr){_root = new Node(key);return true;}Node* parent = nullptr;Node* cur = _root;while (cur){if (key < cur->_key){parent = cur;cur = cur->_left;}else if (key > cur->_key){parent = cur;cur = cur->_right;}else{return false;}}Node* newNode = new Node(key);if (key < parent->_key){parent->_left = newNode;}else{parent->_right = newNode;}return true;
}
//---------------------------------------------------
bool InsertR(const K& key)//递归
{return _InsertR(_root, key);
}
bool _InsertR(Node*& root, const K& key)
{if (root == nullptr){root = new Node(key);return true;}if (key < root->_key){return _InsertR(root->_left, key);}else if (key > root->_key){return _InsertR(root->_right, key);}else{return false;}}

1.3.3 删除

首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:

a. 要删除的结点无孩子结点

b. 要删除的结点只有左孩子结点

c. 要删除的结点只有右孩子结点

d. 要删除的结点有左、右孩子结点

看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程
如下:

情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除(左树最大值,右树最小值)

//删除
bool Erase(const K& key)//循环
{Node* parent = nullptr;Node* cur = _root;while (cur){if (key < cur->_key){parent = cur;cur = cur->_left;}else if (key > cur->_key){parent = cur;cur = cur->_right;}else{if (cur->_left == nullptr){if (parent == nullptr){_root = cur->_right;}else if (parent->_left == cur){parent->_left = cur->_right;}else{parent->_right = cur->_right;}}else if (cur->_right == nullptr){if (parent == nullptr){_root = cur->_left;}else if (parent->_left == cur){parent->_left = cur->_left;}else{parent->_right = cur->_left;}}else{Node* l_max_parent = cur;Node* l_max = cur->_left;while (l_max->_right){l_max_parent = l_max;l_max = l_max->_right;}swap(cur->_key, l_max->_key);if (l_max_parent == cur){l_max_parent->_left = l_max->_left;}else{l_max_parent->_right = l_max->_left;}delete l_max;return true;}}}return false;
}
//---------------------------------------------------
bool EraseR(const K& key)//递归
{return _EraseR(_root, key);
}
bool _EraseR(Node*& root, const K& key)
{if (root == nullptr){return false;}if (root->_key > key){return _EraseR(root->_left, key);}else if (root->_key < key){return _EraseR(root->_right, key);}else{Node* to_delete = root;if (root->_left == nullptr) //1.左为空{root = root->_right;}else if (root->_right == nullptr) //2.右为空{root = root->_left;}else //3.左右不为空{Node* l_max = root->_left;while (l_max->_right){l_max = l_max->_right;}swap(root->_key, l_max->_key);return _EraseR(root->_left, key);//转换为删除左子树最小节点}delete to_delete;return true;}
}

1.3.4 中序遍历

// 中序遍历
void InOrder()
{_InOrder(_root);cout << endl;
}
void _InOrder(Node* root)
{if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);
}

1.4 源码

#pragma once
#include<iostream>
using namespace std;template<typename K>
struct BSTreeNode
{BSTreeNode(const K& key):_key(key), _left(nullptr), _right(nullptr){}K _key;BSTreeNode<K>* _left;BSTreeNode<K>* _right;
};template<typename K>
class BSTree
{typedef BSTreeNode<K> Node;
public:BSTree():_root(nullptr){ }BSTree(const BSTree<K>& bst){_root = Copy(bst._root);}BSTree<K>& operator=(BSTree<K> bst) {swap(_root, bst._root);return *this;}~BSTree(){Destory(_root);}//插入bool Insert(const K& key)//循环{if (_root == nullptr){_root = new Node(key);return true;}Node* parent = nullptr;Node* cur = _root;while (cur){if (key < cur->_key){parent = cur;cur = cur->_left;}else if (key > cur->_key){parent = cur;cur = cur->_right;}else{return false;}}Node* newNode = new Node(key);if (key < parent->_key){parent->_left = newNode;}else{parent->_right = newNode;}return true;}bool InsertR(const K& key)//递归{return _InsertR(_root, key);}//查找bool Find(const K& key)//循环{Node* cur = _root;while (cur){if (key < cur->_key){cur = cur->_left;}else if (key > cur->_key){cur = cur->_right;}else{return true;}}return false;}bool FindR(const K& key)//递归{return _FindR(_root, key);}//删除bool Erase(const K& key)//循环{Node* parent = nullptr;Node* cur = _root;while (cur){if (key < cur->_key){parent = cur;cur = cur->_left;}else if (key > cur->_key){parent = cur;cur = cur->_right;}else{if (cur->_left == nullptr){if (parent == nullptr){_root = cur->_right;}else if (parent->_left == cur){parent->_left = cur->_right;}else{parent->_right = cur->_right;}}else if (cur->_right == nullptr){if (parent == nullptr){_root = cur->_left;}else if (parent->_left == cur){parent->_left = cur->_left;}else{parent->_right = cur->_left;}}else{Node* l_max_parent = cur;Node* l_max = cur->_left;while (l_max->_right){l_max_parent = l_max;l_max = l_max->_right;}swap(cur->_key, l_max->_key);if (l_max_parent == cur){l_max_parent->_left = l_max->_left;}else{l_max_parent->_right = l_max->_left;}delete l_max;return true;}}}return false;}bool EraseR(const K& key)//递归{return _EraseR(_root, key);}// 中序遍历void InOrder(){_InOrder(_root);cout << endl;}
private:bool _InsertR(Node*& root, const K& key){if (root == nullptr){root = new Node(key);return true;}if (key < root->_key){return _InsertR(root->_left, key);}else if (key > root->_key){return _InsertR(root->_right, key);}else{return false;}}bool _EraseR(Node*& root, const K& key){if (root == nullptr){return false;}if (root->_key > key){return _EraseR(root->_left, key);}else if (root->_key < key){return _EraseR(root->_right, key);}else{Node* to_delete = root;if (root->_left == nullptr) //1.左为空{root = root->_right;}else if (root->_right == nullptr) //2.右为空{root = root->_left;}else //3.左右不为空{Node* l_max = root->_left;while (l_max->_right){l_max = l_max->_right;}swap(root->_key, l_max->_key);return _EraseR(root->_left, key);//转换为删除左子树最小节点}delete to_delete;return true;}}bool _FindR(Node* root, const K& key){if (root == nullptr){return false;}if (key < root->_key){return _FindR(root->_left, key);}else if (key > root->_key){return _FindR(root->_right, key);}else{return true;}}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);}Node* Copy(Node* other){if (other == nullptr)return nullptr;Node* newNode = new Node(other->_key);newNode->_left = Copy(other->_left);newNode->_right = Copy(other->_right);return newNode;}void Destory(Node*& root){if (root == nullptr)return;Destory(root->_left);Destory(root->_right);delete root;root = nullptr;}
private:Node* _root;
};

2.二叉树搜索树应用分析

  1. key的搜索模型(快速判断在不在)
  • 门禁系统
  • 小区车辆出入系统
  • 单词拼写检查系统
  1. key/value的搜索模型(通过一个值找另一个值)
  • 商场的车辆出入系统
  • 高铁实名制车票系统

3.二叉树进阶面试题

这些题目更适合使用C++完成,难度也更大一些

  1. 二叉树创建字符串

  2. 二叉树的分层遍历1

  3. 二叉树的分层遍历2

  4. 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先

  5. 二叉树搜索树转换成排序双向链表

  6. 根据一棵树的前序遍历与中序遍历构造二叉树

  7. 根据一棵树的中序遍历与后序遍历构造二叉树

  8. 二叉树的前序遍历,非递归迭代实现

  9. 二叉树中序遍历 ,非递归迭代实现

  10. 二叉树的后序遍历 ,非递归迭代实现

http://www.dtcms.com/wzjs/10043.html

相关文章:

  • 石家庄网站外包海外引流推广平台
  • 做彩票网站需要境外网络推广渠道都有哪些
  • 好的网站建设价格产品品牌推广策划方案
  • 如何做网站的客服自媒体引流推广
  • 如何做品牌宣传与推广国内做seo最好的公司
  • 伍佰亿网站怎么做百度seo点击工具
  • 网站建设基本常识潍坊网站关键词推广
  • 兼职网站建设策划书关键词排名怎么查
  • 哪些网站可以直接做英文字谜搜索引擎推广有哪些平台
  • 安阳市殷都区最新疫情今天的优化新十条
  • 做问卷调查兼职可靠网站电子商务网站开发
  • 启东市住房城乡建设局网站百度推广助手app下载
  • 58同城做网站多少钱武汉外包seo公司
  • 做牛仔的时尚网站百度快速排名平台
  • 手机怎么做网站免费的经典广告推广词
  • 网站中页面模板设计建站之星官网
  • 我想开个网店不知道怎么入手南宁seo推广服务
  • 企业展示网站 数据库设计优化设计五年级上册语文答案
  • 奉化建设网站app平台搭建
  • 安吉做企业网站seo实战密码在线阅读
  • 拖拉建网站黑马培训是正规学校吗
  • 自己建设色情网站赚钱石家庄seo全网营销
  • 关键词优化排名软件sseo排名怎么优化软件
  • 简单建设一个网站的过程毕节地seo
  • 做威客的网站百度检索入口
  • 帮人做网站小红书怎么做关键词排名优化
  • 南宁伯才网络公司做网站好吗怎么注册网站 个人
  • 快速做网站团队百度seo手机
  • 玉环住房与城乡建设规划局网站成品人和精品人的区别在哪
  • 如何开发网站平台开发南宁seo排名优化