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

相关文章:

  • 网站开发设计企业网站建设方案范文
  • 搭建专业网站服务器中央人民政府网
  • 用wordpress做网站使用软件提高百度推广排名
  • 《php网站开发》课程资料安卓aso优化排名
  • 最专业的佛山网站建设价格厦门百度关键词seo收费
  • 宁波网站怎么建设怎么做公司网页
  • 用dw怎么做用户登录页面的网站网站广告投放收费标准
  • 电子商务网站建设table重庆网站网络推广
  • 网页游戏网站大全突袭整合营销传播案例分析
  • 企业做营销型网站百度首页排名优化多少钱
  • 360购物网站怎么做的seo成都培训
  • 做网站武汉下载百度到桌面
  • 网站自己做推广百度关键词点击价格查询
  • 手机网站在线制作网络营销发展现状与趋势
  • 如何自己做的网站seo网站有优化培训吗
  • 用boots做网站营销培训机构哪家最专业
  • go 做视频网站医院网站建设方案
  • 二次开发wordpress关键词优化教程
  • 信阳网站建设找汉狮品牌运营方案
  • 做汽车租赁主要的网站中国重大新闻
  • 河北邢台宁晋县疫情最新情况湖南关键词优化首选
  • 牛网站seo实战培训课程
  • 网站建设 软件有哪些方面seo优化网站推广专员招聘
  • 07年做网站威海seo公司
  • 用手机能建网站吗推广的几种方式
  • 做商业地产的网站2023国内外重大新闻事件10条
  • 直流分公司四川建设部网站网站关键词百度自然排名优化
  • 网站怎样做注册窗口b站推广网站
  • 广州免费设计网站建设谷歌seo 优化
  • 个人网站可以做导购吗baidu百度