Binary Search Tree的实现 C++版
程序员文章站
2022-05-05 19:58:14
...
// 定义二叉检索树类
template <typename Key >
class BST
{
private:
BSTNode<Key >* root;
int nodecount;
void clearhelp(BSTNode<Key >*);
BSTNode<Key>* inserthelp(BSTNode<Key >*, const Key&);
BSTNode<Key>* deletemin(BSTNode<Key >*);
BSTNode<Key>* getmin(BSTNode<Key >*);
BSTNode<Key>* removehelp(BSTNode<Key >*, const Key&);
Key findhelp(BSTNode<Key >*, const Key&) const;
void printhelp(BSTNode<Key >*, int) const;
void preodhelper(BSTNode<Key >*) const;
void inodhelper(BSTNode<Key >*) const;
void postodhelper(BSTNode<Key >*) const;
Key sumvalue(BSTNode<Key>*);
public:
BST() {root = NULL; nodecount = 0;}
~BST() { clearhelp(root); }
void clear()
{
clearhelp(root);
root = NULL;
nodecount = 0;
}
void insert(const Key& k)
{
root = inserthelp(root, k);
nodecount ++;
}
Key remove(const Key& k)
{
Key temp = findhelp(root, k);
if(temp != NULL)
{
root = removehelp(root, k);
nodecount --;
}
return temp;
}
Key removeAny()
{
if(root != NULL)
{
Key temp = root->key();
root = removehelp(root, root->key());
nodecount --;
return temp;
}
else
return NULL;
}
Key find(const Key& k) const
{
return findhelp(root, k);
}
int size()
{
return nodecount;
}
void print() const
{
if(root == NULL)
cout << "The BST is empty." << endl;
else
printhelp(root, 0);
}
void preorder()
{
preodhelper(root);
}
void inorder()
{
inodhelper(root);
}
void postorder()
{
postodhelper(root);
}
Key sum()
{
return sumvalue(root);
}
};
template <typename Key >
Key BST<Key>::findhelp(BSTNode<Key >* root, const Key& k) const
{
if(root == NULL)
return NULL;
if(k < root->key())
return findhelp(root->left(), k);
else if(k > root->key())
return findhelp(root->right(), k);
else
return root->key();
}
template <typename Key >
BSTNode<Key>* BST<Key>::inserthelp(BSTNode<Key >* root, const Key& k)
{
if(root == NULL)
return new BSTNode<Key >(k, NULL, NULL);
if(k < root->key())
root->setLeft(inserthelp(root->left(), k));
else
root->setRight(inserthelp(root->right(), k));
return
root;
}
template <typename Key >
BSTNode<Key >* BST<Key >::deletemin(BSTNode<Key >* root)
{
if(root->left() == NULL)
return root->right();
else
{
root->setLeft(deletemin(root->left()));
return root;
}
}
template <typename Key >
BSTNode<Key >* BST<Key >::getmin(BSTNode<Key >* root)
{
if(root->left() == NULL)
return root;
else
return getmin(root->left());
}
template <typename Key >
BSTNode<Key >* BST<Key >::removehelp(BSTNode<Key >* root, const Key& k)
{
if(root == NULL)
return NULL;
else if(k < root->key())
root -> setLeft(removehelp(root->left(), k));
else if(k > root->key())
root -> setRight(removehelp(root->right(), k));
else
{
BSTNode<Key >* temp =root;
if( root->left() == NULL)
{
root = root->right();
delete temp;
}
else if(root->right() == NULL)
{
root = root->left();
delete temp;
}
else
{
BSTNode<Key >* temp = getmin(root->right());
root->setkey(temp->key());
root->setKey(temp->key());
root->setRight(deletemin(rt->right()));
delete temp;
}
}
return root;
}
template <typename Key >
void BST<Key >::clearhelp(BSTNode<Key >* root)
{
if(root == NULL)
return ;
clearhelp(root->left());
clearhelp(root->right());
delete root;
}
template <typename Key >
void BST<Key >::printhelp(BSTNode<Key >* root, int level) const
{
if(root == NULL)
return ;
printhelp(root->left(), level+1);
for(int i = 0; i<level; i++)
cout << " ";
cout << root->key() << "\n";
printhelp(root->right(), level+1);
}
// 递归,前序遍历
template <typename Key >
void BST<Key>::preodhelper(BSTNode<Key >* rt) const
{
if(rt == NULL)
return ;
cout << rt->key() << " ";
preodhelper(rt->left());
preodhelper(rt->right());
}
// 递归,中序遍历
template <typename Key >
void BST<Key>::inodhelper(BSTNode<Key >* rt) const
{
if(rt == NULL)
return ;
inodhelper(rt->left());
cout << rt->key() << " ";
inodhelper(rt->right());
}
// 递归,后序遍历
template <typename Key >
void BST<Key>::postodhelper(BSTNode<Key >* rt) const
{
if(rt == NULL)
return ;
postodhelper(rt->left());
postodhelper(rt->right());
cout << rt->key() << " ";
}
// 一棵在结点上存放整型数值的数,递归计算所有结点的数值之和
template <typename Key>
Key BST<Key>::sumvalue(BSTNode<Key>* rt)
{
if (rt == NULL)
return 0;
if ((rt->left() == NULL)&&(rt->right() == NULL))
return rt->key();
else
{
return rt->key() + sumvalue(rt->left()) + sumvalue(rt->right());
}
}
#include <iostream>
using namespace std;
// 测试用主程序
int main()
{
int a[] = {15,20,25,18,16,5,7};
int len = sizeof(a) / sizeof(a[0]);
// 创建二叉检索树
BST<int> myBST;
for(int count = 0; count < len; count++)
{
myBST.insert(a[count]);
}
// 打印二叉检索树
myBST.print();
// 前序遍历
myBST.preorder();
cout << endl;
// 中序遍历
myBST.inorder();
cout << endl;
// 后序遍历
myBST.postorder();
cout << endl;
// 打印树的所有结点的数值之和
cout << "The sum of each node's value is: " << myBST.sum() << endl;
return 0;
}
上一篇: 小步快跑是这样玩的(上)
下一篇: 软件质量基本概念 软件质量
推荐阅读
-
LeetCode 236 -- 二叉树的最近公共祖先 ( Lowest Common Ancestor of a Binary Tree ) ( C语言版 )
-
LeetCode 235--二叉搜索树的最近公共祖先 ( Lowest Common Ancestor of a Binary Search Tree ) ( C语言版 )
-
leetcode 235. Lowest Common Ancestor of a Binary Search Tree(二叉树的最低共同祖先)
-
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree C++
-
Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)
-
二叉搜索树(Binary Search Tree)(Java实现)
-
PAT甲级1043 Is It a Binary Search Tree (25分)|C++实现
-
数据结构与算法1:二叉树(binary_tree)的前、中、后序(深度优先)和广度优先遍历及python代码实现
-
二叉搜索树的删除(AldDS1_8_C:Binary Search Tree III)
-
Binary Search Tree的实现 C++版