BST樹

#pragma once


template<class Type>
class BSTree;

template<class Type>
class BSTNode
{
	friend class BSTree<Type>;
public:
	BSTNode() :data(0), leftChild(NULL), rightChild(NULL) {}
	BSTNode(Type d) :data(d), leftChild(NULL), rightChild(NULL) {}
	~BSTNode() {}
public:;
	void Setdata(Type d)
	{
		data = d;
	}
	Type GetData()const
	{
		return data;
	}


public:
	Type data;
	BSTNode<Type>*leftChild;
	BSTNode<Type>*rightChild;
};

template<class Type>
class BSTree
{
public:
	BSTree() :root(NULL) {}
	BSTree(Type ar[], int n) :root(NULL)
	{
		for (int i = 0; i < n; i++)
			insert(ar[i]);
	}
	~BSTree()
	{
		MakeEmpty(root);
	}
public:
	bool insert(Type&x)
	{
		return insert(root, x);
	}
	void sort()
	{
		sort(root);
	}
	bool remove(const Type&key)
	{
		return remove(root, key);
	}
	Type Max()const
	{
		return Max(root)->data;
	}
	
	Type Min()const
	{
		return Min(root)->data;
	}
	
	BSTNode<Type>*Find(Type key)
	{
		return Find(root, key);
	}
	
	Type Parent(Type t)const
	{
		BSTNode<Type>*p = Parent(root, t);
		if (p != NULL)
			return p->data;
		else
			return -1;
	}
	
	void MakeEmpty(BSTNode<Type>*&t)
	{
		if (t != NULL)
		{
			MakeEmpty(t->leftChild);
			MakeEmpty(t->rightChild);
			delete t;
			t = NULL;
		}
	}
	
public:
    BSTNode<Type>*Min(BSTNode<Type>*root)const
	{
		while (root->leftChild != NULL)
			root = root->leftChild;
		return root;
	}
	BSTNode<Type>*Max(BSTNode<Type>*root)const
	{
		while (root->rightChild != NULL)
			root = root->rightChild;
		return root;
	}
	BSTNode<Type>*Find(BSTNode<Type>*root, Type&key)
	{
		if (root != NULL)
		{
			if (root->data == key)
				return root;
			else if (root->data > key)
				Find(root->rightChild, key);
			else if (root->data < key)
				Find(root->leftChild, key);
		}
		return false;
	}
	BSTNode<Type>*Parent(BSTNode<Type>*t, Type val)const
	{

		if (t == NULL || t->data == val)
			return NULL;
		else if (t->leftChild != NULL&&t->leftChild->data == val)
			return t;
		else if (t->rightChild != NULL&&t->rightChild->data == val)
			return t;
		else if (t->data > val)
			Parent(t->leftChild, val);
		else
			Parent(t->rightChild, val);
	}
public:
	bool remove(BSTNode<Type>*&t, const Type &key)
	{
		if (t != NULL)
		{
			if (key < t->data)//在左子樹中刪除
				remove(t->leftChild, key);
			else if (key > t->data)//在右子樹中刪除
				remove(t->rightChild, key);
			else
			{
				/*if (t->leftChild == NULL&&t->rightChild == NULL)//左右子樹都爲空
				{
					delete t;
					t = NULL;
				}
				else if (t->leftChild != NULL&&t->rightChild == NULL)//左不空,右空
				{
					BSTNode<Type>*tmp = t;
					t = t->leftChild;
					return tmp;
				}
				else if (t->leftChild == NULL&&t->rightChild != NULL)//右空,左不空
				{
					BSTNode<Type>*tmp = t;
					t = t->rightChild;
					return tmp;
				}*/
				//左右子樹都不空
			  if (t->leftChild == NULL&&t->rightChild != NULL)
				{
					BSTNode<Type>*tmp = t->leftChild;
					while (tmp->rightChild != NULL)
						tmp = tmp->rightChild;
					t->data = tmp->data;
					remove(t->leftChild, t->data);
				}
			  //他有一個或零個子女
			  else
			  {
				  BSTNode<Type>*tmp = t;
				  if (t->leftChild == NULL)
					  t = t->rightChild;
				  else
					  t = t->leftChild;
				  delete tmp;

			  }

				return true;
			}
		}
		return false;

	}
	void sort(BSTNode<Type>*t)const
	{
		if (t != NULL)
		{
			sort(t->leftChild);
			cout << t->data << " ";
			sort(t->rightChild);
		}
	}

	bool insert(BSTNode<Type>*&t,Type&x)const
	{
		if (t == NULL)
		{
			t = new BSTNode<Type>(x);
			return true;
		}
		else if (x < t->data)
			insert(t->leftChild, x);
		else if (x > t->data)
			insert(t->rightChild, x);
		else
			return false;
			
	}

private:
	BSTNode<Type>*root;
};

void main()
{
	int ar[] = { 53,78,65,17,87,9,81,45,23,68 };
	int n = sizeof(ar) / sizeof(int);
	BSTree<int> bst(ar, n);
	
	bst.sort();
	cout << endl;

	cout << bst.Parent(78) << endl;
	
	cout<<bst.Max()<<endl;
	cout<<bst.Min()<<endl;

	BSTNode<int>*p = NULL;
	if (p = bst.Find(53))
		cout << p->data << endl;
	else
		cout << "not find" << endl;

	bst.remove(87);
	bst.sort();

	//bst.~BSTree();
	//bst.sort();
	
}



發佈了33 篇原創文章 · 獲贊 5 · 訪問量 9110
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章