Data Structures (Weiss) Chapter 4: Binary Search Tree C++
2013-07-09 16:27
337 查看
//
// 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);
else
if( x<t->element) insert(x,t->left);
else
if( 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);
else
if( x>t->element) remove(x,t->right);
else
if( 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)return
NULL;
if( t->left==NULL)return t;
return findMin(t->left);
}
BinaryNode *findMax(BinaryNode *t)const
{
if( t==NULL)return
NULL;
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)return
NULL;
return
new 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)return
false;
else
if( x<t->element)return contains(x,t->left);
else
if( 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;
}
// 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);
else
if( x<t->element) insert(x,t->left);
else
if( 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);
else
if( x>t->element) remove(x,t->right);
else
if( 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)return
NULL;
if( t->left==NULL)return t;
return findMin(t->left);
}
BinaryNode *findMax(BinaryNode *t)const
{
if( t==NULL)return
NULL;
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)return
NULL;
return
new 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)return
false;
else
if( x<t->element)return contains(x,t->left);
else
if( 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;
}
相关文章推荐
- [C++]LeetCode: 122 Validate Binary Search Tree (是否是合法BST树)
- leetcode_c++:树:Convert Sorted Array to Binary Search Tree(108)
- Binary Search Tree (BST) Implementation C++
- Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树
- binary search tree in C++
- 二叉搜索树的查询、插入与删除操作(Binary Search Tree, Search, Insert, Delete)(C++)
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Binary Search Tree的实现 C++版
- Binary Search Tree 二叉搜索树 C++
- Introduction to Algorithms Chapter 12: Binary Search Tree
- LeetCode刷题(C++)——Convert Sorted List to Binary Search Tree(Normal)
- leetcode_c++:树: Recover Binary Search Tree(099)
- PAT Build A Binary Search Tree[C++]
- Binary Search Tree 二叉搜索树 C++
- 【C++】 LeetCode 98. Validate Binary Search Tree
- Binary Search Tree Insertion in C++
- C++数据结构 之 二叉搜索树_Binary Search Tree
- 【C++】【LeetCode】108. Convert Sorted Array to Binary Search Tree
- 108. Convert Sorted Array to Binary Search Tree c++
- leetcode_c++:链表:Convert Sorted List to Binary Search Tree(109)