// binarySearchTree.cpp -- 2011-07-27-17.18
#include "stdafx.h"
#include "binarySearchTree.h"
#include <iostream>
// private methods
binarySearchTree ::Node * binarySearchTree ::m_makeNode (const Item item)
{
Node * newNode = new Node ;
if (NULL == newNode)
return NULL ;
newNode ->item = item ;
newNode ->left = newNode ->right = NULL ;
return newNode ;
}
void binarySearchTree ::m_print (const Node & node)
{
if (&node != NULL)
{
m_print(*(node.left)) ;
std ::cout << node.item << std ::endl ;
m_print(*(node.right)) ;
}
}
binarySearchTree ::Node * binarySearchTree :: m_leftmostOrRightmostNode (Node * const pNode, const direction directionComeFrom)
{
if (pNode != NULL)
{
if (pNode ->left != NULL)
{
Node * returnValueRecord = m_leftmostOrRightmostNode(pNode ->left, Direction_Left) ;
returnValueRecord ->right = pNode ;
pNode ->left = returnValueRecord ;
}
if (pNode ->right != NULL)
{
Node * returnValueRecord = m_leftmostOrRightmostNode(pNode ->right, Direction_Right) ;
returnValueRecord ->left = pNode ;
pNode ->right = returnValueRecord ;
}
if (Direction_Left == directionComeFrom)
{
Node * rightmost = pNode ;
while (rightmost ->right != NULL)
rightmost = rightmost ->right ;
return rightmost ;
}
else
{
Node * leftmost = pNode ;
while (leftmost ->left != NULL)
leftmost = leftmost ->left ;
return leftmost ;
}
}
else
return NULL ;
}
void binarySearchTree ::m_removeAll (Node * pNode)
{
if (pNode != NULL)
{
m_removeAll(pNode ->left) ;
m_removeAll(pNode ->right) ;
delete pNode ;
}
}
// public methods
binarySearchTree ::binarySearchTree (void)
{
m_root = NULL ;
m_current = 0 ;
}
bool binarySearchTree ::isEmpty (void)
{
if (0 == m_current)
return true ;
else
return false ;
}
bool binarySearchTree ::insert (const Item item)
{
Node * newNode = m_makeNode(item) ;
if (NULL == newNode)
return false ;
if (isEmpty())
{
m_root = newNode ;
++m_current ;
}
else
{
Node * scan = m_root ;
while (1)
{
if (item < scan ->item)
{
if (scan ->left != NULL)
{
scan = scan ->left ;
}
else
{
scan ->left = newNode ;
break ;
}
}
else
{
if (scan ->right != NULL)
{
scan = scan ->right ;
}
else
{
scan ->right = newNode ;
break ;
}
}
}
++m_current ;
}
return true ;
}
bool binarySearchTree ::remove (const Item item)
{
if (isEmpty ())
return false ;
Node * scan = m_root, * parent = NULL ;
while (scan != NULL)
{
if (item < scan ->item)
{
parent = scan ;
scan = scan ->left ;
}
else if (item > scan ->item)
{
parent = scan ;
scan = scan ->right ;
}
else
// Target it !
break ;
}
if (NULL == scan)
return false ;
if (NULL == parent)
{
// Target is root.
Node * rootRecord = m_root ;
if (NULL == m_root ->left)
m_root = m_root ->right ;
else if (NULL == m_root ->right)
m_root = m_root ->left ;
else
{
m_root = m_root ->right ;
Node * rightSubTreeOfRoot = m_root ->right ;
while (rightSubTreeOfRoot ->left != NULL)
rightSubTreeOfRoot = rightSubTreeOfRoot ->left ;
rightSubTreeOfRoot ->left = rootRecord ->left ;
}
delete rootRecord ;
} // end NULL == parent
else
{
if (NULL == scan ->left)
{
if (scan == parent ->left)
parent ->left = scan ->right ;
else
parent ->right = scan ->right ;
delete scan ;
}
else if (NULL == scan ->right)
{
if (scan == parent ->left)
parent ->left = scan ->left ;
else
parent ->right = scan ->left ;
delete scan ;
}
else
{
if (scan == parent ->left)
{
// [parent] [parent]
// / /
// [scan] => [right]
// / \ /
// [left] [right] [left]
parent ->left = scan ->right ;
Node * scanRecord = scan ;
scan = scan ->right ;
while (scan ->left != NULL)
scan = scan ->left ;
scan ->left = scanRecord ->left ;
delete scanRecord ;
}
else
{
// [parent] [parent]
// \ \
// [scan] => [right]
// / \ /
// [left] [right] [left]
parent ->right = scan ->right ;
Node * scanRecord = scan ;
scan = scan ->right ;
while (scan ->left != NULL)
scan = scan ->left ;
scan ->left = scanRecord ->left ;
delete scanRecord ;
}
}
}
--m_current ;
return true ;
}
void binarySearchTree ::print (void)
{
m_print(*m_root) ;
}
void binarySearchTree ::becomeToATwoWayCirculationLinkedListAndPrintItAndEmptyTheTree (const direction headDirection)
{
using std ::cout ;
using std ::endl ;
if (headDirection != Direction_Left && headDirection != Direction_Right)
return ;
Node * returnValueRecord = m_leftmostOrRightmostNode(m_root, headDirection) ;
if (Direction_Left == headDirection)
{
cout << "In diminsihing order: " ;
Node * rightmost = returnValueRecord ;
while (rightmost != NULL)
{
cout << rightmost ->item << ' ' ;
rightmost = rightmost ->left ;
}
cout << endl ;
// Find leftmost node
while (returnValueRecord ->left != NULL)
returnValueRecord = returnValueRecord ->left ;
cout << "In increment order: " ;
Node * leftmost = returnValueRecord ;
while (leftmost != NULL)
{
cout << leftmost ->item << ' ' ;
leftmost = leftmost ->right ;
}
cout << endl ;
// Empty the list.
leftmost = returnValueRecord ;
while (leftmost != NULL)
{
Node * temp = leftmost ;
leftmost = leftmost ->right ;
delete temp ;
}
m_root = NULL ;
m_current = 0 ;
}
else
{
cout << "In increment order: " ;
Node * leftmost = returnValueRecord ;
while (leftmost != NULL)
{
cout << leftmost ->item << ' ' ;
leftmost = leftmost ->right ;
}
cout << endl ;
// Find rightmost node
while (returnValueRecord ->right != NULL)
returnValueRecord = returnValueRecord ->right ;
cout << "In diminsihing order: " ;
Node * rightmost = returnValueRecord ;
while (rightmost != NULL)
{
cout << rightmost ->item << ' ' ;
rightmost = rightmost ->left ;
}
cout << endl ;
// Empty the list.
leftmost = returnValueRecord ;
while (leftmost != NULL)
{
Node * temp = leftmost ;
leftmost = leftmost ->right ;
delete temp ;
}
m_root = NULL ;
m_current = 0 ;
}
}
binarySearchTree ::~binarySearchTree (void)
{
m_removeAll(m_root) ;
m_root = NULL ;
m_current = 0 ;
}
二叉搜索树就地转双向链表二叉搜索树实现文件C++
最新推荐文章于 2024-09-23 20:41:21 发布