面试题 -二元查找树转变成排序的双向链表

简介: 1 // Tree.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include 6 #include 7 using namespace std; 8 9 template ...
  1 // Tree.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <malloc.h>
  6 #include <iostream>
  7 using namespace std;
  8 
  9 template<class Type>
 10 struct BinaryNode
 11 {
 12 private:
 13     Type value;
 14     BinaryNode<Type> *left;
 15     BinaryNode<Type> *right;
 16 public:
 17     BinaryNode(Type val):left(NULL),right(NULL)
 18     {
 19         value = val;
 20     }
 21     BinaryNode<Type> * GetLeftBiggest();
 22     BinaryNode<Type> * GetRightSmallest();
 23     void convert();
 24     void Insert(BinaryNode<Type> *ptr);
 25     void Print();
 26     void PrintList();
 27 };
 28 
 29 template<class Type>
 30 void BinaryNode<Type>::Print()
 31 {
 32     if(this==NULL)
 33     {
 34         return;
 35     }
 36     cout<<value<<endl;    
 37     left->Print();
 38     right->Print();
 39 }
 40 
 41 template<class Type>
 42 void BinaryNode<Type>::PrintList()
 43 {
 44     BinaryNode *l = left;
 45     BinaryNode *r = right;
 46 
 47     while(l->left!=NULL)
 48     {
 49         l = l->left;
 50     }
 51     while(l->right!=NULL)
 52     {
 53         cout<<l->value<<endl;
 54         l = l->right;
 55     }
 56     while(l->left != NULL)
 57     {
 58         cout<<l->value<<endl;
 59         l=l->left;
 60     }
 61 
 62 }
 63 
 64 
 65 
 66 template<class Type>
 67 BinaryNode<Type> * BinaryNode<Type>::GetLeftBiggest()
 68 {
 69     BinaryNode<Type> *result=NULL;
 70     if(left!= NULL)
 71     {
 72         result = left;
 73         while(result->right!=NULL)
 74         {
 75             result = result->right;
 76         }
 77     }
 78     return result;
 79 
 80 }
 81 
 82 template<class Type>
 83 BinaryNode<Type> * BinaryNode<Type>::GetRightSmallest()
 84 {
 85     BinaryNode<Type> *result=NULL;
 86     if(right!= NULL)
 87     {
 88         result = right;
 89         while(result->left!=NULL)
 90         {
 91             result = result->left;
 92         }
 93     }
 94         return result;
 95 }
 96 
 97 template<class Type>
 98 void BinaryNode<Type>::convert( )
 99 {
100     if(left!=NULL)
101     {
102         BinaryNode<Type> *ptr = GetLeftBiggest();
103         left->convert();
104         ptr->right = this;
105         this->left = ptr;
106     }
107     if(right!=NULL)
108     {
109         BinaryNode<Type> * ptr = GetRightSmallest();
110         right->convert();
111         ptr->left = this;
112         this->right = ptr;
113     }
114 }
115 
116 template<class Type>
117 void BinaryNode<Type>::Insert(BinaryNode<Type> *ptr)
118 {
119     if((value>=ptr->value)&&(left == NULL))
120     {
121         left = ptr;
122         return;
123     }
124     else if((value>=ptr->value)&&(left != NULL))
125     {
126         return left->Insert(ptr);
127     }
128     else if((value<ptr->value)&&(right == NULL))
129     {
130         right = ptr;
131         return;
132     }
133     else
134     {
135         return right->Insert(ptr);
136     }
137 }
138 
139 int _tmain(int argc, _TCHAR* argv[])
140 {
141     int value;
142     cout<<"Please input the root value"<<endl;
143     cin>>value;
144     BinaryNode<int> *root=  new BinaryNode<int>(value);;
145     
146     int TreeNodeNumber;
147     cout<<"Please input the Tree Node Numbers:"<<endl;
148     cin>>TreeNodeNumber;
149     BinaryNode<int> **nodes = new BinaryNode<int> *[TreeNodeNumber];
150     for(int index=0;index<TreeNodeNumber;index++)
151     {
152         int value;
153         cout<<"Please input the "<<index+1<<" Node"<<endl;
154         cin>>value;
155         nodes[index] = new BinaryNode<int>(value);        
156         root->Insert(nodes[index]);
157     }
158 
159     root->Print();
160     root->convert();
161     root->PrintList();
162     
163     return 0;
164 }

参考:

http://blog.csdn.net/anialy/article/details/7645535

 


作者: HarlanC

博客地址: http://www.cnblogs.com/harlanc/
个人博客: http://www.harlancn.me/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

如果觉的博主写的可以,收到您的赞会是很大的动力,如果您觉的不好,您可以投反对票,但麻烦您留言写下问题在哪里,这样才能共同进步。谢谢!

目录
相关文章
|
4月前
|
机器学习/深度学习 算法
24. 两两交换链表中的节点, 19.删除链表的倒数第N个节点 ,面试题 02.07. 链表相交
1. **两两交换链表中的节点**:通过引入虚拟头结点,使所有节点都能采用统一的交换逻辑,避免对头结点单独处理。 2. **删除链表的倒数第N个节点**:利用双指针技巧,让快慢指针保持N个节点的距离,当快指针到达末尾时,慢指针正好指向待删除节点的前一个节点。 3. **链表相交**:先计算两链表长度并调整起点,确保从相同距离末尾的位置开始遍历,从而高效找到相交节点或确定无交点。 以上方法均在时间复杂度和空间复杂度上进行了优化,适合用于理解和掌握链表的基本操作及常见算法设计思路。
|
存储 算法 搜索推荐
探索常见数据结构:数组、链表、栈、队列、树和图
探索常见数据结构:数组、链表、栈、队列、树和图
378 64
|
11月前
|
存储 缓存 算法
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式,强调了合理选择数据结构的重要性,并通过案例分析展示了其在实际项目中的应用,旨在帮助读者提升编程能力。
324 5
|
存储 Java
HashMap之链表转红黑树(树化 )-treefyBin方法源码解读(所有涉及到的方法均有详细解读,欢迎指正)
本文详细解析了Java HashMap中链表转红黑树的机制,包括树化条件(链表长度达8且数组长度≥64)及转换流程,确保高效处理大量数据。
528 1
|
存储 算法
LeetCode第83题删除排序链表中的重复元素
文章介绍了LeetCode第83题"删除排序链表中的重复元素"的解法,使用双指针技术在原链表上原地删除重复元素,提供了一种时间和空间效率都较高的解决方案。
LeetCode第83题删除排序链表中的重复元素
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
142 0
|
存储 算法 安全
HashMap常见面试题(超全面):实现原理、扩容机制、链表何时升级为红黑树、死循环
HashMap常见面试题:红黑树、散列表,HashMap实现原理、扩容机制,HashMap的jd1.7与jdk1.8有什么区别,寻址算法、链表何时升级为红黑树、死循环
|
算法
❤️算法笔记❤️-(每日一刷-83、删除排序链表中的重复项)
❤️算法笔记❤️-(每日一刷-83、删除排序链表中的重复项)
143 0
|
存储 算法 Python
【面试题】合井K个升序链表
【面试题】合井K个升序链表
83 0
|
存储 Java
【Java集合类面试十】、HashMap中的循环链表是如何产生的?
在多线程环境下,HashMap在扩容时如果发生条件竞争,元素的插入顺序可能形成循环链表,导致死循环。