1 // 二叉搜索树转双向链表.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 using namespace std;
7
8
9 /*************************************************
10 设计者:cslave
11 版本说明:本代码免费用于商用,拷贝,转移,使用,但是
12 有本代码导致的问题,本人概不负责。
13 设计时间:2012.6.25
14 分发原则:遵守GNU规范。
15 **************************************************/
16
17
18 /**************************************************
19
20 设计一套接口,在这里让我们回忆下之前在链表那章实现的
21 代码,他有一个非常好玩的接口,反应一个设计哲学,首先
22 是创建一个链表节点,然后是将两个节点发生关系。就是先
23 创建一个节点,然后将节点间的关系添加进来。
24
25 下面我们将仿照这种设计方案,设计出自己二叉搜索书结构
26 首先创建二叉树节点,然后将二叉树节点间发生关系,有区
27 别的是,链表是两者之间的关系,而二叉树是三者之间的关系
28
29 ***************************************************/
30
31
32
33
34 struct BinaryNode
35 {
36 int m_nValue;
37 BinaryNode* m_pLeft;
38 BinaryNode* m_pRight;
39 };
40
41
42 BinaryNode* CreateNode(int Value) //创建单个的树节点
43 {
44 BinaryNode* pNode=new BinaryNode();
45 pNode->m_nValue=Value;
46 pNode->m_pLeft=NULL;
47 pNode->m_pRight=NULL;
48 return pNode;
49 }
50
51 void ConnectBinaryNode(BinaryNode* pParent,BinaryNode* pLeftChild,BinaryNode* pRightChild)
52 {
53 if(pParent==NULL)
54 throw exception("ConnectBinaryNode Failed ! pParent is NULL!\n");
55 else
56 {
57 pParent->m_pLeft=pLeftChild;
58 pParent->m_pRight=pRightChild;
59 }
60 }
61
62 void PrintBinaryNode(BinaryNode* pRoot)
63 {
64 if(pRoot)
65 {
66 cout<<"The Value Of The Node Is"<<pRoot->m_nValue<<"\t";
67 if(pRoot->m_pLeft)
68 cout<<"The Value Of The LeftNode Is"<<pRoot->m_pLeft->m_nValue<<"\t";
69 else
70 cout<<"The Value Of The LeftNode Is Empty"<<"\t";
71 if(pRoot->m_pRight)
72 cout<<"The Value Of The RightNode Is"<<pRoot->m_pRight->m_nValue<<"\t";
73 else
74 cout<<"The Value Of The RightNode Is Empty"<<"\t";
75 }
76 else
77 cout<<"pRoot Is NULL";
78 cout<<endl;
79 return ;
80 }
81
82 void PrintBinartTree(BinaryNode* pRoot)
83 {
84 PrintBinaryNode(pRoot);
85 if(pRoot)
86 {
87 if(pRoot->m_pLeft)
88 PrintBinaryNode(pRoot->m_pLeft);
89 if(pRoot->m_pRight)
90 PrintBinaryNode(pRoot->m_pRight);
91 }
92
93 }
94
95
96
97 void DestroyTree(BinaryNode* pRoot)
98 {
99 if(pRoot)
100 {
101 BinaryNode* pLeft=pRoot->m_pLeft;
102 BinaryNode* pRight=pRoot->m_pRight;
103 delete pRoot;
104 DestroyTree(pLeft);
105 DestroyTree(pRight);
106 }
107
108 }
109
110
111
112 /*************************************************
113
114 上述为完整的二叉搜索树的函数和数据,我们注意到除了
115 单节点和节点关系创建之外还有三个函数,分别为打印
116 节点和打印二叉树,还有一个销毁二叉树,总共五个函数。
117 下面我们比较下二叉搜索树和链表的创建函数
118 1 单节点创建 链表和二叉树都有
119 2 节点关系创建 链表和二叉树都有
120 3 打印单个节点 链表和二叉树都有
121 4 打印所有节点 链表和二叉树都有
122 5 销毁数据结构 链表和二叉树都有
123 6 添加单个节点 链表有,而二叉树是使用节点关系来表述
124
125 **************************************************/
126
127
128 void ConvertNode(BinaryNode* pRoot,BinaryNode** pHeadOfList)
129 {
130 if(pRoot==NULL)
131 return;
132 BinaryNode* pCurrent=pRoot;
133 if(pCurrent->m_pLeft!=NULL)
134 ConvertNode(pCurrent->m_pLeft,pHeadOfList);
135 pCurrent->m_pLeft=*pHeadOfList;
136 if(*pHeadOfList!=NULL)
137 (*pHeadOfList)->m_pRight=pCurrent;
138 *pHeadOfList=pCurrent;
139 if(pCurrent->m_pRight!=NULL)
140 ConvertNode(pCurrent->m_pRight,pHeadOfList);
141 }
142
143
144
145
146 BinaryNode* Convert(BinaryNode* pRoot)
147 {
148 BinaryNode* pLastNode=NULL;
149 ConvertNode(pRoot,&pLastNode);
150 BinaryNode* pHeadOfList=pLastNode;
151 while(pHeadOfList!=NULL&&pHeadOfList->m_pLeft!=NULL)
152 pHeadOfList=pHeadOfList->m_pLeft;
153 return pHeadOfList;
154 }
155
156 void PrintListNode(BinaryNode* pRoot)
157 {
158 if(!pRoot)
159 return;
160 BinaryNode* pTemp=pRoot;
161 while(pTemp->m_pRight)
162 {
163 cout<<"The Value Of The ListNode Is:"<<pTemp->m_nValue<<endl;
164 pTemp=pTemp->m_pRight;
165 }
166 cout<<"The Value Of The ListNode Is:"<<pTemp->m_nValue<<endl;
167 }
168
169 void Test()
170 {
171 BinaryNode* node1=CreateNode(10);
172 BinaryNode* node2=CreateNode(6);
173 BinaryNode* node3=CreateNode(14);
174 BinaryNode* node4=CreateNode(4);
175 BinaryNode* node5=CreateNode(8);
176 BinaryNode* node6=CreateNode(12);
177 BinaryNode* node7=CreateNode(16);
178 ConnectBinaryNode(node1,node2,node3);
179 ConnectBinaryNode(node2,node4,node5);
180 ConnectBinaryNode(node3,node6,node7);
181 BinaryNode* pRoot=Convert(node1);
182 PrintListNode(pRoot);
183
184
185
186 }
187
188
189 int _tmain(int argc, _TCHAR* argv[])
190 {
191 Test();
192 return 0;
193 }