1. 程式人生 > >PAT-ADVANCED1066——Root of AVL Tree

PAT-ADVANCED1066——Root of AVL Tree

題目描述:

題目翻譯:

AVL樹是一棵自平衡的二分搜尋樹。在AVL樹中,左右子樹的高度差不會超過1;任何時刻如果左右子樹的高度差超過了1,自平衡操作會使得其維持左右子樹高度差不超過1這個性質。下圖展示了旋轉規則。

現在給你一串插入序列,你需要輸出由該插入順序構成的AVL樹的根結點。

輸入格式:

每個輸入檔案包含一個測試用例。在每個測試用例中,第一行有一個正整數N(<= 20)。第二行有N個不同的整數。一行中的所有數字由一個空格分隔。

輸出格式:

對每個測試用例,輸出該AVL樹的根節點。

輸入樣例1:

5
88 70 61 96 120

輸出樣例1:

70

輸入樣例2:

7
88 70 61 96 120 90 65

輸出樣例2:

88

知識點:構建AVL樹

思路一:按插入順序構建AVL樹(靜態陣列實現)

時間複雜度和空間複雜度均是O(N)。

C++程式碼:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct node {
	int data;
	int height;
	int lchild;
	int rchild;
};

int N;
vector<int> input;
node Node[20];
int index = 0;

int newNode(int num);
int getHeight(int root);
int getBalanceFactor(int root);
void updateHeight(int root);
void leftTurn(int &root);
void rightTurn(int &root);
void insert(int &root, int num);
int create();

int main(){
	cin >> N;
	int num;
	for(int i = 0; i < N; i++){
		cin >> num;
		input.push_back(num);
	}
	int root = create();
	cout << Node[root].data << endl;
	return 0;
}

int newNode(int num){
	int root = index++;
	Node[root].data = num;
	Node[root].height = 1;
	Node[root].lchild = Node[root].rchild = -1;
	return root;
}

int getHeight(int root){
	if(root == -1){
		return 0;
	}
	return Node[root].height;
}

int getBalanceFactor(int root){
	return getHeight(Node[root].lchild) - getHeight(Node[root].rchild);
}

void updateHeight(int root) {
	Node[root].height = max(getHeight(Node[root].lchild), getHeight(Node[root].rchild)) + 1;
}

void leftTurn(int &root) {
	int temp = Node[root].rchild;
	Node[root].rchild = Node[temp].lchild;
	Node[temp].lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void rightTurn(int &root) {
	int temp = Node[root].lchild;
	Node[root].lchild = Node[temp].rchild;
	Node[temp].rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void insert(int &root, int num) {
	if(root == -1){
		root = newNode(num);
		return;
	}
	if(num < Node[root].data){
		insert(Node[root].lchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(Node[root].lchild) == 1){
				rightTurn(root);
			}else if(getBalanceFactor(Node[root].lchild) == -1){
				leftTurn(Node[root].lchild);
				rightTurn(root);
			}
		}
	}else{
		insert(Node[root].rchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(Node[root].rchild) == -1){
				leftTurn(root);
			}else if(getBalanceFactor(Node[root].rchild) == 1){
				rightTurn(Node[root].rchild);
				leftTurn(root);
			}
		}
	}
}

int create(){
	int root = -1;
	for(int i = 0; i < N; i++){
		insert(root, input[i]);
	}
	return root;
}

C++解題報告:

思路二:按插入順序構建AVL樹(指標實現)

時間複雜度和空間複雜度均是O(N)。

C++程式碼:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct node {
	int data;
	int height;
	node* lchild;
	node* rchild;
};

int N;
vector<int> input;

node* newNode(int num);
int getHeight(node* root);
int getBalanceFactor(node* root);
void updateHeight(node* root);
void leftTurn(node* &root);
void rightTurn(node* &root);
void insert(node* &root, int num);
node* create();

int main(){
	cin >> N;
	int num;
	for(int i = 0; i < N; i++){
		cin >> num;
		input.push_back(num);
	}
	node* root = create();
	cout << root->data << endl;
	return 0;
}

node* newNode(int num){
	node* root = new node;
	root->data = num;
	root->height = 1;
	root->lchild = root->rchild = NULL;
	return root;
}

int getHeight(node* root){
	if(root == NULL){
		return 0;
	}
	return root->height;	
}

int getBalanceFactor(node* root){
	return getHeight(root->lchild) - getHeight(root->rchild);
}

void updateHeight(node* root){
	root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}

void leftTurn(node* &root){
	node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void rightTurn(node* &root){
	node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void insert(node* &root, int num){
	if(root == NULL){
		root = newNode(num);
		return;
	}
	if(num < root->data){
		insert(root->lchild, num);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){
				rightTurn(root);
			}else if(getBalanceFactor(root->lchild) == -1){
				leftTurn(root->lchild);
				rightTurn(root);
			}
		} 
	}else{
		insert(root->rchild, num);
		updateHeight(root); 
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->rchild) == -1){
				leftTurn(root);
			}else if(getBalanceFactor(root->rchild) == 1){
				rightTurn(root->rchild);
				leftTurn(root);
			}
		}
	}
}

node* create(){
	node* root = NULL;
	for(int i = 0; i < N; i++){
		insert(root, input[i]);
	}
	return root;
}

C++解題報告: