平衡二叉樹(AVL)的插入和刪除詳解(下)

平衡二叉樹(AVL)的插入和刪除詳解(上):http://blog.csdn.net/sysu_arui/article/details/7897017

1、測試代碼

爲減小篇幅,只給出了主程序,其他函數模塊請看(上)中的描述。

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	AVL t ;
	initAVL(t);
	bool taller = false;
	bool shorter = false;
	int key;
	string major;
	ElementType e;
	int choice = -1;
	bool flag = true;
	
	while(flag)
	{
		cout<<"--------------------"<<endl;
		cout<<"0. print"<<endl
			<<"1. insert"<<endl 
			<<"2. delete"<<endl
			<<"3. search"<<endl
			<<"4. exit"<<endl
			<<"--------------------"<<endl
			<<"please input your choice: ";
		cin>>choice;
		switch(choice)
		{
			case 0:
				printAVL(t);
				cout<<endl<<endl;
				break;
			case 1:
				inOrderTraverse(t);
				cout<<endl<<"input the elements to be inserted,end by 0:"<<endl;
				while(cin>>key && key)
				{
					cin>>major;
					ElementType e(key,major);
					if(insertAVL(t,e,taller))

					{
						cout<<"insert element "<<e<<" successfully"<<endl;
					}
					else
					{
						cout<<"there already exists an element with key "<< e.key<<endl;
					}
				}
				//while(cin>>e && e.key)
//				{
//					if(insertAVL(t,e,taller))
//					{
//						cout<<"insert element "<<e<<" successfully"<<endl;
//					}
//					else
//					{
//						cout<<"there already exists an element with key "<< e.key<<endl;
//					}
//				}
				cout<<"after insert: "<<endl;
				printAVL(t);
				cout<<endl<<endl;
				break;
				
			case 2:
				inOrderTraverse(t);
				cout<<endl<<"input the keys to be deleted,end by 0:"<<endl;
				while(cin>>key && key)
				{
					if(deleteAVL(t,key,shorter))
					{
						cout<<"delete the element with key "<<key<<" successfully"<<endl;
					}
					else
					{
						cout<<"no such an element with key "<<key<<endl;
					}
				}
				cout<<"after delete: "<<endl;
				printAVL(t);
				cout<<endl<<endl;
				break;
				
			case 3:
				inOrderTraverse(t);
				cout<<endl<<"input the keys to be searched,end by 0:"<<endl;
				while(cin>>key && key)
				{
					if(searchAVL(t,key))
						cout<<key<<" is in the tree"<<endl;
					else
						cout<<key<<" is not in the tree"<<endl;
				}
				cout<<endl<<endl;
				break;
				
			case 4:
				flag = false;
				break;
				
			default:
				cout<<"error! watch and input again!"<<endl<<endl;
				
		}
	}
	destroyAVL(t);
	
    system("PAUSE");
    return EXIT_SUCCESS;
}

注:

(1)前面我們對元素類型輸入和輸出操作符進行了重載,這裏可以直接輸入和輸出。當然也可以採用先獲取數據成員,然後構造對象的方式。

(2)請注意上面代碼的I/O格式,下面的測試用例會給出示例。我們假設沒有關鍵字爲0,即採用0作爲輸入結束。

2、測試用例

(1)  輸入1,開始insert。接着輸入要插入的數據元素,每行一個(學號和專業之間以空格分隔),如果採用的是重載>>後的輸入方式,那麼以 0 0作爲結束,如果採用的是另外的方式,直接輸入0結束,上面的代碼插入刪除查找都是以0作爲輸入結束。

20 dm

10 english

5 physics

30 chinese

40 language

15 japanese

25 biology

23 mathematics

50 chemistry

1 physics

3 geography

0

插入完成後,會給出提示,最後給出前序和中序輸出。可以對比下面的圖看是否正確。

(2)  輸入3,進行search。依次輸入1 2 3 5 7 8 10 13 15 17 20 23 30 31 50 60 0

觀察輸出結果看是否正確

(3)  輸入2,進行delete。依次輸入15 23 25 1 30 50 40 3 0

(4)  輸入1,打印平衡二叉樹。比較看看輸出和自己畫的是否相符。

3、插入刪除圖例

(1)依次插入20、10、5、30、40、15、25、23、50、1、3



(2)在上圖中依次刪除15、23、25、1、30、50、40、3





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