23.動靜態綁定,二叉搜索樹

  1. 靜態綁定又稱爲前期綁定(早綁定),在程序編譯期間確定了程序的行爲,也稱爲靜態多態,比如:函數重載
  2. 動態綁定又稱後期綁定(晚綁定),是在程序運行期間,根據具體拿到的類型確定程序的具體行爲,調用具體的函數,也稱爲動態多態
//                                                          
//動態綁定
//抽象類 --> 定義接口
class A
{
public:
	//純虛函數
	virtual void Eat() = 0
	{
		cout<<"A::Eat()"<<endl;
	}
	virtual void Foot() = 0;
	virtual void Sleep() = 0;
};

class Person : public A
{
public:
	virtual void Eat()
	{
		cout<<"Person::Eat()"<<endl;
	}
	virtual void Foot()
	{
		cout<<"Person::Foot()"<<endl;
	}
	virtual void Sleep()
	{
		cout<<"Person::Sleep()"<<endl;
	}
};

class Dog : public A
{
public:
	virtual void Eat()
	{
		cout<<"Dog::Eat()"<<endl;
	}
	virtual void Foot()
	{
		cout<<"Dog::Foot()"<<endl;
	}
	virtual void Sleep()
	{
		cout<<"Dog::Sleep()"<<endl;
	}

};

class Bird : public A
{
public:
	virtual void Eat()
	{
		cout<<"Bird::Eat()"<<endl;
	}
	virtual void Foot()
	{
		cout<<"Bird::Foot()"<<endl;
	}
	virtual void Sleep()
	{
		cout<<"Bird::Sleep()"<<endl;
	}
public:
	virtual void Fly()
	{
		cout<<"Bird::Fly()"<<endl;
	}
};

class TN : public Bird
{
public:
	virtual void Eat()
	{
		cout<<"TN::Eat()"<<endl;
	}
	virtual void Foot()
	{
		cout<<"TN::Foot()"<<endl;
	}
	virtual void Sleep()
	{
		cout<<"TN::Sleep()"<<endl;
	}
private:
	virtual void Fly();
};

//動態綁定
void Active(A *pa)
{
	pa->Eat();
	pa->Foot();
	pa->Sleep();
}


void main()
{
	Person p;
	Dog dog;
	Active(&dog);

	Bird b;
	//b.Fly();
}

抽象類不能初始化對象,抽象類的純虛函數被派生類繼承後得全部重寫,抽象類有作爲接口類的作用,抽象類的純虛函數也可以實現但是沒有必要因爲毫無意義

//二叉搜索樹
#include <iostream>
#include <assert.h>
using namespace std;

template<typename Type>
class BSTree;

template<typename Type>
class BSTNode
{
	friend class BSTree<Type>;
public:
	BSTNode(Type d = Type(), BSTNode<Type>* left = nullptr, BSTNode<Type>* right = nullptr)
		: data(d), LeftChild(left), RightChild(right)
	{

	}
private:
	Type data;
	BSTNode<Type>* LeftChild;
	BSTNode<Type>* RightChild;
};

#if 1
template<typename Type>
class BSTree
{
public:
	BSTree() : root(nullptr)
	{}
	BSTree(Type ar[], int n) : root(nullptr)
	{
		for (int i = 0; i < n; ++i)
		{
			Insert(ar[i]);
		}
	}
	~BSTree()
	{
		Clear();
	}
public:
	bool Insert(const Type &key);
	bool Remove(const Type &key);
	void Order()const;
	BSTNode<Type>* Find(const Type &key);
	Type Max()const;
	Type Min()const;
	void Clear();  //歸還在堆上開闢的搜索二叉樹
protected:
	bool Insert(BSTNode<Type>* &root, const Type& key)
	{
		if (root == nullptr)
		{
			root = new BSTNode<Type>(key);  //
			return true;
		}
		else if (key < root->data)
			return Insert(root->LeftChild, key);
		else if (key > root->data)
			return Insert(root->RightChild, key);
		else
			return false;
	}
	bool Remove(BSTNode<Type>*& root, const Type& key)
	{
		if (root == nullptr)
			return nullptr;
		else if(root->LeftChild == nullptr && root->RightChild == nullptr)
		{
			delete root;
			root = nullptr;
		}
		else if (root->LeftChild != nullptr && root->RightChild == nullptr
			|| root->LeftChild == nullptr && root->RightChild != nullptr )
		{
			BSTNode<Type>* p = root;
			root = (root->LeftChild != nullptr) ? root->LeftChild : root->RightChild;
			delete p;
		}
		else
		{
			BSTNode<Type>* p = root->LeftChild;
			//找到右子樹中最大關鍵值的結點
			while (p->RightChild != nullptr)
				p = p->RightChild;
			//將根結點的值替換成右子樹最大關鍵值結點的值
			root->data = p->data;
			//刪除右子樹中最大關鍵值的結點
			Remove(root->LeftChild, p->data);
		}
	}
	void Order(BSTNode<Type>* root)const
	{
		if (root != nullptr)
		{
			Order(root->LeftChild);
			cout << root->data << " ";
			Order(root->RightChild);
		}
	}
	BSTNode<Type>* Find(BSTNode<Type>* root, const Type& key)
	{
		if (root == nullptr)
			return nullptr;
		else if (key == root->data)
			return root;
		else if (key < root->data)
			return Find(root->LeftChild, key);
		else
			return Find(root->RightChild, key);
	}
	Type Max(BSTNode<Type>* root)const
	{
		assert(root != nullptr);
		while (root->RightChild != nullptr)
			root = root->RightChild;
		return root->data;
	}
	Type Min(BSTNode<Type>* root)const
	{
		assert(root != nullptr);
		while (root->LeftChild != nullptr)
			root = root->LeftChild;
		return root->data;
	}
	void Clear(BSTNode<Type>* root)
	{
		if (root != nullptr)
		{
			Clear(root->LeftChild);
			Clear(root->RightChild);
			delete root;
			root = nullptr;
		}
	}
private:
	BSTNode<Type>* root;
};

template<typename Type>
bool BSTree<Type>::Insert(const Type& key)
{
	return Insert(root, key);
}
template<typename Type>
bool BSTree<Type>::Remove(const Type& key)
{
	return Remove(root, key);
}
template<typename Type>
void BSTree<Type>::Order()const
{
	Order(root);
}
template<typename Type>
BSTNode<Type>* BSTree<Type>::Find(const Type& key)
{
	return Find(root, key);
}
template<typename Type>
Type BSTree<Type>::Max()const
{
	return Max(root);
}
template<typename Type>
Type BSTree<Type>::Min()const
{
	return Min(root);
}
template<typename Type>
void BSTree<Type>::Clear()
{
	Clear(root);
}


int main()
{
	BSTree<int> bst;
	int ar[] = { 5,3,4,1,7,8,2,6,0,9 };
	int n = sizeof(ar) / sizeof(int);
	
	//插入方法
	for (int i = 0; i < n; ++i)
	{
		bst.Insert(ar[i]);
	}

	//重載了構造方法之後可以在構造的同時插入
	BSTree<int> bst1(ar, n);


	//中序遍歷
	bst.Order();

	//查找
	BSTNode<int>* res = bst.Find(3);
	if (res != nullptr)
		printf("找到了3,地址爲%p\n", res);
	else
		printf("沒有找到3\n");

	cout << "最大值" << bst.Max() << endl;
	cout << "最小值" << bst.Min() << endl;
	return 0;

}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章