亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? bst.h

?? 二岔搜索樹(shù) 可完成基本操作 遍歷 查找 刪除 判斷兩樹(shù)是否相等
?? H
字號(hào):
#ifndef BST_H
#define BST_H

#include <assert.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int max(int a, int b)
{
	return ((a >= b) ? a : b);		
}

template<class Type> class BST;

// 二叉搜索樹(shù)的節(jié)點(diǎn)類
template<class Type> class BSTNode {
	friend class BST<Type>;
public:
	BSTNode(Type d = 0, BSTNode<Type> *left = 0, BSTNode<Type> *right = 0) : data(d), leftChild(left), rightChild(right) { }
	Type getData() const { return data; }
private:
	BSTNode<Type> *leftChild;  // 指向左子女結(jié)點(diǎn)
	BSTNode<Type> *rightChild; // 指向右子女結(jié)點(diǎn)
	Type data;  // 數(shù)據(jù)
};

//////////////////////////////////////////////////////////////////////////////////////////

// 二叉搜索樹(shù)的類BST
template<class Type> class BST {
public:
	BST() : root(0) { }  // 構(gòu)造函數(shù):創(chuàng)建一個(gè)空樹(shù)
	BST(const BST<Type> &); // 復(fù)制構(gòu)造函數(shù)
	BST& operator=(const BST<Type> &); // 重載運(yùn)算符'='
	~BST(); // 析構(gòu)函數(shù)
	
	bool isEmpty() const { return root == 0; }  // 判斷二叉搜索樹(shù)是否為空	
	bool operator==(const BST<Type> &t) const;  // 判斷當(dāng)前樹(shù)(*this)與樹(shù)t是否相等

	BSTNode<Type> *getRoot() const { return root; }  // 取根root的指針值
	BSTNode<Type> *parent(BSTNode<Type> *current) const;  // 返回雙親節(jié)點(diǎn)的地址
	BSTNode<Type> *leftChild(BSTNode<Type> *current) const; // 返回左子女的節(jié)點(diǎn)地址
	BSTNode<Type> *rightChild(BSTNode<Type> *current) const; // 返回右子女的節(jié)點(diǎn)地址
	bool find(const Type &item) const;  // 搜索元素

	void insert(const Type &item);  // 插入新元素
	
	void remove(const Type &x);  // 刪除含x的節(jié)點(diǎn)

	Type min() const; // 求最小元素
	Type max() const; // 求最大元素

	void inOrder() const; // 中序遍歷二叉搜索樹(shù)

	int size() const;  // 計(jì)算二叉搜索樹(shù)的節(jié)點(diǎn)個(gè)數(shù)
	int depth() const;  // 計(jì)算二叉搜索樹(shù)的高度
private:
	BSTNode<Type> *root;  // 二叉搜索樹(shù)的根指針	
	void destroy(BSTNode<Type> *current);  // 刪除根為current的子樹(shù)	
	// 從節(jié)點(diǎn)start開(kāi)始,搜索current的雙親節(jié)點(diǎn)
	BSTNode<Type> *parent(BSTNode<Type> *start, BSTNode<Type> *current) const;  
	void insert(BSTNode<Type> * &start, const Type &item);  // 插入
	void remove(BSTNode<Type> * &start, const Type &item); // 刪除

	BSTNode<Type>* find(const Type &item, BSTNode<Type> *ptr) const; // 搜索

	BSTNode<Type> *min(BSTNode<Type> *ptr) const;  // 求最小
	BSTNode<Type> *max(BSTNode<Type> *ptr) const;  // 求最大

	void inOrder(BSTNode<Type> *current) const; // 中序遍歷以current為根的二叉搜索樹(shù)
	
	int size(const BSTNode<Type> *current) const;   // 計(jì)算以current為根的二叉搜索樹(shù)的節(jié)點(diǎn)個(gè)數(shù)
	int depth(const BSTNode<Type> *current) const;  // 計(jì)算以current為根的二叉搜索樹(shù)的高度

	// 復(fù)制以current為根的樹(shù)
	BSTNode<Type>* copy(BSTNode<Type> *current);

	bool equal(BSTNode<Type> *a, BSTNode<Type> *b) const;  // 判斷樹(shù)a和b是否相等
};

// 復(fù)制構(gòu)造函數(shù)
template<class Type> 
BST<Type>::BST(const BST<Type> &right)
{
	root = copy(right.root);
}

// 復(fù)制以current為根的樹(shù)
template<class Type> 
BSTNode<Type>* BST<Type>::copy(BSTNode<Type> *current)
{
	if (!current)  // 根為空,返回空指針
		return 0;

	BSTNode<Type> *temp = new BSTNode<Type>;
	temp->data = current->data;
	temp->leftChild = copy(current->leftChild);
	temp->rightChild = copy(current->rightChild);

	return temp;
}

// 重載運(yùn)算符'='
template<class Type> 
BST<Type>& BST<Type>::operator=(const BST<Type> &right) 
{
	 // 避免自我賦值和對(duì)已經(jīng)相等的樹(shù)進(jìn)行賦值
	if ((this == &right) || equal(root, right.root)) 
		return *this;
	
	destroy(root);  // 刪除原來(lái)的樹(shù)
	root = copy(right.root); // 賦值

	return *this;
}

// 析構(gòu)函數(shù)
template<class Type>
BST<Type>::~BST() 
{
	if (root)
		destroy(root); 

	root = 0;
}  

// 私有函數(shù):若指針current不為空,則刪除根為current的子樹(shù)
template<class Type> void BST<Type>::destroy(BSTNode<Type> *current)
{
	if (current) {
		destroy(current->leftChild);  // 刪除current的左子樹(shù)
		destroy(current->rightChild);  // 刪除current的右子樹(shù)
		delete current;  // 刪除current			
	}	
}

// 返回current的雙親節(jié)點(diǎn)的地址
template<class Type> 
BSTNode<Type>* BST<Type>::parent(BSTNode<Type> *current) const 
{
	if (root == 0 || root == current)
		return 0;
	else
		return parent(root, current);
}

// 所有函數(shù):從節(jié)點(diǎn)start開(kāi)始,搜索節(jié)點(diǎn)current的雙親節(jié)點(diǎn)
// 若找到,則函數(shù)返回雙親節(jié)點(diǎn)的地址,否則返回0
template<class Type>
BSTNode<Type>* BST<Type>::parent(BSTNode<Type> *start, BSTNode<Type> *current) const
{
	if (start == 0)
		return 0;
	if (start->leftChild == current || start->rightChild == current) // 找到
		return start;  

	BSTNode<Type> *temp;

	if (temp = parent(start->leftChild, current))  // 在左子樹(shù)中搜索
		return temp;
	else
		return parent(start->rightChild, current);  // 在右子樹(shù)中搜索
}

// 返回節(jié)點(diǎn)current的左子女節(jié)點(diǎn)地址
template<class Type>
BSTNode<Type>* BST<Type>::leftChild(BSTNode<Type> *current) const
{
	if (!root)
		return 0;
	return current->leftChild; 
} 

// 返回節(jié)點(diǎn)current的右子女節(jié)點(diǎn)地址
template<class Type>
BSTNode<Type>* BST<Type>::rightChild(BSTNode<Type> *current) const
{
	if (!root)
		return 0;
	return current->rightChild;
} 


// 在二叉搜索樹(shù)中插入值為item的新節(jié)點(diǎn)
template<class Type>
void BST<Type>::insert(const Type &item)
{
	insert(root, item);
}

// 私有函數(shù):在以根start的二叉搜索樹(shù)中插入所含值為item的節(jié)點(diǎn)
// 若樹(shù)中已經(jīng)有含x的節(jié)點(diǎn),則不插入
template<class Type> 
void BST<Type>::insert(BSTNode<Type> * &start, const Type &item)
{
	if (!start) {  // 新節(jié)點(diǎn)作為葉節(jié)點(diǎn)插入
		start = new BSTNode<Type>(item);  // 創(chuàng)建新節(jié)點(diǎn)
		assert(start);
	}
	else if ( item < start->data) // 小于根的關(guān)鍵碼,向左子樹(shù)插入
		insert(start->leftChild, item);   
	else if (item > start->data)
		insert(start->rightChild, item);  // 大于根的關(guān)鍵碼,向右子樹(shù)插入
}

// 刪除含x的節(jié)點(diǎn)
template<class Type> 
void BST<Type>::remove(const Type &x)
{
	remove(root, x); 
} 

// 私有函數(shù):在以ptr為根的二叉搜索樹(shù)中。若刪除成功,新根通過(guò)ptr返回
template<class Type> 
void BST<Type>::remove(BSTNode<Type> *&ptr, const Type &x)
{
	BSTNode<Type> *temp;
	if (ptr) {
		if (x < ptr->data)
			remove(ptr->leftChild, x);  // 在左子樹(shù)中刪除
		else if (x > ptr->data)
			remove(ptr->rightChild, x);  // 在右子樹(shù)中刪除
		else if(ptr->leftChild && ptr->rightChild) {  // ptr指示關(guān)鍵碼為x的節(jié)點(diǎn),它有兩個(gè)子女
			temp = min(ptr->rightChild); // 在ptr的右子樹(shù)中搜尋最小節(jié)點(diǎn)
			ptr->data = temp->data;  // 用該節(jié)點(diǎn)數(shù)據(jù)代替根節(jié)點(diǎn)數(shù)據(jù)
			remove(ptr->rightChild, ptr->data);  // 在右子樹(shù)中刪除該節(jié)點(diǎn)
		}
		else { // ptr指示關(guān)鍵碼為x的節(jié)點(diǎn),它只有一個(gè)或零個(gè)子女
			temp = ptr;
			if (!ptr->leftChild)
				ptr = ptr->rightChild;  // 只有右子女
			else if (!ptr->rightChild)
				ptr = ptr->leftChild;  // 只有左子女
			delete temp;
		}
	}	
} 

// 求最小元素
template<class Type> Type BST<Type>::min() const {
	return min(root)->getData(); 
}

// 尋找以ptr為根的二叉搜索樹(shù)的最小值的地址
template<class Type> 
BSTNode<Type>* BST<Type>::min(BSTNode<Type> *ptr) const 
{
	if (!ptr)
		return 0;

	BSTNode<Type> *temp = ptr;
	
	while (temp->leftChild) 
		temp = temp->leftChild;

	return temp;	
}

// 求最大元素
template<class Type> Type BST<Type>::max() const {
	return max(root)->getData(); 
}

// 尋找以ptr為根的二叉搜索樹(shù)的最大值的地址
template<class Type> 
BSTNode<Type>* BST<Type>::max(BSTNode<Type> *ptr) const 
{
	if (!ptr)
		return 0;

	BSTNode<Type> *temp = ptr;
	
	while (temp->rightChild) 
		temp = temp->rightChild;

	return temp;	
}

// 搜索元素item
template<class Type>
bool BST<Type>::find(const Type &item) const
{
	return (find(item, root) != 0);
}

// 私有函數(shù):在以ptr為根的二叉搜索數(shù)中搜索含item的節(jié)點(diǎn)
template<class Type>
BSTNode<Type>* BST<Type>::find(const Type &item, BSTNode<Type> *ptr) const
{

	if (ptr) { // 樹(shù)不空

		
		// 遞歸算法
		if (item < ptr->data)
		return find(item, ptr->leftChild); // 到左子樹(shù)中搜索
		else if (item > ptr->data)
		return find(item, ptr->rightChild);  // 到右子樹(shù)中搜索
		else
		return ptr; // 搜索成功	
		

		/*

		// 迭代算法	

		BSTNode<Type> *temp = ptr;  // 從根開(kāi)始搜索

		while (temp) {
			if (temp->data == item)  // 搜索成功
				return temp;  
			else if (temp->data < item)
				temp = temp->rightChild; // 否則,繼續(xù)搜索右子樹(shù)
			else
				temp = temp->leftChild;  // 否則,繼續(xù)搜索左子樹(shù)
		}
		*/
	}
	
	return 0;	
}

// 中序遍歷二叉搜索樹(shù)
template<class Type>
void BST<Type>::inOrder() const
{
	inOrder(root);
}

// 私有函數(shù):中序遍歷以current為根的二叉搜索樹(shù)
template<class Type>
void BST<Type>::inOrder(BSTNode<Type> *current) const
{
	if(current) {
		inOrder(current->leftChild);  // 中序遍歷左子樹(shù)
		cout << current->data << ' '; // 訪問(wèn)根節(jié)點(diǎn),用輸出語(yǔ)句暫代
		inOrder(current->rightChild); // 中序遍歷右子樹(shù)
	}
}

// 計(jì)算二叉搜索樹(shù)的節(jié)點(diǎn)個(gè)數(shù)
template<class Type>
int BST<Type>::size() const
{
	return size(root);
}

// 私有函數(shù):計(jì)算以current為根的二叉搜索樹(shù)的節(jié)點(diǎn)個(gè)數(shù)
template<class Type>
int BST<Type>::size(const BSTNode<Type> *current) const
{
	if (!current)  // 遞歸結(jié)束條件
		return 0;
	else
		return 1 + size(current->leftChild) + size(current->rightChild);
}

// 計(jì)算二叉搜索樹(shù)的高度
template<class Type>
int BST<Type>::depth() const
{
	return depth(root);
}

// 私有函數(shù):計(jì)算以current為根的二叉搜索樹(shù)的節(jié)點(diǎn)個(gè)數(shù)
template<class Type>
int BST<Type>::depth(const BSTNode<Type> *current) const
{
	if (!current)  // 遞歸結(jié)束條件
		return -1;
	else
		return 1 + ::max(depth(current->leftChild), depth(current->rightChild));
}

// 判斷當(dāng)前樹(shù)(*this)與樹(shù)t是否相等
template<class Type>
bool BST<Type>::operator==(const BST<Type> &t) const
{
	return equal(root, t.root);
}

// 私有函數(shù):判斷樹(shù)a和b是否相等
template<class Type>
bool BST<Type>::equal(BSTNode<Type> *a, BSTNode<Type> *b) const
{
	if (!a && !b)
		return true;
	if (a && b && (a->data == b->data) && 
		equal(a->leftChild, b->leftChild) && 
		equal(a->rightChild, b->rightChild))
		return true;
	else
		return false;
}

#endif // BST_H

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产精品久久午夜| 亚洲在线视频一区| 色美美综合视频| 久久99精品久久久久婷婷| 中文字幕一区二区三区色视频 | 亚洲成人黄色小说| 久久蜜桃av一区二区天堂| 欧美午夜一区二区三区免费大片| 国产一区二区主播在线| 亚洲成人免费av| 中文字幕在线不卡| 亚洲精品一区在线观看| 欧美久久一区二区| 99免费精品视频| 国产精品911| 麻豆91在线播放免费| 亚洲激情第一区| 18成人在线观看| 久久精品这里都是精品| 欧美一级精品大片| 欧美性极品少妇| a级精品国产片在线观看| 国产麻豆精品久久一二三| 日韩国产精品久久| 午夜视黄欧洲亚洲| 亚洲精品国产高清久久伦理二区| 中文字幕欧美三区| 久久久久国产精品麻豆| 欧美变态口味重另类| 欧美一区二区三区的| 欧美三级视频在线观看| 一本大道久久a久久精品综合| 成人综合在线视频| 成人黄色电影在线 | 欧美在线观看一区二区| bt欧美亚洲午夜电影天堂| 国产福利一区在线| 国产一区二区不卡| 国产精品白丝jk白祙喷水网站| 久久精品国内一区二区三区| 欧美aaa在线| 青青草一区二区三区| 日本一不卡视频| 日本三级亚洲精品| 日韩电影在线观看一区| 日韩成人一区二区三区在线观看| 午夜一区二区三区在线观看| 亚洲一区二区不卡免费| 亚洲高清免费观看| 亚洲6080在线| 日韩制服丝袜先锋影音| 日韩电影在线一区二区| 麻豆精品国产91久久久久久| 激情伊人五月天久久综合| 国内久久婷婷综合| 国产不卡在线播放| 97久久超碰国产精品电影| 91蝌蚪国产九色| 欧美亚洲国产一区二区三区| 欧美日韩不卡一区二区| 日韩欧美中文字幕精品| 久久久亚洲精品石原莉奈| 国产三级精品三级在线专区| 国产精品久久久久天堂| 亚洲欧美日韩在线播放| 亚洲大尺度视频在线观看| 日本成人在线一区| 国产一区二区剧情av在线| 不卡一区中文字幕| 欧美日韩中文字幕精品| 精品国产乱子伦一区| 国产清纯白嫩初高生在线观看91 | 亚洲二区视频在线| 青青草国产精品亚洲专区无| 国产福利一区二区三区在线视频| 99麻豆久久久国产精品免费| 欧美精品久久久久久久多人混战| 精品国产乱码久久久久久图片| 中文字幕第一区二区| 亚洲国产一区二区视频| 精品一区在线看| 91免费看片在线观看| 91精品国产福利在线观看| 国产日韩欧美麻豆| 亚洲国产精品天堂| 激情五月激情综合网| 日本电影亚洲天堂一区| 91精品欧美久久久久久动漫| 国产精品久久久久影院色老大| 亚洲一级电影视频| 精品视频一区三区九区| 欧美精品一区二区三区视频| 亚洲情趣在线观看| 麻豆免费看一区二区三区| a级高清视频欧美日韩| 日韩一本二本av| 亚洲欧美日韩系列| 国产一区二区三区蝌蚪| 在线欧美日韩精品| 国产日韩精品视频一区| 日韩电影在线观看网站| 97se亚洲国产综合自在线观| 欧美va天堂va视频va在线| 亚洲精品伦理在线| 国产69精品久久99不卡| 91.麻豆视频| 亚洲精品久久久蜜桃| 国产91丝袜在线播放| 欧美二区三区91| 亚洲免费色视频| 国产盗摄一区二区三区| 欧美一级高清大全免费观看| 亚洲一区免费视频| 91在线视频网址| 日本一区二区三级电影在线观看 | 日本午夜一本久久久综合| 91女厕偷拍女厕偷拍高清| 日韩精品专区在线影院重磅| 亚洲成人自拍一区| 91浏览器在线视频| 国产精品成人午夜| 床上的激情91.| 欧美精彩视频一区二区三区| 久久99精品国产麻豆不卡| 欧美裸体bbwbbwbbw| 亚洲精品乱码久久久久久久久 | 日本欧美大码aⅴ在线播放| 欧美在线一二三四区| 1区2区3区精品视频| 国产成人自拍在线| 久久综合久色欧美综合狠狠| 毛片av一区二区| 欧美成人午夜电影| 蜜桃91丨九色丨蝌蚪91桃色| 这里只有精品99re| 午夜精品久久久久久| 欧美日韩国产系列| 香蕉久久一区二区不卡无毒影院| 欧洲激情一区二区| 亚洲在线免费播放| 欧美在线视频日韩| 午夜亚洲福利老司机| 欧美精品电影在线播放| 日韩精品高清不卡| 欧美一级片免费看| 美女视频网站久久| 337p粉嫩大胆噜噜噜噜噜91av| 国内精品久久久久影院色| 久久婷婷色综合| 粉嫩欧美一区二区三区高清影视| 中文字幕av免费专区久久| 成人黄色一级视频| 亚洲靠逼com| 欧美日韩国产一级片| 麻豆精品国产传媒mv男同 | 91麻豆精品91久久久久同性| 香蕉av福利精品导航| 欧美一级二级三级蜜桃| 久久99九九99精品| 久久久777精品电影网影网 | 国产精品久久久久久久久免费丝袜 | 精品亚洲成a人| 国产欧美综合在线观看第十页| av资源站一区| 一区二区三区四区在线| 884aa四虎影成人精品一区| 极品尤物av久久免费看| 中文一区一区三区高中清不卡| 91捆绑美女网站| 免费在线观看日韩欧美| 日韩在线一区二区三区| 精品三级在线观看| 成人黄页在线观看| 日韩黄色小视频| 欧美国产精品v| 欧美日韩国产首页在线观看| 国产剧情av麻豆香蕉精品| 亚洲精品高清视频在线观看| 欧美一区二区二区| 国产成人av福利| 亚洲国产综合人成综合网站| 久久人人97超碰com| 91麻豆福利精品推荐| 久久精品国产99国产| 亚洲色欲色欲www| 91精品国产综合久久国产大片| 国产成人在线视频网站| 亚洲无人区一区| 国产日韩欧美电影| 91.com视频| 99久久精品费精品国产一区二区 | 国产成人在线视频网站| 亚洲一区电影777| 国产三级精品在线| 欧美高清激情brazzers| 99re热视频精品| 国产一区二区三区在线观看精品| 亚洲一区二区黄色| 国产精品国模大尺度视频| 精品国内片67194|