二叉树的模板类

同样的问题,当时忘了卫星数据的模板了

//二叉树的模板类
#include <iostream>

using namespace std;

template <class Type>
struct node{
	Type value;
	node<Type> *left;
	node<Type> *right;
	node<Type> *parent;
}; 

template <class Type>
class tree_class{
	private:
		node<Type> *root;//树的类不定义额外的键值与内容空间了
		int size;
	public:
		tree_class();
		void NodeCpy(node<Type>* &,node<Type>* &);//两个函数用于递归,完成拷贝构造与析构 这个Cpy的需要用指针的引用,在函数里改变指针值 
		void NodeDel(node<Type> *);
		tree_class(tree_class<Type> &);
		~tree_class();
		bool Insert(const Type &);//插入value 
		void Show(node<Type> *);
		void ShowAll() ;//中序输出 
		node<Type> * SearchNode(node<Type> *,const Type &);
		node<Type> * Search(const Type &);//搜寻value,返回这个value的节点的指针 不存在返回NULL 
		node<Type> * Max();//最大value的节点 
		node<Type> * MinNode(node<Type> *); //从某节点找最小value的节点,没有左子树就是这个节点,有左子树就是一路向左子树搜索 
		node<Type> * Min();
		node<Type> * Replace(node<Type> * des,node<Type> * src);//src代替des的位置(鸠占鹊巢),包括des的父母节点,左右孩子节点全指向src,返回这个空出来的des ,
		//上面这个不太对,src把des占了但是src自己下面的子树全丢了,,不能把des的左右孩子节点改过来 ,只把des的父母节点改了,src再指向父母节点,相当于把src跟src的子树替换掉des 
		bool Delete(const Type&);
		
};

template <class Type>
tree_class<Type>::tree_class(){
	root = NULL;
	size = 0;
	return ;
}

template <class Type>//这里用的是指针的引用,然后就直接把des的指针改变,并且有个parent属性存在,所以至少要在函数里对下一层进行判断了。我觉得也可以用返回指针的方式,就需要在调用的时候先判断到节点了吗 
void tree_class<Type>::NodeCpy(node<Type>  * &des,node<Type> * &src){
	node<Type> *tmp;
	if(src->left!=NULL){
	//	cout<<"src,left"<<endl;
		tmp = new node<Type>;
		tmp->value = src->left->value;//这里可以再用一个临时指针,指向src的下一层 
		tmp->left = NULL;
		tmp->right = NULL;
		tmp->parent = des;
		des->left = tmp;//在des上加入了左孩子节点 
		size++;
		NodeCpy(des->left,src->left);
	}else{
		des->left =NULL;
	}
	
	if(src->right!=NULL){
	//	cout<<"src,right"<<endl;
		tmp = new node<Type>;
		tmp->value = src->right->value;
		tmp->left = NULL;
		tmp->right = NULL;
		tmp->parent = des;
		des->right = tmp;//在des上加入了右孩子节点
		size++;
		NodeCpy(des->right,src->right);
	}else{
		des->right = NULL;
	} 

	return;	
}

template <class Type>
tree_class<Type>::tree_class(tree_class<Type> & arg){
	if(arg.root==NULL){
		//cout<<"arg==NULL"<<endl;
		return;
	} 
	root = new node<Type>;
	root->value = arg.root->value;
	size = 1;
	NodeCpy(root,arg.root);
}

template <class Type>
void tree_class<Type>::NodeDel(node<Type> * arg){
	if(arg==NULL) return;
	else{
		NodeDel(arg->left);
		NodeDel(arg->right); 
		delete arg;
	}
	return ;
}
template <class Type>
tree_class<Type>::~tree_class()
{
	size = 0;
	NodeDel(root->left);
	NodeDel(root->right);
	delete root;
	return; 
	
} 

template <class Type>
bool tree_class<Type>::Insert(const Type & value){
	node<Type> *tmp;//用来遍历向下的指针 ,直到NULL 
	node<Type> *tmp_p;//用到指到当前节点的指针 
	tmp = root;
	tmp_p = NULL;
	while(tmp!=NULL){
		tmp_p = tmp;
		if(value>tmp->value) tmp = tmp->right;
		else if(value<tmp->value) tmp = tmp->left;
		else if(value==tmp->value) return false;
	} //一直搜索到了二叉树的最下层节点,最后tmp指到了这个NULL位置,tmp_p是这个NULL位置的父节点 
	tmp = new node<Type>;
	tmp->value = value;
	tmp->left = NULL;
	tmp->right = NULL;
	tmp->parent = tmp_p;
	if(tmp_p==NULL){
		root = tmp;
		return true;
	}//无元素
	else if(tmp_p->value>value){
		tmp_p->left = tmp;
		return true;
	} 
	else if(tmp_p->value<value){
		tmp_p->right = tmp;
		return true;
	}
	else{
		cout<<"logic failed"<<endl;
		return false;
	}
	
}

template <class Type>
void tree_class<Type>::Show(node<Type> * arg){
	if(arg!=NULL){
		Show(arg->left);
		cout<<"value="<<arg->value<<endl;
		Show(arg->right);
	}
	else return;
}

template <class Type>
void tree_class<Type>::ShowAll() {
	node<Type> *tmp;
	tmp = root;
	Show(tmp);
	return;
}

template <class Type>
node<Type> * tree_class<Type>::SearchNode(node<Type> * p,const Type & value){
	if(p==NULL||p->value==value) return p;
	if(p->value>value) return SearchNode(p->left,value);
	if(p->value<value) return SearchNode(p->right,value);
	
}

template <class Type>
node<Type> * tree_class<Type>::Search(const Type & value){
	//return SearchNode(root,value);
	node<Type> *tmp=root;
	while(tmp!=NULL){
		if(tmp->value==value) return tmp;
		else if(tmp->value>value) tmp = tmp->left;
		else tmp = tmp->right;
	}
	return tmp;
}

template <class Type>
node<Type> * tree_class<Type>::Max(){
	node<Type> *tmp=root;
	if(tmp==NULL) return tmp;
	while(tmp->right!=NULL){
		tmp = tmp->right;
	}
	return tmp;
}

template <class Type>
node<Type> * tree_class<Type>::MinNode(node<Type> * arg){
	node<Type> *tmp = arg;
	if(tmp==NULL) return tmp;
	while(tmp->left!=NULL){
		tmp = tmp->left;
	}
	return tmp;
}

template <class Type>
node<Type> * tree_class<Type>::Min(){
	return MinNode(root);
}

template <class Type>
node<Type> * tree_class<Type>::Replace(node<Type> * des,node<Type> * src){
	if(des->parent==NULL) root = src;
//	src->left = des->left;
//	src->right = des->right;
	
	else if(des->parent->left==des){
		des->parent->left = src;
	}else if(des->parent->right==des){
		des->parent->right = src;
	}
	if(src!=NULL)  src->parent = des->parent;
	return des; 
/*	if(des->left!=NULL){
		des->left->parent = src;
	}
	if(des->right!=NULL){
		des->right->parent = src;
	}*/
}

template <class Type>
bool tree_class<Type>::Delete(const Type & value){
	node<Type> *tmp;
	tmp = Search(value);
	if(tmp==NULL) return false;
	if(tmp->left==NULL){
		tmp=Replace(tmp,tmp->right);
		delete tmp;
		return true;
	} 
	else if(tmp->right==NULL){
		tmp=Replace(tmp,tmp->left);
		delete tmp;
		return true;
	}
	else{
		node<Type> *right_min;
		right_min = MinNode(tmp->right);
		cout<<"right_min="<<right_min->value<<endl;
		//这个子树不是tmp的右节点的话,这个节点肯定是最左的节点,要把这个节点摘出来,把它右子树补到这个节点 
		if(right_min->parent!=tmp){
			right_min = Replace(right_min,right_min->right);
			right_min->right = tmp->right;//再把这个摘出来的节点右边接到tmp的右子树 
			tmp->right->parent = right_min; 
		}
		//然后再拼接摘出来的这个节点左边子树 
		tmp = Replace(tmp,right_min);
		right_min->left = tmp->left;
		tmp->left->parent = right_min;
		delete tmp;
	}
	
} 

int main(){
	tree_class<int> test;
	test.Insert(1);
	test.Insert(4);
	test.Insert(19);
	test.Insert(7);
	test.Insert(86); 

	test.Insert(59);
	
	test.Insert(61);
	test.ShowAll();
	//tree_class<int> test2 = test;
//	test2.ShowAll();
	node<int> *p = test.Search(19);
	if(p==NULL) cout<<"search NULL"<<endl;
	else cout<<"search value="<<p->value<<endl;
	p = test.Max();
	cout<<"max value="<<p->value<<endl;
	p = test.Min();
	cout<<"min value="<<p->value<<endl;
	test.Delete(19);
	test.ShowAll();
	return 0;
} 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值