二叉搜索树与双向链表
来源:互联网 发布:lv男士名片夹 淘宝 编辑:程序博客网 时间:2024/06/03 13:42
问题:输入一颗二叉搜素树,将该二叉搜索树转换成一个排序的双向链表,要求不能创建任何新的结点,只能调整树中结点指针的指向。
二叉树的结点的定义如下:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode * m_pLeft;
BinaryTreeNode* m_pRight;
};
举个栗子:
实现的详细代码如下:
#include "stdafx.h"#include "..\Utilities\BinaryTree.h"void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList);BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree){ BinaryTreeNode *pLastNodeInList = NULL; ConvertNode(pRootOfTree, &pLastNodeInList); // pLastNodeInList指向双向链表的尾结点, // 我们需要返回头结点 BinaryTreeNode *pHeadOfList = pLastNodeInList; while(pHeadOfList != NULL && pHeadOfList->m_pLeft != NULL) pHeadOfList = pHeadOfList->m_pLeft; return pHeadOfList;}void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList){ if(pNode == NULL) return; BinaryTreeNode *pCurrent = pNode; if (pCurrent->m_pLeft != NULL) ConvertNode(pCurrent->m_pLeft, pLastNodeInList); pCurrent->m_pLeft = *pLastNodeInList; if(*pLastNodeInList != NULL) (*pLastNodeInList)->m_pRight = pCurrent; *pLastNodeInList = pCurrent; if (pCurrent->m_pRight != NULL) ConvertNode(pCurrent->m_pRight, pLastNodeInList);}// ====================测试代码====================void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList){ BinaryTreeNode* pNode = pHeadOfList; printf("The nodes from left to right are:\n"); while(pNode != NULL) { printf("%d\t", pNode->m_nValue); if(pNode->m_pRight == NULL) break; pNode = pNode->m_pRight; } printf("\nThe nodes from right to left are:\n"); while(pNode != NULL) { printf("%d\t", pNode->m_nValue); if(pNode->m_pLeft == NULL) break; pNode = pNode->m_pLeft; } printf("\n");}void DestroyList(BinaryTreeNode* pHeadOfList){ BinaryTreeNode* pNode = pHeadOfList; while(pNode != NULL) { BinaryTreeNode* pNext = pNode->m_pRight; delete pNode; pNode = pNext; }}void Test(char* testName, BinaryTreeNode* pRootOfTree){ if(testName != NULL) printf("%s begins:\n", testName); PrintTree(pRootOfTree); BinaryTreeNode* pHeadOfList = Convert(pRootOfTree); PrintDoubleLinkedList(pHeadOfList);}// 10// / \// 6 14// /\ /\// 4 8 12 16void Test1(){ BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16); ConnectTreeNodes(pNode10, pNode6, pNode14); ConnectTreeNodes(pNode6, pNode4, pNode8); ConnectTreeNodes(pNode14, pNode12, pNode16); Test("Test1", pNode10); DestroyList(pNode4);}// 5// /// 4// /// 3// /// 2// /// 1void Test2(){ BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); ConnectTreeNodes(pNode5, pNode4, NULL); ConnectTreeNodes(pNode4, pNode3, NULL); ConnectTreeNodes(pNode3, pNode2, NULL); ConnectTreeNodes(pNode2, pNode1, NULL); Test("Test2", pNode5); DestroyList(pNode1);}// 1// \// 2// \// 3// \// 4// \// 5void Test3(){ BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNode1, NULL, pNode2); ConnectTreeNodes(pNode2, NULL, pNode3); ConnectTreeNodes(pNode3, NULL, pNode4); ConnectTreeNodes(pNode4, NULL, pNode5); Test("Test3", pNode1); DestroyList(pNode1);}// 树中只有1个结点void Test4(){ BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); Test("Test4", pNode1); DestroyList(pNode1);}// 树中没有结点void Test5(){ Test("Test5", NULL);}int _tmain(int argc, _TCHAR* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); return 0;}显示的部分结果如下:
0 0
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 二叉搜索树与双向链表
- 动态代理模式示例-接上文
- Makefile学习笔记(二)
- java定时器 schedule和scheduleAtFixedRate区别(转)
- php 获取变量的整数值 intval()
- java使用validator进行校验
- 二叉搜索树与双向链表
- Matlab 的基本结构
- filebeat-5.1.1配置详解
- AndroidStudio坑爹错误之一
- 关于pokeman go
- linux内核源码树建立
- 错误 强制类型转换错误 查出来是对象类 无法强制转换为list
- maven的配置及一些常用命令
- Eclipse+Maven创建webapp项目