二叉树的翻转代码


代码如下:

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
	int nValue;
	struct node *pLeft;
	struct node *pRight;
	struct node *pFather;
}BinaryTree;

BinaryTree *CreateBinaryTree()
{
	BinaryTree *pRoot = NULL;

	//根
	pRoot = (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->nValue = 1;
	pRoot->pFather = NULL;

	//根的左
	pRoot->pLeft =  (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->pLeft->nValue = 2;
	pRoot->pLeft->pFather = pRoot;

	//左的左
	pRoot->pLeft->pLeft =  (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->pLeft->pLeft->nValue = 4;
	pRoot->pLeft->pLeft->pFather = pRoot->pLeft;
	pRoot->pLeft->pLeft->pLeft = NULL;
	pRoot->pLeft->pLeft->pRight = NULL;
	

	//左的右
	pRoot->pLeft->pRight =  (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->pLeft->pRight->nValue = 5;
	pRoot->pLeft->pRight->pFather = pRoot->pLeft;
	pRoot->pLeft->pRight->pLeft = NULL;
	pRoot->pLeft->pRight->pRight = NULL;

	//根的右
	pRoot->pRight =  (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->pRight->nValue = 3;
	pRoot->pRight->pFather = pRoot;

	//右的左
	pRoot->pRight->pLeft =  (BinaryTree*)malloc(sizeof(BinaryTree));
	pRoot->pRight->pLeft->nValue = 6;
	pRoot->pRight->pLeft->pFather = pRoot->pRight;
	pRoot->pRight->pLeft->pLeft = NULL;
	pRoot->pRight->pLeft->pRight = NULL;

	//右的右
	pRoot->pRight->pRight = NULL;

	return pRoot;
}



void PreOrderTrversal(BinaryTree *pTree)
{
	if(pTree == NULL)return;

	//根
	printf("%d ",pTree->nValue);

	//左
	PreOrderTrversal(pTree->pLeft);

	//右
	PreOrderTrversal(pTree->pRight);
}

void RightRotate(BinaryTree **pTree)
{
	if(*pTree == NULL)return;

	BinaryTree *pNode = NULL;
	BinaryTree *pMark = NULL;
	pNode = *pTree;
	pMark = pNode->pLeft;  //右旋 标记支点的左侧

	//三个孩子关系
	pNode->pLeft = pMark->pRight;
	pMark->pRight = pNode;

	//支点父亲是否存在  如果存在 找出支点是其父亲的左孩子还是右孩子  
	if( pNode->pFather != NULL)
	{
		if(pNode == pNode->pFather->pLeft)
		{
			pNode->pFather->pLeft = pMark;
           //如果是pNode是其父亲的左孩子,则让pMark作为父亲的左孩子        }
		else
		{
			pNode->pFather->pRight = pMark;//如果是品pNode的右孩子,则让pMark作为父亲的右孩子
		}
	}
	else
	{
		*pTree = pMark;
	}

	//三个父亲关系
	if(pNode->pLeft != NULL)
	{
		pNode->pLeft->pFather = pNode;
	}
	
	pMark->pFather = pNode->pFather;
	pNode->pFather = pMark;
}
void LeftRotate(BinaryTree **pTree)
{
	if(*pTree == NULL)return;

	BinaryTree *pNode = NULL;
	BinaryTree *pMark = NULL;
	pNode = *pTree;
	pMark = pNode->pRight;  //左旋 标记支点的右侧

	//三个孩子关系
	pNode->pRight = pMark->pLeft;
	pMark->pLeft = pNode;

	//支点父亲是否存在
	if( pNode->pFather != NULL)
	{
		if(pNode == pNode->pFather->pLeft)
		{
			pNode->pFather->pLeft = pMark;
		}
		else
		{
			pNode->pFather->pRight = pMark;
		}
	}
	else
	{
		*pTree = pMark;
	}

	//三个父亲关系
	if(pNode->pRight != NULL)
	{
		pNode->pRight->pFather = pNode;
	}
	
	pMark->pFather = pNode->pFather;
	pNode->pFather = pMark;
}



int main()
{
	BinaryTree *pTree = NULL;
	pTree = CreateBinaryTree();
	PreOrderTrversal(pTree);
	printf("\n");
	RightRotate(&pTree);
	PreOrderTrversal(pTree);
	printf("\n");
	LeftRotate(&pTree);
	PreOrderTrversal(pTree);
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值