今天又写了delete的部分,时间就这么被一天天地浪费了,我感到十分惋惜呀
 #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的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. VMware中创建Ubuntu16.0.4虚拟桥连方式无法上网

    一.问题描述 在本地VMvare中已经安装了两台虚拟机,网络方式都是桥连,上网都可以自动获取IP地址 和HOST主机是一个号段的 ,同为192.168.1.X KingServer1(原始安装)  桥 ...

  2. (转)【风宇冲】Unity3D教程宝典之Blur

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                   BlurBlur模糊其实理解了以后非常简单.核心原理就是 1个点的颜色 并不用该点的颜色,而是用该点周围 ...

  3. Discuz常见小问题-如何关闭验证码

    进入后台,在防灌水,验证设置中可以切换哪些情况下是否使用验证码 如果启用验证码,也客户修改验证码的难度,样式.最后点击提交,完成之后可以退出到前台,测试是否能够不用验证码自动登录

  4. STL 笔记(四) 迭代器 iterator

    stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators   仅仅读,输 ...

  5. C/C++——程序的内存分配

    C/C++程序内存分配 一.预备知识-程序的内存分配 一个由c/C++编译的程序占用的内存分为下面几个部分 1.栈区(stack):由编译器自己主动分配释放 ,存放函数的參数值,局部变量的值等.其操作 ...

  6. sudo 和环境变量

    https://askubuntu.com/questions/57915/environment-variables-when-run-with-sudo https://www.phusionpa ...

  7. Android学习笔记二:activity的理解

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513290.html 一:activity定义了app的页面 一个app有很多个页面组成,一个页面其实就是一个 ...

  8. Oracle NET工作原理、配置及连接问题排查

    一.Oracle NET配置文件 Oracle NET是一个软件层,支持不同网络协议之间的转换.不同的物理机器可以借助这个软件层实现相互间的通信,具体而言就是实现对oracle的远程访问. oracl ...

  9. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  10. ubuntu下查询SSH状态和安装SSH服务

    1.查询SSH的安装状态 rpm -qa |grep ssh 上面的命令可能出现提示说rpm未安装,可以使用下面这命令进行安装 sudo apt-get install rpm 某些帖子上也可以使用y ...