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 low memory killer
对于PC来说,内存是至关重要.如果某个程序发生了内存泄漏,那么一般情况下系统就会将其进程Kill掉.Linux中使用一种名称为OOM(Out Of Memory,内存不足)的机制来完成这个任务,该机制 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(三)安装spark2.2.1
如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...
- Web项目MySQL配置文件运维
root@mysqltest:/etc/mysql/mysql.conf.d# cat mysqld.cnf # # The MySQL database server configuration f ...
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...
- mysql zerofill 的使用
转自:http://www.jquerycn.cn/blog/mysql/ 那这个int[M]中M是什么意义喃,在定义数值型数据类型的时候,可以在关键字括号内指定整数值(如:int(M),M的最大值为 ...
- 线程:主线程、子线程 同步线程、异步线程 单线程、多线程 System.Threading与System.Windows.Threading
入门-------------------------------------------------------------------------------- 概述与概念 一个C#程序开始 ...
- FIS前端集成解决方案
FIS前端集成解决方案-文档结构 什么是FIS 部署FIS FIS基本使用 模块定义 加载方式 调用Tangram 2.0 FIS开发实例 --附件下载-- 什么是FIS FIS提供了一套贯穿开发流程 ...
- 【树莓派】服务配置相关2:基于RPi Desktop的服务配置
该文接续之前写过的一篇:[树莓派]服务配置相关. 这是我个人用来进行树莓派盒子安装配置的脚本,对于外部其他博友,可以部分参考,但不需要逐个引用. 现在有一定更新,部分按如下脚本来操作: step1: ...
- Java 通配符匹配查找文件
比较了一下Java正则表达式与通配符之间的差别,很简单的进行了一下转化就行了.此外要注意String的replace和replaceAll的用法的含义,不要搞错了. 字符串匹配例子 String s ...
- 浅谈mysql中utf8和utf8mb4区别
转自:http://ourmysql.com/archives/1402 实践过程中发现有时mysql的字符集会引起故障,所以需要了解下这个知识点. 一.简介 MySQL在5.5.3之后增加了这个u ...