zoukankan      html  css  js  c++  java
  • 二叉排序树的实现

    /*
     * bst.cpp
     *
     *  Created on: 2013年8月3日
     *      Author: 黄俊东
     *      加油,努力就会有机会。总有一天我会有章泽天那样的女朋友的。。。。。。
     */
    
    #include <iostream>
    
    using namespace std;
    
    typedef char T;
    class bst{
    	struct Node{
    		T data;
    		Node* L;
    		Node* R;
    		Node(const T& d):data(d),L(),R(){
    		}
    		Node(const T& d,Node* l , Node* r):data(d),L(l),R(r){
    		}
    	};
    
    	typedef Node* tree;
    	Node* rp;//指向根节点
    	int n;//节点个数
    
    public:
    	bst():rp(),n(){
    
    	}
    
    	void insert(tree& t,Node* p){
    		if(t == NULL){
    			t = p;
    		}else if(p->data < t->data){
    			insert(t->L,p);
    		}else{
    			insert(t->R,p);
    		}
    	}
    
    	tree& find(tree& t , const T& d){
    		if(t == NULL){
    			return t;
    		}else if(d == t->data){
    			return t;
    		}else if(d<t->data){
    			return find(t->L,d);
    		}else{
    			return find(t->R,d);
    		}
    	}
    
    	void travel(tree t)const{
    		if(t != NULL){
    			travel(t->L);
    			cout<<t->data<<' ';
    			travel(t->R);
    		}
    	}
    
    	void clear(tree t){
    		if(t!= NULL){
    			clear(t->L);
    			clear(t->R);
    			delete t;
    			t=NULL;
    		}
    	}
    
    	int high(tree t){
    		if(t == NULL){
    			return 0;
    		}
    		int lh = high(t->L);
    		int rh = high(t->R);
    		return 1+((lh>rh)?lh:rh);
    
    	}
    	void clear(){
    		clear(rp);
    	}
    
    	~bst(){
    		clear();
    	}
    
    	void insert(const T& d){
    		insert(rp,new Node(d));
    		++n;
    	}
    
    	tree& find(const T& d){
    		return find(rp,d);
    	}
    
    	void travel()const{
    		travel(rp);
    		cout<<endl;
    	}
    
    	bool empty()const{
    		return rp == NULL;
    	}
    
    	int size()const{
    		return n;
    	}
    
    	bool remove(const T& d){
    		tree& t = find(d);
    		if(t == NULL){
    			return false;
    		}
    		Node* p = t;
    		if(t->L!=NULL){
    			insert(t->R,t->L);
    		}
    		t= t->R;
    		delete p;
    		--n;
    		return true;
    	}
    
    	const T& root()const{
    		if(!rp){
    			throw "空";
    		}
    
    		return rp->data;
    	}
    
    	void update(const T& olddata , const T& newdata){
    		if(remove(olddata)){
    			insert(newdata);
    		}
    	}
    };
    
    int main(){
    	bst b;
    	b.insert('k');b.insert('s');b.insert('f');b.insert('t');
    	b.insert('a');b.insert('m');b.insert('x');b.insert('e');
    	b.insert('w');b.insert('b');b.insert('u');b.insert('j');
    	b.travel();
    
    	b.remove('k');b.remove('m');b.remove('j');b.remove('u');
    	b.travel();
    
    	while(!b.empty()){
    		b.remove(b.root());
    	}
    	cout<<"size:"<<b.size()<<endl;
    	b.travel();
    }
    


  • 相关阅读:
    质数检测器
    《视觉SLAM十四讲》学习日志(一)——预备知识
    C++类的介绍
    Python数据类型解析(基础篇)
    括号匹配详解
    哈夫曼树编码
    分治之归并,快速排序
    洛谷p2216 多次单调队列,扫描矩阵中的最大值减去最小值最的固定大小子矩阵
    洛谷p1886滑动窗口最大最小值 双单调队列
    洛谷p1725 露琪诺 单调队列优化的DP
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3237082.html
Copyright © 2011-2022 走看看