二叉树练习

// erchashu_xiti.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<queue>
#include <stack> 
using namespace std;

//////////定义树节点结构
typedef struct Tree {
	int key;
	Tree* p;
	Tree* left;
	Tree* right;
	int Heigh;
}Tree;
typedef Tree* TreeNode;

static int Height(TreeNode p)  //返回树高,尤其是空节点高度的处理
{
	if (p == NULL)
		return -1;
	else
		return p->Heigh;
}

static TreeNode zuoxuan(TreeNode &K2)       //左旋转,高子树在左边
{
	TreeNode K1;
	K1 = K2->left;
	K2->left = K1->right;
	K1->right = K2;
	K2->Heigh= max(Height(K2->left), Height(K2->right)) + 1;
	K1->Heigh = max(Height(K1->left), Height(K1->right)) + 1;
	return K1;
}

static TreeNode youxuan(TreeNode &K1)    //右旋转,高子树在右边
{
	TreeNode K2;
	K2 = K1->right;
	K1->right = K2->left;
	K2->left = K1;
	K2->Heigh = max(Height(K2->left), Height(K2->right)) + 1;
	K1->Heigh = max(Height(K1->left), Height(K1->right)) + 1;
	return K2;
}

static TreeNode zuoyouxuan(TreeNode &K3)   //左右旋转,高子树在左边,孙子在儿子右边
{
	K3->left = youxuan(K3->left);
	return zuoxuan(K3);
}

static TreeNode youzuoxuan(TreeNode &K3)  //右左旋转,高子树在右边,孙子在儿子左边
{
	K3->right = zuoxuan(K3->right);
	return youxuan(K3);
}


TreeNode Insert(TreeNode& T, int x, TreeNode p)  //插入的递归版本
{
	if (T == NULL) {
		T = (TreeNode)malloc(sizeof(Tree));
		T->key = x;
		T->left = NULL;
		T->right = NULL;
		T->p = p;
		T->Heigh = 0;
	}
	else if(x!=T->key) {
		p = T;
		if (x < T->key)
		{
			T->left=Insert(T->left, x, p);
			if (Height(T->left) - Height(T->right) == 2)
				if (x < T->left->key)
					T = zuoxuan(T);
				else
					T = zuoyouxuan(T);

		}
		else if (x > T->key)
		{
			T->right=Insert(T->right, x, T);
			if (Height(T->right) - Height(T->left) == 2)
				if (x > T->right->key)
					T = youxuan(T);
				else
					T = youzuoxuan(T);
		}
		T->Heigh = max(Height(T->left), Height(T->right)) + 1;
	}
	return T;
}


bool Insert2(TreeNode& T, int x)             //插入的非递归版本
{
	stack<TreeNode> node;
	bool flag = 0;
	TreeNode Tmp = T;
	while (!flag) {
		if (Tmp == NULL) {
			Tmp = (TreeNode)malloc(sizeof(Tree));
			Tmp->key = x;
			Tmp->left = NULL;
			Tmp->right = NULL;
			Tmp->Heigh = 0;
			if (!node.empty()) {
				if (x > node.top()->key)
					node.top()->right = Tmp;
				else
					node.top()->left = Tmp;
			}
			while (!node.empty()) {
				Tmp = node.top();
				node.pop();
				Tmp->Heigh = max(Height(Tmp->left), Height(Tmp->right)) + 1;
				if (Height(Tmp->left) - Height(Tmp->right) == 2) {
					if (x < Tmp->left->key)
						Tmp = zuoxuan(Tmp);
					else
						Tmp = zuoyouxuan(Tmp);
					if (!node.empty()) {
						if (Tmp->key > node.top()->key)
							node.top()->right = Tmp;
						else
							node.top()->left = Tmp;
					}
					Tmp->Heigh = max(Height(Tmp->left), Height(Tmp->right)) + 1;
					
				}
				else if (Height(Tmp->right) - Height(Tmp->left) == 2) {
					if (x > Tmp->right->key)
						Tmp = youxuan(Tmp);
					else
						Tmp = youzuoxuan(Tmp);
					
					if (!node.empty()) {
						if (Tmp->key > node.top()->key)
							node.top()->right = Tmp;
						else
							node.top()->left = Tmp;
					}
					Tmp->Heigh = max(Height(Tmp->left), Height(Tmp->right)) + 1;
				}
			}
			flag = 1;
		}
		else
		{
			while (Tmp!=NULL) {
				node.push(Tmp);
				if (x > Tmp->key)   Tmp = Tmp->right;
				else Tmp = Tmp->left;
			}
		}
	}
	T = Tmp;
	return true;
}


TreeNode FindMin(TreeNode T)   //&函数别名别乱用,开始这里用了函数别名导致最后结果丢失了右子树
{
	TreeNode tmp=T;
	while(tmp->left)
		tmp = tmp->left;
	return tmp;
}

TreeNode Delete(TreeNode& T, int x)   //删除操作,主要考察删除后左右子树高度的变化
{
	TreeNode Tmp;
	if (T == NULL)
		cout << "empty!" << endl;
	else if (T->key > x)
	{
		T->left = Delete(T->left, x);
		if (Height(T->right) - Height(T->left) == 2)
			if (x > T->right->key)
				T = youxuan(T);
			else
				T = youzuoxuan(T);
	}
	else if (T->key < x)
	{
		T->right = Delete(T->right, x);
		if (Height(T->left) - Height(T->right) == 2)
			if (x < T->left->key)
				T = zuoxuan(T);
			else
				T = zuoyouxuan(T);
	}
	else
	{
		if (T->left&&T->right) {
			Tmp = FindMin(T->right);
			T->key = Tmp->key;
			T->right = Delete(T->right, T->key);
		}
		else
		{
			Tmp = T;
			if (T->left == NULL)
				T = T->right;
			else if (T->right == NULL)
				T = T->left;
			free(Tmp);
		}
		
	}
	return T;
}

void Init(TreeNode& T, int key[],int n)     //利用数组初始化二叉树
{
	for (int i = 1; i <= n; i++)
		//Insert(T, key[i], NULL);
	    Insert2(T, key[i]);
}



void PrintTree(TreeNode T)  //层序遍历
{
	cout << T->key << endl;
	queue<TreeNode> q;
	q.push(T->left);
	q.push(T->right);
	while (!q.empty())
	{
		if(q.front()==NULL) q.pop();
		else
		{
			cout << q.front()->key << endl;
			q.push(q.front()->left);
			q.push(q.front()->right);
			q.pop();
		}
		
	}
}

int main()
{
	TreeNode T=NULL;
	int key[10] = { 0,2,1,4,5,9,3,6,7 };
	Init(T, key,8);
	//cout << FindMin(T)->key<<endl;
	PrintTree(T);
	//Insert2(T, 10);
	//Delete(T, 5);
	//PrintTree(T);
	//cout << T->right->key;
	while (1);
    return 0;
}

  

posted @ 2017-03-21 23:38  lineaar  阅读(193)  评论(0编辑  收藏  举报