| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 831 人关注过本帖
标题:[开源][c++]模版类构建2叉搜索树!(更新中……)
只看楼主 加入收藏
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
 问题点数:0 回复次数:3 
[开源][c++]模版类构建2叉搜索树!(更新中……)

#ifndef BSTREE_H
#define BSTREE_H

#include <iostream>

//tree node
template<class T>
struct node{
node(const T &val) : data(val),left(NULL),right(NULL){}
T data;
node *left;
node *right;
};

//binay search tree
template<class T>
class BSTree{
public:
BSTree() : root(NULL){}

//copy control
~BSTree();
BSTree(const BSTree<T>&);
BSTree& operator= (const BSTree<T>&);
//member functions

void add(const T&); //add an element to the tree

void pre_print() const{pre_recursive(root);} //print the tree by pre order
void in_print() const{in_recursive(root);} //print the tree by in order
void post_print() const{post_recursive(root);} //print the tree by post order

void clear(){clr_recursive(root);root = NULL;} //delete all the nodes

bool empty() const{return root == NULL;}
int size() const{return size_recursive(root);} //count how many nodes
int height() const{return height_recursive(root);} //count the hight of a tree

node<T>* get_root() const {return root;}
protected:
//auxiliary functions
void pre_recursive(node<T>*) const;
void in_recursive(node<T>*) const;
void post_recursive(node<T>*) const;

void clr_recursive(node<T>*);
int size_recursive(node<T>*) const;
int height_recursive(node<T>*)const;
private:
//data member
node<T> *root;
};

template<class T>
BSTree<T>::~BSTree(){
clear();
}

template<class T>
void BSTree<T>::add(const T &val){
if (root == NULL)
root = new node<T>(val);
else{
node<T> *new_node = new node<T>(val);
node<T> *curr = root;
while (true){
if (val < curr->data){
if (curr->left == NULL){
curr->left = new_node;
break;
}
else
curr = curr->left;
}
else{
if (curr->right == NULL){
curr->right = new_node;
break;
}
else
curr = curr->right;
}
}
}
}

template<class T>
void BSTree<T>::pre_recursive(node<T> *pre_root) const{
if (pre_root){
std::cout << pre_root->data << " ";
pre_recursive(pre_root->left);
pre_recursive(pre_root->right);
}
}

template<class T>
void BSTree<T>::in_recursive(node<T> *in_root) const{
if (in_root){
in_recursive(in_root->left);
std::cout << in_root->data << " ";
in_recursive(in_root->right);
}
}

template<class T>
void BSTree<T>::post_recursive(node<T> *post_root) const{
if (post_root){
post_recursive(post_root->left);
post_recursive(post_root->right);
std::cout << post_root->data << " ";
}
}

template<class T>
void BSTree<T>::clr_recursive(node<T> *clr_root){
if (clr_root){
clr_recursive(clr_root->left);
clr_recursive(clr_root->right);
delete clr_root;
}
}

template<class T>
int BSTree<T>::size_recursive(node<T> *size_root) const{
if (size_root){
return size_recursive(size_root->left)
+ size_recursive(size_root->right)+1;
}
return 0;
}

template<class T>
int BSTree<T>::height_recursive(node<T> *height_root) const{
int lh,rh;
if (height_root){
lh = height_recursive(height_root->left);
rh = height_recursive(height_root->right);
return (lh > rh ? lh : rh) + 1;
}
return 0;
}

#endif


数据结构学的不是很好,不足地方请指出下!还有什么需要改进的?

再次更新……谢谢了!

[此贴子已经被作者于2007-5-23 11:31:23编辑过]

搜索更多相关主题的帖子: 模版 开源 搜索 构建 
2007-05-22 09:01
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

补充一点:(解决了)
可以看到我析够函数那里是注释掉的,因为我写不出来。
给我说说怎么写!!!谢谢!!!

[此贴子已经被作者于2007-5-23 10:57:50编辑过]


Fight  to win  or  die...
2007-05-22 10:32
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
龙龙的老公(好记些):

我那个求树高的递归方法是copy的你的,给我讲讲啊,不明白!

Fight  to win  or  die...
2007-05-23 10:59
neverDie
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2007-5-5
收藏
得分:0 

看不懂,代码真工整啊!


2007-05-24 11:36
快速回复:[开源][c++]模版类构建2叉搜索树!(更新中……)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018010 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved