今天又写了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. python 获取几小时之前,几分钟前,几天前,几个月前,及几年前的具体时间

    引入以下两个包: import datetime import arrow 具体代码 # import datetime # import arrow def getTime(self, flag,d ...

  2. [Algorithm] Longest Substring Without Repeating Characters?

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  3. 转载:Unicode和Utf-8有何区别 转载自知乎 原文作者不详

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  4. C++中public、protected、private的差别

    第一: private,public,protected的訪问范围:   private: 仅仅能由该类中的函数.其友元函数訪问,不能被不论什么其它訪问.该类的对象也不能訪问. protected: ...

  5. HDU1069(还是dp基础)

    今天不想说太多废话-由于等下要写自己主动提交机. 不知道能不能成功呢? 题目的意思就是,一个猴子,在叠砖头 ...以下的要严格大于上面的.求叠起来最高能到多少- n非常少,n^2算法毫无压力-话说dp ...

  6. Spring boot 各种入门及问题

    Spring boot 入门 整合(完整版): https://blog.csdn.net/winter_chen001/article/details/77249029 mybatis-genera ...

  7. css规范 - bem

    用我的话简述来说,即 B:何种元素 E:何种模块使用它(header,footer)等 M:描述它是做何种事情的 例如就是我有个主页,名称是:index.html index_header_logo ...

  8. vuejs 过渡效果

    过渡效果 https://cn.vuejs.org/v2/guide/transitions.html http://router.vuejs.org/zh-cn/advanced/transitio ...

  9. Java Netty (1)

    Netty是由JBOSS提供的一个java开源框架,本质上也是NIO,是对NIO的封装,比NIO更加高级,功能更加强大.可以说发展的路线是IO->NIO->Netty. ServerBoo ...

  10. 8个很实用的在线工具来提高你的Web设计和开发能力

    近期在网上看到一张帖子感觉不错.只是是英文版的今天闲着没事锻炼一下英语能力分享给大家看看! 原文地址:http://webdesignledger.com/tools/8-useful-online- ...