Data Structures (Weiss) Chapter 4: Binary Search Tree C++
来源:互联网 发布:win10 软件桌面图标 编辑:程序博客网 时间:2024/06/05 18:11
//
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structure C++, Weiss, P.126 BinarySearchTree
// add a function PrintTree
#include <iostream>
using namespace std;
template<typename Comparable>
class BinarySearchTree
{
public:
BinarySearchTree() { root=NULL;}
BinarySearchTree(const BinarySearchTree &rhs)
{
operator=(rhs);
}
~BinarySearchTree()
{
makeEmpty();
}
const BinarySearchTree &operator=(const BinarySearchTree &rhs)
{
if(this!=&rhs)
{
makeEmpty();
root=clone(rhs.root);
}
return *this;
}
const Comparable &findMin()const
{
return findMin(root)->element;
}
const Comparable &findMax()const
{
return findMax(root)->element;
}
bool contains(const Comparable &x)const;
bool isEmpty()const;
void printTreePreOrder()const
{
printTreePreOrder(root);
cout<<endl;
}
void printTreeInOrder()const
{
printTreeInOrder(root);
cout<<endl;
}
void printTreePostOrder()const
{
printTreePostOrder(root);
cout<<endl;
}
void printTree()const
{
PrintTree(root,0);
}
void makeEmpty();
void insert(const Comparable &x);
void remove(const Comparable &x);
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt)
: element(theElement), left(lt), right(rt){}
};
BinaryNode *root;
void insert(const Comparable &x, BinaryNode * &t)const
{
if( t==NULL) t=new BinaryNode(x,NULL,NULL);
elseif( x<t->element) insert(x,t->left);
elseif( x>t->element) insert(x,t->right);
else ;//duplicate, do nothing.
}
void remove(const Comparable &x, BinaryNode * &t)const
{
if( t==NULL)return;
if( x<t->element) remove(x,t->left);
elseif( x>t->element) remove(x,t->right);
elseif( t->left!=NULL && t->right!=NULL)
{
t->element=findMin(t->right)->element;
remove( t->element, t->right);
}
else
{
BinaryNode *oldNode=t;
t=(t->left!=NULL) ? t->left : t->right;
delete oldNode;
}
}
BinaryNode *findMin(BinaryNode *t)const
{
if( t==NULL)returnNULL;
if( t->left==NULL)return t;
return findMin(t->left);
}
BinaryNode *findMax(BinaryNode *t)const
{
if( t==NULL)returnNULL;
if( t->right==NULL)return t;
return findMax(t->right);
}
bool contains(const Comparable &x, BinaryNode *t)const;
void makeEmpty( BinaryNode * &t)
{
if( t!=NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
void printTreePreOrder( BinaryNode *t)const
{
if( t!=NULL)
{
cout<<t->element<<" ";
printTreePreOrder(t->left);
printTreePreOrder(t->right);
}
}
void printTreeInOrder( BinaryNode *t)const
{
if( t!=NULL)
{
printTreeInOrder(t->left);
cout<<t->element<<" ";
printTreeInOrder(t->right);
}
}
void printTreePostOrder( BinaryNode *t)const
{
if( t!=NULL)
{
printTreePostOrder(t->left);
printTreePostOrder(t->right);
cout<<t->element<<" ";
}
}
BinaryNode *clone( BinaryNode *t)const
{
if( t==NULL)returnNULL;
returnnew BinaryNode(t->element, clone(t->left), clone(t->right));
}
void PrintTree( BinaryNode *t,int level) const
{
if( t!=NULL)
{
PrintTree( t->right, level+1);
for(int i=0; i<6*level; ++i)
cout<<" ";
cout<<t->element<<endl;
PrintTree(t->left,level+1);
}
}
};
template<typename Comparable>
bool BinarySearchTree<Comparable>::contains(const Comparable &x)const
{
return contains(x,root);
}
template<typename Comparable>
void BinarySearchTree<Comparable>::insert(const Comparable &x)
{
insert(x,root);
}
template<typename Comparable>
void BinarySearchTree<Comparable>::remove(const Comparable &x)
{
remove(x,root);
}
template<typename Comparable>
bool BinarySearchTree<Comparable>::contains(const Comparable &x, BinaryNode *t)const
{
if( t==NULL)returnfalse;
elseif( x<t->element)return contains(x,t->left);
elseif( x>t->element)return contains(x,t->right);
else returntrue;
}
template<typename Comparable>
void BinarySearchTree<Comparable>::makeEmpty()
{
makeEmpty(root);
}
template<typename Comparable>
bool BinarySearchTree<Comparable>::isEmpty()const
{
return root==NULL;
}
int main()
{
BinarySearchTree<int> T;
for(int i=5; i<25; ++i)
T.insert(i%10);
T.printTreeInOrder();
T.printTreePreOrder();
T.printTreePostOrder();
T.printTree();
BinarySearchTree<int> T1(T), T2;
T2=T;
T1.printTreeInOrder();
T1.printTreePreOrder();
T1.printTreePostOrder();
T2.printTreeInOrder();
T2.printTreePreOrder();
T2.printTreePostOrder();
T2.remove(5);
T2.printTreeInOrder();
T2.printTreePreOrder();
T2.printTreePostOrder();
return 0;
}
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 12: Splay Tree 伸展树
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 3: List C++
- Data Structures (Weiss) Chapter 5: Separate Chain
- Data Structures (Weiss) Chapter 6: Leftist Heap
- Data Structures (Weiss) Chapter 6: Binomial Queue
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- Data Structures (Weiss) Chapter 8: Union and Find, Disjoint Sets, union by sizes, C++
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- 骨骼蒙皮动画(Skinned Mesh)的原理解析(二)
- 【IOS开发】UITextField 常用属性
- OpenCV源码中Haar训练及提取特征的代码
- 电路设计——电容设计
- Kindle刚入华,电子书瞬间被破解!
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- 线段树——HDU1255 覆盖的面积解题报告
- 一道有意思的acm: 1789. Searching for the Dodecahedron
- C# JSON 转List<T>类
- 为公司做好长远计划company plan ahead
- DevSuite项目管理平台助力莱芜钢铁集团,有效管控并行项目
- zoj 1095 丑数、1151反转单词、1088 约瑟夫环
- hud 2151 Worm
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树