沟通的目的不在于自己表达了什么,而在于对方给到你的反馈, 自由了解了对方反馈,才能真正达到沟通的目的!
1.BinTree.h
#pragma once
typedef int BTDataType
;
typedef struct BTNode
{
struct BTNode
* _pLeft
;
struct BTNode
* _pRight
;
BTDataType _data
;
}BTNode
;
BTNode
* BuyBinTreeNode(BTDataType data
);
BTNode
* _CreatBinTree(BTDataType
* array
, int size
, int* index
, BTDataType invalid
);
BTNode
* CreatBinTree(BTDataType
* array
, int size
,
BTDataType invalid
);
void* CopyBinTree(BTNode
* pRoot
);
void PreOrder(BTNode
* pRoot
);
void InOrder(BTNode
* pRoot
);
void PosOrder(BTNode
* pRoot
);
void GetKLevelNodeCount(BTNode
* pRoot
, int K
);
int GetBinTreeSize(BTNode
* pRoot
);
int GetLeafCount(BTNode
* pRoot
);
void GetBinTreeHeight(BTNode
* pRoot
);
BTNode
BinTreeFind(BTNode
* pRoot
, BTDataType x
);
void DestroyBinTree(BTNode
** pRoot
);
void TestBinTree();
2.BinTree.c
#include "BinTree.h"
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
**
BTNode
* BuyBinTreeNode(BTDataType data
){
BTNode
* pNewNode
= (BTNode
*)malloc(sizeof(BTNode
));
if (NULL == pNewNode
){
assert(0);
return NULL;
}
pNewNode
->_data
= data
;
pNewNode
->_pLeft
= NULL;
pNewNode
->_pRight
= NULL;
return pNewNode
;
}
**
BTNode
* _CreatBinTree(BTDataType
* array
, int size
, int* index
, BTDataType invalid
{
BTNode
* pRoot
= NULL;
if (*index
< size
&& invalid
!= array
[*index
]){
pRoot
= BuyBinTreeNode(array
[*index
]);
++(*index
);
pRoot
->_pLeft
= _CreatBinTree(array
, size
, index
, invalid
);
++(*index
);
pRoot
->_pRight
= _CreatBinTree(array
, size
, index
, invalid
);
}
return pRoot
;
}
**
BTNode
* CreatBinTree(BTDataType
* array
, int size
,BTDataType invalid
){
int index
= 0;
return _CreatBinTree(array
, size
, &index
, invalid
);
}
**
void PreOrder(BTNode
* pRoot
){
if (pRoot
){
printf("%c ", pRoot
->_data
);
PreOrder(pRoot
->_pLeft
);
PreOrder(pRoot
->_pRight
);
}
}
**
void InOrder(BTNode
* pRoot
){
if (pRoot
){
InOrder(pRoot
->_pLeft
);
printf("%c ", pRoot
->_data
);
InOrder(pRoot
->_pRight
);
}
}
**
void PosOrder(BTNode
* pRoot
){
if (pRoot
){
PosOrder(pRoot
->_pLeft
);
PosOrder(pRoot
->_pRight
);
printf("%c ", pRoot
->_data
);
}
}
**
int GetLeafCount(BTNode
* pRoot
){
if (NULL == pRoot
){
return 0;
}
if (NULL == pRoot
->_pLeft
&& NULL == pRoot
->_pRight
){
return 1;
}
return GetLeafCount(pRoot
->_pLeft
) + GetLeafCount(pRoot
->_pRight
);
}
**
void DestroyBinTree(BTNode
** pRoot
){
if (*pRoot
){
DestroyBinTree(&(*pRoot
)->_pLeft
);
DestroyBinTree(&(*pRoot
)->_pRight
);
free(*pRoot
);
*pRoot
= NULL;
}
}
**
void TestBinTree(){
char* str
= "ABD$$$CE$$F";
BTNode
* pRoot
= CreatBinTree(str
, strlen(str
),'$');
printf("前序遍历结果: ");
PreOrder(pRoot
);
printf("\n");
printf("中序遍历结果: ");
InOrder(pRoot
);
printf("\n");
printf("后序遍历结果: ");
PosOrder(pRoot
);
printf("\n");
DestroyBinTree(&pRoot
);
}
3.test.c
#include "BinTree.h"
int main(){
TestBinTree();
system("pause");
return 0;
}
#include <iostream>
using namespace std
;
#if 0
void HeapAdjust(int* array
, int size
, int parent
)
{
int child
= parent
* 2 + 1;
while (child
< size
)
{
if (child
+1 < size
&& array
[child
+1] > array
[child
])
{
child
+= 1;
}
if (array
[parent
] < array
[child
])
{
swap(array
[parent
], array
[child
]);
parent
= child
;
child
= parent
* 2 + 1;
}
else
return;
}
}
void HeapSort(int* array
, int size
)
{
int root
= ((size
- 2) >> 1);
for (; root
>= 0; --root
)
HeapAdjust(array
, size
, root
);
int end
= size
- 1;
while (end
)
{
swap(array
[0], array
[end
]);
HeapAdjust(array
, end
, 0);
--end
;
}
}
int main()
{
int array
[] = {3,4,1,9,0,6,8,5,2,7};
HeapSort(array
, sizeof(array
) / sizeof(array
[0]));
return 0;
}
#endif
#if 0
template<class T, class Compare>
void HeapAdjust(T
* array
, int size
, int parent
, Compare com
)
{
int child
= parent
* 2 + 1;
while (child
< size
)
{
if (child
+ 1 < size
&& com(array
[child
+ 1], array
[child
]))
{
child
+= 1;
}
if (com(array
[child
], array
[parent
]))
{
swap(array
[parent
], array
[child
]);
parent
= child
;
child
= parent
* 2 + 1;
}
else
return;
}
}
template<class T, class Compare>
void HeapSort(T
* array
, int size
, Compare com
)
{
int root
= ((size
- 2) >> 1);
for (; root
>= 0; --root
)
HeapAdjust(array
, size
, root
, com
);
int end
= size
- 1;
while (end
)
{
swap(array
[0], array
[end
]);
HeapAdjust(array
, end
, 0, com
);
--end
;
}
}
#include <functional>
int main()
{
int array
[] = { 3, 4, 1, 9, 0, 6, 8, 5, 2, 7 };
HeapSort(array
, sizeof(array
) / sizeof(array
[0]), less
<int>());
return 0;
}
#endif
#include <vector>
struct BTNode
{
BTNode(int data
)
: _pLeft(nullptr)
, _pRight(nullptr)
, _data(data
)
{}
BTNode
* _pLeft
;
BTNode
* _pRight
;
int _data
;
};
BTNode
* CreateBinTree(const vector
<int>& v
, int& index
, const int invalid
)
{
BTNode
* pRoot
= nullptr;
if (index
< v
.size() && invalid
!= v
[index
])
{
pRoot
= new BTNode(v
[index
]);
pRoot
->_pLeft
= CreateBinTree(v
, ++index
, invalid
);
pRoot
->_pRight
= CreateBinTree(v
, ++index
, invalid
);
}
return pRoot
;
}
void Destroy(BTNode
*& pRoot
)
{
if (pRoot
)
{
Destroy(pRoot
->_pLeft
);
Destroy(pRoot
->_pRight
);
delete pRoot
;
pRoot
= nullptr;
}
}
void PreOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
cout
<< pRoot
->_data
<< " ";
PreOrder(pRoot
->_pLeft
);
PreOrder(pRoot
->_pRight
);
}
}
#include <stack>
void PreOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
s
.push(pRoot
);
while (!s
.empty())
{
BTNode
* pCur
= s
.top();
s
.pop();
cout
<< pCur
->_data
<< " ";
if (pCur
->_pRight
)
s
.push(pCur
->_pRight
);
if (pCur
->_pLeft
)
s
.push(pCur
->_pLeft
);
}
}
void InOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
InOrder(pRoot
->_pLeft
);
cout
<< pRoot
->_data
<< " ";
InOrder(pRoot
->_pRight
);
}
}
void InOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
BTNode
* pCur
= pRoot
;
while (!s
.empty() || pCur
)
{
while (pCur
)
{
s
.push(pCur
);
pCur
= pCur
->_pLeft
;
}
pCur
= s
.top();
cout
<< pCur
->_data
<< " ";
s
.pop();
pCur
= pCur
->_pRight
;
}
}
void PostOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
PostOrder(pRoot
->_pLeft
);
PostOrder(pRoot
->_pRight
);
cout
<< pRoot
->_data
<< " ";
}
}
void PostOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
BTNode
* pCur
= pRoot
;
BTNode
* pPrev
= nullptr;
while (!s
.empty() || pCur
)
{
while (pCur
)
{
s
.push(pCur
);
pCur
= pCur
->_pLeft
;
}
BTNode
* pTop
= s
.top();
if (nullptr == pTop
->_pRight
||
pTop
->_pRight
== pPrev
)
{
cout
<< pTop
->_data
<< " ";
pPrev
= pTop
;
s
.pop();
}
else
{
pCur
= pTop
->_pRight
;
}
}
}
#include <queue>
void LevelOrder(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
queue
<BTNode
*> q
;
q
.push(pRoot
);
while (!q
.empty())
{
BTNode
* pCur
= q
.front();
cout
<< pCur
->_data
<< " ";
if (pCur
->_pLeft
)
q
.push(pCur
->_pLeft
);
if (pCur
->_pRight
)
q
.push(pCur
->_pRight
);
q
.pop();
}
}
size_t
Height(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
size_t leftHeight
= Height(pRoot
->_pLeft
);
size_t rightHeight
= Height(pRoot
->_pRight
);
return leftHeight
> rightHeight
? leftHeight
+ 1 : rightHeight
+ 1;
}
size_t
GetBTreeNode(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
return GetBTreeNode(pRoot
->_pLeft
) +
GetBTreeNode(pRoot
->_pRight
) + 1;
}
size_t
GetLeafNode(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
if (nullptr == pRoot
->_pLeft
&& nullptr == pRoot
->_pRight
)
{
return 1;
}
return GetLeafNode(pRoot
->_pLeft
) + GetLeafNode(pRoot
->_pRight
);
}
size_t
GetKLevelNodeCount(BTNode
* pRoot
, size_t K
)
{
if (nullptr == pRoot
|| 0 == K
)
return 0;
if (1 == K
)
return 1;
return GetKLevelNodeCount(pRoot
->_pLeft
, K
- 1) +
GetKLevelNodeCount(pRoot
->_pRight
, K
- 1);
}
BTNode
* GetParent(BTNode
* pRoot
, BTNode
* pNode
)
{
if (nullptr == pRoot
|| pRoot
== pNode
)
return nullptr;
if (pRoot
->_pLeft
== pNode
|| pRoot
->_pRight
== pNode
)
return pRoot
;
BTNode
* pParent
= GetParent(pRoot
->_pLeft
, pNode
);
if (pParent
)
return pParent
;
return GetParent(pRoot
->_pRight
, pNode
);
}
BTNode
* _ReBuildTree(const vector
<int>& pre
, int& index
, const vector
<int>& In
, int left
, int right
)
{
if (left
>= right
)
return nullptr;
BTNode
* pRoot
= new BTNode(pre
[index
]);
size_t InIdx
= left
;
while (In
[InIdx
] != pre
[index
])
InIdx
++;
if (left
< InIdx
)
pRoot
->_pLeft
= _ReBuildTree(pre
, ++index
, In
, left
, InIdx
);
if (index
+1 < right
)
pRoot
->_pRight
= _ReBuildTree(pre
, ++index
, In
, InIdx
+ 1, right
);
return pRoot
;
}
BTNode
* ReBuildTree(const vector
<int>& pre
, const vector
<int>& In
)
{
int index
= 0;
return _ReBuildTree(pre
, index
, In
, 0, In
.size());
}
int main()
{
vector
<int> v
{ 1, 2, 3, -1,-1,-1,4, 5, -1,-1,6 };
int index
= 0;
BTNode
* pRoot
= CreateBinTree(v
, index
, -1);
PreOrder(pRoot
);
PreOrderNor(pRoot
);
cout
<< endl
;
InOrder(pRoot
);
InOrderNor(pRoot
);
cout
<< endl
;
PostOrder(pRoot
);
PostOrderNor(pRoot
);
Destroy(pRoot
);
vector
<int> Pre
{ 1, 2, 3, 4, 5, 6 };
vector
<int> In
{3,2,1,5,4,6};
BTNode
* pNewRoot
= ReBuildTree(Pre
, In
);
return 0;
}
转载请注明原文地址: https://yun.8miu.com/read-19686.html