算法描述:
输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针。
算法实现:
/************************************************************************* > File Name: main.c > Author: cyf > Mail: [email protected] > Created Time: 2016年05月05日 星期四 16时58分31秒 ************************************************************************/ #include "Convert.h" void PrintDoubleLinkedList(struct BinaryTreeNode* pHeadOfList) { struct BinaryTreeNode* pNode = pHeadOfList; printf("The nodes from left to right are:\n"); while(pNode != NULL) { printf("%d\t", pNode->value); if(pNode->right == NULL) break; pNode = pNode->right; } printf("\nThe nodes from right to left are:\n"); while(pNode != NULL) { printf("%d\t", pNode->value); if(pNode->left == NULL) break; pNode = pNode->left; } printf("\n"); } void DestroyList(struct BinaryTreeNode* pHeadOfList) { struct BinaryTreeNode* pNode = pHeadOfList; while(pNode != NULL) { struct BinaryTreeNode* pNext = pNode->right; free(pNode); pNode = pNext; } } void Test(char* testName, struct BinaryTreeNode* pRootOfTree) { if(testName != NULL) printf("%s begins:\n", testName); PrintTree(pRootOfTree); struct BinaryTreeNode* pHeadOfList = convert(pRootOfTree); PrintDoubleLinkedList(pHeadOfList); } void Test1() { struct BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); struct BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); struct BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); struct BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); struct BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); struct BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); struct BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16); ConnectTreeNodes(pNode10, pNode6, pNode14); ConnectTreeNodes(pNode6, pNode4, pNode8); ConnectTreeNodes(pNode14, pNode12, pNode16); Test("Test1", pNode10); DestroyList(pNode4); } int main() { Test1(); return 0; }
/************************************************************************* > File Name: Tree.h > Author: cyf > Mail: [email protected] > Created Time: 2016年04月14日 星期四 20时34分23秒 ************************************************************************/ #ifndef _TREE_H #define _TREE_H #include <stdio.h> #include <stdlib.h> struct BinaryTreeNode { int value; struct BinaryTreeNode *left; struct BinaryTreeNode *right; }; struct BinaryTreeNode *CreateBinaryTreeNode(int value); void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight); void PrintTreeNode(struct BinaryTreeNode *pNode); void PrintTree(struct BinaryTreeNode *pRoot); void DestroyTree(struct BinaryTreeNode *pRoot); #endif
/************************************************************************* > File Name: Tree.c > Author: cyf > Mail: [email protected] > Created Time: 2016年04月14日 星期四 20时38分43秒 ************************************************************************/ #include "BinaryTree.h" struct BinaryTreeNode *CreateBinaryTreeNode(int value) { struct BinaryTreeNode *pNode = (struct BinaryTreeNode *)malloc(sizeof(struct BinaryTreeNode)); pNode->value = value; pNode->left = NULL; pNode->right = NULL; return pNode; } void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight) { if (pRoot != NULL) { pRoot->left = pLeft; pRoot->right = pRight; } } void PrintTreeNode(struct BinaryTreeNode *pNode) { if (pNode != NULL) { printf("the value of this node is :%d\n", pNode->value); if (pNode->left != NULL) { printf("the value of left child is %d\n",pNode->left->value); } else { printf("the left child is NULL\n"); } if (pNode->right != NULL) { printf("the value of right child is %d\n",pNode->right->value); } else { printf("the right child is NULL\n"); } } printf("\n"); } void PrintTree(struct BinaryTreeNode *pRoot) { PrintTreeNode(pRoot); if (pRoot != NULL) { if (pRoot->left != NULL) PrintTree(pRoot->left); if (pRoot->right != NULL) PrintTree(pRoot->right); } } void DestroyTree(struct BinaryTreeNode *pRoot) { if (pRoot != NULL) { struct BinaryTreeNode *pLeft = pRoot->left; struct BinaryTreeNode *pRight = pRoot->right; free(pRoot); //pRoot == NULL; DestroyTree(pLeft); DestroyTree(pRight); } }
/************************************************************************* > File Name: Convert.h > Author: cyf > Mail: [email protected] > Created Time: 2016年05月05日 星期四 16时38分25秒 ************************************************************************/ #ifndef _CONVERT_H #define _CONVERT_H #include <stdio.h> #include <stdlib.h> #include "BinaryTree.h" struct BinaryTreeNode* convert(struct BinaryTreeNode *pRoot); void convertTree(struct BinaryTreeNode *pRoot, struct BinaryTreeNode **pLastNodeInList); #endif
/************************************************************************* > File Name: Convert.c > Author: cyf > Mail: [email protected] > Created Time: 2016年05月05日 星期四 16时40分37秒 ************************************************************************/ #include "Convert.h" struct BinaryTreeNode* convert(struct BinaryTreeNode *pRoot) { struct BinaryTreeNode *pLastNodeInList = NULL; convertTree(pRoot, &pLastNodeInList); struct BinaryTreeNode *pHeadList = pLastNodeInList; while (pHeadList != NULL && pHeadList->left != NULL) pHeadList = pHeadList->left; return pHeadList; } void convertTree(struct BinaryTreeNode *pRoot, struct BinaryTreeNode **pLastNodeInList) { if (pRoot == NULL) return ; struct BinaryTreeNode *pCurrent = pRoot; if (pCurrent->left != NULL) convertTree(pCurrent->left, pLastNodeInList); pCurrent->left = *pLastNodeInList; if (*pLastNodeInList != NULL) (*pLastNodeInList)->right = pCurrent; *pLastNodeInList = pCurrent; if (pCurrent->right != NULL) convertTree(pCurrent->right, pLastNodeInList); }
CC = gcc CFLAGS = -g -O2 -Wall %.o:%.c $(CC) -o $@ -c $(CFLAGS) $< main:main.o Convert.o BinaryTree.o $(CC) main.o Convert.o BinaryTree.o -o main $(CFLAGS) clean: rm -rf *.o main