二叉搜索树就地转双向链表二叉搜索树实现文件C++
2011-07-28 18:01
585 查看
// 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 ; }
相关文章推荐
- 剑指offer第二十六题【二叉搜索树与双向链表】c++实现
- 多文件编程动态开辟空间实现双向链表的应用 c++版
- C++ 构造双向链表的实现代码
- C++ 学习练手 - 双向链表的模板实现
- c++模板实现双向链表
- C++实现双向链表
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- c++实现双向链表,类模板双向链表
- C++ 实现双向链表的实例
- 用C++实现的双向链表
- C++ 实现双向链表
- c++实现双向循环链表
- 【C++】双向链表的实现
- 数据结构与算法分析笔记与总结(java实现)--二叉树22:二叉搜索树与双向链表
- C++实现双向链表的创建,插入,修改,删除
- c++双向链表的实现_1.0版本
- 使用C++实现的双向链表
- C++实现双向链表
- 【头文件】c++实现链表
- C++实现有哨兵的双向循环链表