当前位置: 首页 > 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/151237.html

相关文章:

  • 专业建站公司前景广州百度seo公司
  • 海外音乐类网站做的比较好的公司网址有哪些
  • 印刷个性化网站建设的意义长沙网
  • 电子商务网站开发项目网站外链购买
  • 网站设计的公司排名网站开发平台有哪些
  • 自己想做个网站 费用福州seo扣费
  • 做网站需要的参考文献广东疫情最新通报
  • 常州网站建设制作工作室网站目录
  • asp网站制作软件电商培训心得体会
  • 陕西高端品牌网站建设价格业务推广网站
  • 学院网站建设项目微信crm系统
  • 江苏网络公司网站建设关键词排名怎么做上首页
  • 政府网站平台建设和维护管理百度推广公司电话
  • 网站正在建设中热最新舆情信息网
  • 海洋网络做网站不负责想做seo哪里有培训的
  • 深圳做网站新科怎么开网店新手入门
  • 怎么在网站做视频接口seo怎么做
  • 50万做网站网站交易
  • 与网站建设关系密切的知识点黄冈网站推广软件费用是多少
  • 扬州有什么做网站的公司百度识图在线识图
  • 做网站 参考文献百度服务热线电话
  • 美女图片网站模板肇庆百度快照优化
  • 文登住房和建设局网站百度app官网下载
  • phpcms v9 网站搬家宁波seo网络推广渠道介绍
  • 建设银行云南分行招聘网站seo网站诊断价格
  • 汕头制作网站软件搜索引擎优化seo多少钱
  • 个人网站如何做支付功能前端开发
  • 做医院网站公司电话线上培训机构
  • 装修行业网站建设百度搜索引擎的优缺点
  • 订阅号自定义可以做链接网站不关键词优化公司哪家好