将二叉查找树转化为链表的代码实现
2012-04-09 16:00
288 查看
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
根据提供的思路,我自己写了一个读入任意序列的整数,建立二叉查找树再改成链表的C++代码:
样例输入:
样例输出:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
根据提供的思路,我自己写了一个读入任意序列的整数,建立二叉查找树再改成链表的C++代码:
#include <iostream> #include <fstream> #include <string> using namespace std; // binary search tree node struct BSTreeNode { int value; struct BSTreeNode *leftChild; struct BSTreeNode *rightChild; }; void addBSTreeNode(struct BSTreeNode **ppRoot/*double pointer to the root*/, int new_value); void printBSTree(ofstream *fout, struct BSTreeNode *root, int level); void treeToLinkedList(struct BSTreeNode **ppHead, struct BSTreeNode **ppTail, struct BSTreeNode *root); int main() { ifstream fin ("tree_to_linked_list.in"); ofstream fout ("tree_to_linked_list.out"); int in; struct BSTreeNode *root = NULL, *head = NULL, *tail = NULL, *node = NULL; if(fin.good() == false) { cerr << "file open failed" << endl; return 0; } while(true) { if(fin.eof()) break; fin >> in; addBSTreeNode(&root, in); } printBSTree(&fout, root, 0); fout << endl; treeToLinkedList(&head, &tail, root); for(node = head; node != NULL; node = node->rightChild) { fout << node->value << " "; } fout << endl; for(node = tail; node != NULL; node = node->leftChild) { fout << node->value << " "; } fout << endl; return 0; } void addBSTreeNode(struct BSTreeNode **ppRoot/*double pointer to the root*/, int new_value) { if(*ppRoot == NULL) { *ppRoot = new struct BSTreeNode(); (*ppRoot)->value = new_value; return; } if(new_value == (*ppRoot)->value) { return; } else if(new_value < (*ppRoot)->value) { addBSTreeNode(&(*ppRoot)->leftChild, new_value); } else { addBSTreeNode(&(*ppRoot)->rightChild, new_value); } } void printBSTree(ofstream *pFout, struct BSTreeNode *root, int level) { if(root == NULL) return; printBSTree(pFout, root->leftChild, level + 1); int i; for(i = 0; i < level; i ++) (*pFout) << "\t"; (*pFout) << root->value << endl; printBSTree(pFout, root->rightChild, level + 1); } void treeToLinkedList(struct BSTreeNode **ppHead, struct BSTreeNode **ppTail, struct BSTreeNode *root) { struct BSTreeNode *ltemp, *rtemp; if(root == NULL) { (*ppHead) = NULL; (*ppTail) = NULL; return; } treeToLinkedList(ppHead, <emp, root->leftChild); treeToLinkedList(&rtemp, ppTail, root->rightChild); if(*ppHead == NULL) { (*ppHead) = root; root->leftChild = NULL; } else { root->leftChild = ltemp; ltemp->rightChild = root; } if(*ppTail == NULL) { (*ppTail) = root; root->rightChild = NULL; } else { root->rightChild = rtemp; rtemp->leftChild = root; } }
样例输入:
10 5 14 45 64 3 7 12 11 33 52 63 23 46 73 44 66 22 45 67 78 83
样例输出:
3 5 7 10 11 12 14 22 23 33 44 45 46 52 63 64 66 67 73 78 83 3 5 7 10 11 12 14 22 23 33 44 45 46 52 63 64 66 67 73 78 83 83 78 73 67 66 64 63 52 46 45 44 33 23 22 14 12 11 10 7 5 3
相关文章推荐
- 线性表之<链表linklist>的储存及插入、删除-C代码实现
- 剑指offer:从尾到头打印链表代码实现
- Java编程实现从尾到头打印链表代码实例
- STL之顺序容器适配器(栈的链表实现代码)
- 100行代码用C++基本实现双向链表
- 通讯录实现代码 C++ 链表
- 如何判断链表有没有环,环的大小?环的切入点在哪里以及链表的长度(分别用javascrip和Java代码实现)
- 二叉查找树C实现代码
- 用范型实现链表代码理解
- C# 泛型实现链表、代码重用
- 恳求帮助!!!怎么用程序实现:将HTML代码转化为MHTML代码
- 链表的代码实现
- 探究数据结构之链表Java代码实现(一)
- 数字转化成字符串 (自己用代码实现)
- C++学习笔记(四)指针实现的链表、堆栈、队列、二叉查找树
- 前序、中序、后序相互转化的C代码简单递归实现
- 线性表之单链表 图解和代码实现
- 二叉查找树——算法导论第12章简易代码实现~
- 链表数据结构图解 和 代码实现
- 在实际项目中实现把swf资源里面的MC进行转化成按钮,实现代码的复用