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

武汉网站建设培训公司推广文案

武汉网站建设培训,公司推广文案,五家渠网站建设,网站建设主要流程文章目录 前言一、二叉搜索树的介绍二、模拟实现二叉搜索树三、leetcode---根据二叉树创建字符串四、leetcode---二叉树的最近公共祖先总结 前言 二叉搜索树的介绍、模拟实现二叉搜索树、leetcode—根据二叉树创建字符串、leetcode—二叉树的最近公共祖先等的介绍 一、二叉搜索…

文章目录

  • 前言
  • 一、二叉搜索树的介绍
  • 二、模拟实现二叉搜索树
  • 三、leetcode---根据二叉树创建字符串
  • 四、leetcode---二叉树的最近公共祖先
  • 总结


前言

二叉搜索树的介绍、模拟实现二叉搜索树、leetcode—根据二叉树创建字符串、leetcode—二叉树的最近公共祖先等的介绍


一、二叉搜索树的介绍

在这里插入图片描述

  • 如上图所示,搜索二叉树就是比当前节点大的存放在右子树,比当前节点小的存放在左子树中。

二、模拟实现二叉搜索树

#pragma oncetemplate<class K>
struct BSTreeNode
{BSTreeNode(const K& key): _left(nullptr), _right(nullptr), _key(key){}BSTreeNode<K>* _left;BSTreeNode<K>* _right;K _key;
};template <class K>
class BSTree
{typedef BSTreeNode<K> Node;
public:BSTree():_root(nullptr){}BSTree(const BSTree<K>& tree){_root = copy(tree._root);}BSTree<K>& operator=(BSTree<K> tree){swap(_root, tree._root);return *this;}~BSTree(){Destroy(_root);}bool Insert(const K& key){if (_root == nullptr){_root = new Node(key);return true;}Node* parent = nullptr;Node* cur = _root;while (cur){if (cur->_key < key){parent = cur;cur = cur->_right;}else if (cur->_key > key){parent = cur;cur = cur->_left;}else{return false;}}cur = new Node(key);if (parent->_key < key){parent->_right = cur;}else{parent->_left = cur;}}bool Erase(const K& key){Node* parent = nullptr;Node* cur = _root;while (cur){if (cur->_key < key){parent = cur;cur = cur->_right;}else if (cur->_key > key){parent = cur;cur = cur->_left;}else{// 找到了if (cur->_left == nullptr) // 找到的节点左树为空{if (cur == _root){_root = _root->_right;}else{if (parent->_left == cur){parent->_left = cur->_right;}else{parent->_right = cur->_right;}}}else if(cur->_right == nullptr) // 找到的节点右树为空{if (cur == _root){_root = _root->_left;}else{if (parent->_left == cur){parent->_left = cur->_left;}else{parent->_right = cur->_left;}}}else // 左右节点都不为空{// 找左子树的最大节点(最右)Node* parent = cur;Node* leftMax = cur->_left;while (leftMax->_right){parent = leftMax;leftMax = leftMax->_right;}swap(leftMax->_key, cur->_key);if (parent->_left == leftMax){parent->_left = leftMax->_left;}else{parent->_right = leftMax->_left;}cur = leftMax;}delete cur;cur = nullptr;return true;}}return false;}Node* find(const K& key){Node* cur = _root;while (cur){if (cur->_key < key){cur = cur->_right;}else if (cur->_key > key){cur = cur->_left;}else{return cur;}}return nullptr;}void InOrder(){_InOrder(_root);cout << endl;}// 递归插入bool InsertR(const K& key){return _InsertR(_root, key);}// 递归删除bool EraseR(const K& key){return _EraseR(_root, key);}Node* findR(const K& key){return _findR(_root, key);}private:// 递归查找Node* _findR(Node* root, const K& key){if (root == nullptr){return nullptr;}if (root->_key < key){_findR(root->_right, key);}else if (root->_key > key){_findR(root->_left, key);}else{return root;}}// copy	Node* copy(Node* root){if (root == nullptr){return nullptr;}Node* copyroot = new Node(root->_key);copyroot->_left = copy(root->_left);copyroot->_right = copy(root->_right);return copyroot;}// 递归析构void Destroy(Node*& root){if (root == nullptr){return;}Destroy(root->_left);Destroy(root->_right);delete root;root = nullptr;}// 递归版本删除bool _EraseR(Node*& root, const K& key){if (root == nullptr){return false;}if (root->_key < key){_EraseR(root->_right, key);}else if (root->_key > key){_EraseR(root->_left, key);}else{Node* del = root;if (root->_left == nullptr){root = root->_right;}else if (root->_right == nullptr){root = root->_left;}else{Node* leftMax = root->_left;while (leftMax->_right){leftMax = leftMax->_right;}swap(leftMax->_key, root->_key);return _EraseR(root->_left, key);}delete del;return true;}}// 递归版本插入数据bool _InsertR(Node*& root, const K& key){if (root == nullptr){root = new Node(key);return true;}if (root->_key < key){_InsertR(root->_right, key);}else if(root->_key > key){_InsertR(root->_left, key);}else{return false;}}void _InOrder(Node* root){if (root == nullptr){return;}_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);}Node* _root;
};

测试:

#include <iostream>
using namespace std;#include "BinarySearchTree.h"void test1()
{BSTree<int> t;int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };for (auto e : a){t.Insert(e);}t.InOrder();t.Erase(8);t.InOrder();t.Erase(4);t.InOrder();t.Erase(6);t.InOrder();t.Erase(7);t.InOrder();
}void test2()
{BSTree<int> t;int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };for (auto e : a){t.InsertR(e);}t.InOrder();t.EraseR(8);t.InOrder();t.EraseR(4);t.InOrder();t.EraseR(6);t.InOrder();t.EraseR(7);t.InOrder();
}void test3()
{BSTree<int> t;int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };for (auto e : a){t.InsertR(e);}t.InOrder();BSTree<int> t1(t);t1.InOrder();BSTree<int> t2;t2 = t1;t2.InOrder();}void test4()
{BSTree<int> t;int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };for (auto e : a){t.InsertR(e);}t.InOrder();cout << t.find(10) << endl;cout << t.findR(100) << endl;}
int main()
{test1();test2();test3();test4();return 0;
}

在这里插入图片描述

三、leetcode—根据二叉树创建字符串

leetcode—根据二叉树创建字符串

在这里插入图片描述

class Solution {
public:string tree2str(TreeNode* root) {if(root == nullptr){return "";}string str;str += to_string(root->val);if(root->left || root->right){str += '(';str += tree2str(root->left);str += ')';}if(root->right){str += '(';str += tree2str(root->right);str += ')';}return str;}
};

四、leetcode—二叉树的最近公共祖先

leetcode—二叉树的最近公共祖先

在这里插入图片描述

class Solution {
public:bool Find(TreeNode* root, TreeNode* x){if(root == nullptr){return false;}return root == x || Find(root->left,x) || Find(root->right, x);}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root == nullptr){return nullptr;}if(root == p || root == q){return root;}bool pInLeft, pInRight, qInLeft, qInRight;pInLeft = Find(root->left, p);pInRight = !pInLeft;qInLeft = Find(root->left, q);qInRight = !qInLeft;if(pInLeft && qInLeft){return lowestCommonAncestor(root->left, p, q); }else if(pInRight && qInRight){return lowestCommonAncestor(root->right, p, q); }else{return root;} }
};

总结

二叉搜索树的介绍、模拟实现二叉搜索树、leetcode—根据二叉树创建字符串、leetcode—二叉树的最近公共祖先等的介绍

http://www.hkea.cn/news/148954/

相关文章:

  • js怎么做网站跨境电商网站
  • 台州外贸网站建设百度推广费用多少
  • 虚拟机怎么做网站空间培训班管理系统 免费
  • wordpress离线文章发布郑州seo关键词排名优化
  • 龙岗区网站建设中国职业培训在线
  • 南山网站建设外包优化网站
  • 个人怎么做网站推广神起网络游戏推广平台
  • 做网站的关键技术运营推广的方式和渠道有哪些
  • jsp做就业网站网推项目
  • 网站开发的目的和意义重庆seo排名电话
  • 顺义专业建站公司最有效的线上推广方式
  • 大连网站网站搭建制作百度识图 上传图片
  • 给人做网站多少钱黑科技引流推广神器怎么下载
  • 沈阳做网站最好的公司百度快照怎么删除
  • 设置本机外网ip做网站网站免费制作平台
  • 有什么推荐做简历的网站2024的新闻有哪些
  • 申请做网站 论坛版主惠州seo外包服务
  • 网站照片上传不了域名解析ip
  • 胖小七网站建设2022最新国际新闻10条简短
  • wordpress 网站备份厦门seo外包服务
  • 网站建设及推广培训杭州百度快照优化排名
  • 简单手机网站开发软件关键词排名代发
  • visio画网站开发类图注册域名后怎么建网站
  • 道里网站运营培训北京网络营销咨询公司
  • 目前做网站流行的语言seo关键词排名优化哪家好
  • 长沙营销型网站制作费用seo图片优化
  • 学生诚信档案建设网站seo数据分析
  • 北京住房城乡建设厅网站首页1688官网入口
  • 网站建设需要懂什么软件徐州百度seo排名优化
  • wordpress网站样式网站排名查询