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 ...
随机推荐
- Android -- Property Animation
3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...
- grunt-cmd-transport提取deps[]的BUG
该BUG已经在GitHub上提了issue,详见:#56 文件 // employee/static/adder.js define(function (require, exports) { exp ...
- SSE,MSE,RMSE,R-square指标讲解
SSE(和方差.误差平方和):The sum of squares due to errorMSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...
- tensorflow_python中文手册
https://www.tensorflow.org/api_docs/python/tf/nn/static_bidirectional_rnn https://www.w3cschool.cn/t ...
- List 转 ObservableCollection
ObservableCollection<UserInfo> oc = new ObservableCollection<UserInfo>(); ls.ForEach(x = ...
- Spring Boot集成持久化Quartz定时任务管理和界面展示
本文是对之前的一篇文章Spring+SpringMVC+mybatis+Quartz整合代码部分做的一个修改和补充, 其中最大的变化就是后台框架变成了Spring Boot. 本工程所用到的技术或工具 ...
- Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)
通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...
- MonoDB的数据准备
首先是数据的录入,为了分析我们服务器集群的性能,需要准备大量的用户数据,幸运的是mtools提供了mgenerate方法供我们使用.他可以根据一个数据模版向 MongoDB 中插入任意条 json ...
- 可以在任何时候attach一个shader到program对象
可以在任何时候attach一个shader到program对象,不一定非要在指定source和编译以后,具体的描述如下: Once you have a program object create ...
- 《OpenGL® ES™ 3.0 Programming Guide》读书笔记1 ----总览
OpenGL ES 3.0 Graphics Pipeline OpenGL ES 3.0 Vertex Shader Transform feedback: Additionally, OpenGL ...