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 ...
随机推荐
- 用jstl标签判断一个字符串是否包含了另一个字符串
<c:if test="${fn:contains(str1,str2)}">
- Android -- queryIntentActivities
某些时候你想要知道某个APP是否有注册了一个明确的intent,比如说你想要检查某个receiver是否存在,然后根据是否存在来这个receiver来在你的AP里面enable某些功能.我们可以通过P ...
- 【Spark】SparkStreaming-流处理-规则动态更新-解决方案
SparkStreaming-流处理-规则动态更新-解决方案 image2017-10-27_11-10-53.png (1067×738) elasticsearch-head Elasticsea ...
- POI中setDefaultColumnWidth方法不起作用的原因
sheet.setDefaultRowHeight((short) (2 * 256)); //设置默认行高,表示2个字符的高度 sheet.setDefaultColumnWidth(17); ...
- Python引用(import)文件夹下的py文件的方法
Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...
- [幽默漫画]对于程序猿来说deadline很容易搞定!
更多漫画在这里
- WPF按钮删除默认的鼠标悬停效果
<Style x:Key="NormalMouseButton" TargetType="Button"> <Setter Property= ...
- 【树莓派】【转】树莓派3装Android 6.0,支持Wi-Fi和蓝牙
树莓派3装Android 6.0,支持Wi-Fi和蓝牙 相信对于许多树莓派初学者(包括我)来说,Android系统的确是一个不错的选择.但国内这方面资源稀缺,经本人FQ苦寻,找到了老外的树莓派Andr ...
- Linux中Sed的用法
Linux中Sed的用法 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: ...
- 低危漏洞- X-Frame-Options Header未配置
原文链接:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/X-Frame-Options?redirectlocale=en-US&redi ...