二叉树的翻转代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aaa_cainiao_66666/article/details/79954140


代码如下:

#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;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页