二叉树的下一个节点

题目:
给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点?
树中的结点除了有两个分别指向左右子结点的指针以外,还有一个指向父结点的指针。

思路:
如果该节点含有右子树,那么他中序遍历的下一个节点就是他右子树的 “最” 左节点
如果他没有右子树,但是有父节点:
1.他是父节点的左子树,那么下一节点就是父节点
2.他是父节点的右子树,那么继续向上遍历,直到找到一个是自己父节点的左子树的节点,返回父节点
如果没有父节点,那么就是头结点,又没有右子树,那么返回为空

思路实现:

BinaryTreeNode* GetNext(BinaryTreeNode* pNode)
{
	if (pNode == nullptr)
		return nullptr;

	BinaryTreeNode* pRight = pNode->m_pRight;
	BinaryTreeNode* parent = pNode->m_pParent;
	BinaryTreeNode* pCurrent = pNode;
	BinaryTreeNode* pNext = nullptr;
	if (pRight)//如果有右子树
	{
		pNext = pRight;
		while (pRight->m_pLeft)
		{
			pNext = pRight->m_pLeft;
			pRight->m_pLeft = pRight->m_pLeft->m_pLeft;
		}
		return pNext;
	}
	else if (parent)//没有右子树但是有父节点
	{
		parent = pNode->m_pParent;
		BinaryTreeNode* pCurrent = pNode;
		while (parent&&pCurrent == parent->m_pRight)
		{
			pCurrent = parent;
			parent = parent->m_pParent;
		}
		pNext = parent;
	}
	//else//没有父节点,这是没有右子树的头结点,无下一节点
		//return nullptr;
	return pNext;
}

完整代码如下,包含测试代码,测试代码参照原书

#include <iostream>
#include <vector>

using namespace std;

struct BinaryTreeNode
{
	int                    m_nValue;
	BinaryTreeNode*        m_pLeft;
	BinaryTreeNode*        m_pRight;
	BinaryTreeNode*        m_pParent;
};

BinaryTreeNode* GetNext(BinaryTreeNode* pNode)
{
	if (pNode == nullptr)
		return nullptr;

	BinaryTreeNode* pRight = pNode->m_pRight;
	BinaryTreeNode* parent = pNode->m_pParent;
	BinaryTreeNode* pCurrent = pNode;
	BinaryTreeNode* pNext = nullptr;
	if (pRight)//如果有右子树
	{
		pNext = pRight;
		while (pRight->m_pLeft)
		{
			pNext = pRight->m_pLeft;
			pRight->m_pLeft = pRight->m_pLeft->m_pLeft;
		}
		return pNext;
	}
	else if (parent)//没有右子树但是有父节点
	{
		parent = pNode->m_pParent;
		BinaryTreeNode* pCurrent = pNode;
		while (parent&&pCurrent == parent->m_pRight)
		{
			pCurrent = parent;
			parent = parent->m_pParent;
		}
		pNext = parent;
	}
	//else//没有父节点,这是没有右子树的头结点,无下一节点
		//return nullptr;
	return pNext;
}
// ==================== 辅助代码用来构建二叉树 ====================
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = value;
	pNode->m_pLeft = nullptr;
	pNode->m_pRight = nullptr;
	pNode->m_pParent = nullptr;

	return pNode;
}

void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
	if (pParent != nullptr)
	{
		pParent->m_pLeft = pLeft;
		pParent->m_pRight = pRight;

		if (pLeft != nullptr)
			pLeft->m_pParent = pParent;
		if (pRight != nullptr)
			pRight->m_pParent = pParent;
	}
}

void PrintTreeNode(BinaryTreeNode* pNode)
{
	if (pNode != nullptr)
	{
		printf("value of this node is: %d\n", pNode->m_nValue);

		if (pNode->m_pLeft != nullptr)
			printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
		else
			printf("left child is null.\n");

		if (pNode->m_pRight != nullptr)
			printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
		else
			printf("right child is null.\n");
	}
	else
	{
		printf("this node is null.\n");
	}

	printf("\n");
}

void PrintTree(BinaryTreeNode* pRoot)
{
	PrintTreeNode(pRoot);

	if (pRoot != nullptr)
	{
		if (pRoot->m_pLeft != nullptr)
			PrintTree(pRoot->m_pLeft);

		if (pRoot->m_pRight != nullptr)
			PrintTree(pRoot->m_pRight);
	}
}

void DestroyTree(BinaryTreeNode* pRoot)
{
	if (pRoot != nullptr)
	{
		BinaryTreeNode* pLeft = pRoot->m_pLeft;
		BinaryTreeNode* pRight = pRoot->m_pRight;

		delete pRoot;
		pRoot = nullptr;

		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}

// ====================测试代码====================
void Test(char* testName, BinaryTreeNode* pNode, BinaryTreeNode* expected)
{
	if (testName != nullptr)
		printf("%s begins: ", testName);

	BinaryTreeNode* pNext = GetNext(pNode);
	if (pNext == expected)
		printf("Passed.\n");
	else
		printf("FAILED.\n");
}

//            8
//        6      10
//       5 7    9  11
void Test1_7()
{
	BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
	BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
	BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
	BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
	BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
	BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

	ConnectTreeNodes(pNode8, pNode6, pNode10);
	ConnectTreeNodes(pNode6, pNode5, pNode7);
	ConnectTreeNodes(pNode10, pNode9, pNode11);

	Test("Test1", pNode8, pNode9);
	Test("Test2", pNode6, pNode7);
	Test("Test3", pNode10, pNode11);
	Test("Test4", pNode5, pNode6);
	Test("Test5", pNode7, pNode8);
	Test("Test6", pNode9, pNode10);
	Test("Test7", pNode11, nullptr);

	DestroyTree(pNode8);
}

//            5
//          4
//        3
//      2
void Test8_11()
{
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
	BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
	BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
	BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);

	ConnectTreeNodes(pNode5, pNode4, nullptr);
	ConnectTreeNodes(pNode4, pNode3, nullptr);
	ConnectTreeNodes(pNode3, pNode2, nullptr);

	Test("Test8", pNode5, nullptr);
	Test("Test9", pNode4, pNode5);
	Test("Test10", pNode3, pNode4);
	Test("Test11", pNode2, pNode3);

	DestroyTree(pNode5);
}

//        2
//         3
//          4
//           5
void Test12_15()
{
	BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
	BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
	BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

	ConnectTreeNodes(pNode2, nullptr, pNode3);
	ConnectTreeNodes(pNode3, nullptr, pNode4);
	ConnectTreeNodes(pNode4, nullptr, pNode5);

	Test("Test12", pNode5, nullptr);
	Test("Test13", pNode4, pNode5);
	Test("Test14", pNode3, pNode4);
	Test("Test15", pNode2, pNode3);

	DestroyTree(pNode2);
}

void Test16()
{
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

	Test("Test16", pNode5, nullptr);

	DestroyTree(pNode5);
}

int main(int argc, char* argv[])
{
	Test1_7();
	Test8_11();
	Test12_15();
	Test16();
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值