binary search tree study
- #pragma once
- #include "stdio.h"
- struct Node
- {
- Node(int aValue)
- :m_Value(aValue)
- ,m_pLeft(NULL)
- ,m_pRight(NULL)
- ,m_pParent(NULL)
- {
- }
- int m_Value;
- Node* m_pLeft;
- Node* m_pRight;
- Node* m_pParent;
- };
- class BinarySearchTree
- {
- public:
- BinarySearchTree(void);
- ~BinarySearchTree(void);
- void Init();
- void Insert(int aValue);
- void Delete(int aValue);
- Node* MaxNode(Node* apNode);
- Node* MinNode(Node* apNode);
- Node* Search(int aValue);
- void PreOrderPrint();
- void AfterOrderPrint();
- void MidOrderPrint();
- void PrintNode( Node* lpNode )
- {
- printf("%d ", lpNode->m_Value);
- }
- Node* Successor(Node* aNode);
- private:
- Node* m_pRootNode;
- };
- #include "BinarySearchTree.h"
- #include <stack>
- #include <queue>
- BinarySearchTree::BinarySearchTree(void)
- :m_pRootNode(NULL)
- {
- }
- BinarySearchTree::~BinarySearchTree(void)
- {
- }
- void BinarySearchTree::Insert( int aValue )
- {
- Node* lpNewNode = new Node(aValue);
- if (NULL == m_pRootNode)
- {
- m_pRootNode = lpNewNode;
- }
- else
- {
- Node* lpNode = m_pRootNode;
- Node* lpPNode = NULL;
- while(lpNode)
- {
- lpPNode = lpNode;
- if (lpNode->m_Value > aValue)
- {
- lpNode = lpNode->m_pLeft;
- }
- else
- {
- lpNode = lpNode->m_pRight;
- }
- }
- if (lpPNode->m_Value > aValue)
- {
- lpPNode->m_pLeft = lpNewNode;
- }
- else
- {
- lpPNode->m_pRight = lpNewNode;
- }
- lpNewNode->m_pParent = lpPNode;
- }
- }
- void BinarySearchTree::Init()
- {
- }
- void BinarySearchTree::Delete( int aValue )
- {
- Node* lpNode = Search(aValue);
- if (NULL == lpNode)
- {
- return;
- }
- Node* lpDeleteNode = NULL;
- if (!lpNode->m_pLeft && !lpNode->m_pRight)
- {
- if (lpNode->m_pParent->m_pLeft = lpNode)
- {
- lpNode->m_pParent->m_pLeft = NULL;
- }
- else
- {
- lpNode->m_pParent->m_pRight = NULL;
- }
- delete lpNode;
- }
- else
- {
- if (!lpNode->m_pLeft && lpNode->m_pRight)
- {
- if (lpNode->m_pParent->m_pLeft == lpNode)
- {
- lpNode->m_pParent->m_pLeft = lpNode->m_pRight;
- lpNode->m_pRight->m_pParent = lpNode->m_pParent;
- }
- else
- {
- lpNode->m_pParent->m_pRight = lpNode->m_pRight;
- lpNode->m_pRight->m_pParent = lpNode->m_pParent;
- }
- delete lpNode;
- lpNode =NULL;
- }
- else if (lpNode->m_pLeft && !lpNode->m_pRight)
- {
- if (lpNode->m_pParent->m_pLeft == lpNode)
- {
- lpNode->m_pParent->m_pLeft = lpNode->m_pLeft;
- lpNode->m_pLeft->m_pParent = lpNode->m_pParent;
- }
- else
- {
- lpNode->m_pParent->m_pRight = lpNode->m_pLeft;
- lpNode->m_pLeft->m_pParent = lpNode->m_pParent;
- }
- delete lpNode;
- lpNode = NULL;
- }
- else
- {
- Node* lpSuccessorNode = Successor(lpNode);
- lpNode->m_Value = lpSuccessorNode->m_Value;
- if (lpSuccessorNode->m_pRight)
- {
- lpSuccessorNode->m_pParent->m_pLeft = lpSuccessorNode->m_pRight;
- lpSuccessorNode->m_pRight->m_pParent = lpSuccessorNode->m_pParent;
- }
- delete lpSuccessorNode;
- lpSuccessorNode = NULL;
- }
- }
- }
- void BinarySearchTree::PreOrderPrint()
- {
- std::queue<Node*> lStack;
- Node* lpCurNode = m_pRootNode;
- if (NULL != lpCurNode)
- {
- lStack.push(lpCurNode);
- while(!lStack.empty())
- {
- Node* lpNode = lStack.front();
- lStack.pop();
- PrintNode(lpNode);
- if (lpNode->m_pLeft)
- {
- lStack.push(lpNode->m_pLeft);
- }
- if (lpNode->m_pRight)
- {
- lStack.push(lpNode->m_pRight);
- }
- }
- }
- }
- void BinarySearchTree::AfterOrderPrint()
- {
- std::stack<Node*> lStack;
- Node* lpCurNode = m_pRootNode;
- bool lDone = true;
- while(!lStack.empty() || lpCurNode)
- {
- if (lpCurNode)
- {
- lStack.push(lpCurNode);
- lpCurNode = lpCurNode->m_pRight;
- }
- else
- {
- lpCurNode = lStack.top();
- lStack.pop();
- PrintNode(lpCurNode);
- lpCurNode = lpCurNode->m_pLeft;
- }
- }
- }
- void BinarySearchTree::MidOrderPrint()
- {
- Node* lpNode = m_pRootNode;
- std::stack<Node*> lQueue;
- while(NULL != lpNode || !lQueue.empty())
- {
- if (NULL != lpNode)
- {
- lQueue.push(lpNode);
- lpNode = lpNode->m_pLeft;
- }
- else
- {
- lpNode = lQueue.top();
- lQueue.pop();
- PrintNode(lpNode);
- lpNode = lpNode->m_pRight;
- }
- }
- }
- Node* BinarySearchTree::MinNode(Node* apNode)
- {
- Node* lpPreNode = NULL;
- Node* lpNode = apNode;
- while(lpNode)
- {
- lpPreNode = lpPreNode;
- lpNode = lpNode->m_pLeft;
- }
- return lpPreNode;
- }
- Node* BinarySearchTree::MaxNode(Node* apNode)
- {
- Node* lpPreNode = NULL;
- Node* lpNode = apNode;
- while(lpNode)
- {
- lpPreNode = lpPreNode;
- lpNode = lpNode->m_pRight;
- }
- return lpPreNode;
- }
- Node* BinarySearchTree::Successor(Node* aNode)
- {
- if (NULL == m_pRootNode)
- {
- return NULL;
- }
- Node* lpNode = aNode;
- if (lpNode->m_pRight)
- {
- return MinNode(lpNode->m_pRight);
- }
- else
- {
- while(lpNode && lpNode->m_pParent->m_pRight == lpNode)
- {
- lpNode = lpNode->m_pParent;
- }
- return lpNode;
- }
- }
- Node* BinarySearchTree::Search( int aValue )
- {
- Node* lpNode = m_pRootNode;
- while(lpNode)
- {
- if (lpNode->m_Value > aValue)
- {
- lpNode = lpNode->m_pLeft;
- }
- else if (lpNode->m_Value < aValue)
- {
- lpNode = lpNode->m_pRight;
- }
- else
- {
- return lpNode;
- }
- }
- return NULL;
- }
binary search tree study的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- iOS中的时钟动画
iOS 动画效果非常多,我们在开发中可能会遇到很多动画特效,我们就会用到核心动画框架CoreAnimation,核心动画里面的动画效果有很多,都是在QuartzCore.framework框架里面,今 ...
- Linux下简单线程池的实现
大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个服务请求到达,就创建一个新的服务 ...
- SSE,MSE,RMSE,R-square指标讲解
SSE(和方差.误差平方和):The sum of squares due to errorMSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...
- 转:无监督特征学习——Unsupervised feature learning and deep learning
http://blog.csdn.net/abcjennifer/article/details/7804962 无监督学习近年来很热,先后应用于computer vision, audio clas ...
- Spark Strcutured Streaming中使用Dataset的groupBy agg 与 join 示例(java api)
Dataset的groupBy agg示例 Dataset<Row> resultDs = dsParsed .groupBy("enodeb_id", "e ...
- Java归去来第2集:利用Eclipse创建Maven Web项目
一.前言 如果还不了解剧情,请返回第一集的剧情 Java归去来第1集:手动给Eclipse配置Maven环境 二.利用Eclipse创建Maven Web项目 选择File-New- ...
- Sqlserver 2008 R2安装的盘符空间不够用的解决办法
例如我把一个sqlserver数据库安装在了D盘,结果发现D盘只剩下20G的可用空间,可是数据却每天的在增长,如何办?于是百度到了以下解决办法 方法很多: 1.可以给primary文件组添加文件.选择 ...
- (转)Unity3D研究院之Assetbundle的原理(六十一)
Assetbundle 是Unity Pro提供提供的功能,它可以把多个游戏对象或者资源二进制文件封装到Assetbundle中,提供了封装与解包的方法使用起来很便利. 1.预设 A ...
- [Canvas]奔跑的马
下载地址:https://files.cnblogs.com/files/xiandedanteng/52-WalkingHorse.rar,请用Chrome浏览器打开观看动态效果. 图例: 源码: ...
- python 读取单所有json数据写入mongodb(单个)
<--------------主函数-------------------> from pymongo import MongoClientfrom bson.objectid impor ...