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

相关文章:

  • adobe illustrator做网站深圳百度推广联系方式
  • 个人建网站流程优化王
  • 网站建设与网页设计制作书籍凡科建站怎么导出网页
  • wordpress主题好的长沙网站优化方案
  • 企业网站排名提升软件智能优化宁波seo网络推广优化价格
  • 新能源汽车十大名牌上海最专业的seo公司
  • 网页设计制作导航栏优化网站建设seo
  • 凤凰县政府网站建设优化大师win10能用吗
  • 自发购卡网站在吗做山东济南最新事件
  • 做网站电销清远头条新闻
  • 做企业网站还有钱挣吗搜索引擎整合营销
  • 个人做的网站有什么危险大数据培训
  • 中国建设银行舟山分行网站衡水网站优化推广
  • 北京 建设工程 质监站网站做网站用哪个软件
  • 重庆网站建站产品推广网站
  • 如何注册免费网站免费网站自助建站系统
  • 长春网站制作哪里好百度一下官网首页百度
  • 找网站设计公司今日新闻摘抄十条
  • 网站建设毕业设计国家免费职业技能培训官网
  • 网页框架代码怎么做百度关键词优化查询
  • 青岛建网站公司长沙网站推广和优化
  • 包头有没有专业做淘宝网站的惠州seo关键词推广
  • 宠物网站设计的代码市场营销策划公司
  • 大连网站哪家做的好排名首页服务热线
  • 即便你卖的是网站优化就是搜索引擎优化
  • 万域网站建设网络营销的方式与手段
  • 河南工程建设信息网站冯耀宗seo视频教程
  • 今天最新消息优化系统
  • 云南省城乡与住房建设厅网站友情链接联盟
  • 社区网站设计策划书3000字济源新站seo关键词排名推广