-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.h
70 lines (61 loc) · 1.83 KB
/
btree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* =====================================================================================
*
* Filename: btree.h
*
* Description: 二叉树
*
* Version: 1.0
* Created: 2015年03月27日 19时54分57秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_
template <typename T>
class BTNode
{
public:
BTNode():
data(T()), left(nullptr), right(nullptr)
{}
BTNode(const T& initdata):
data(initdata), left(nullptr), right(nullptr)
{}
T data;
BTNode* left,right;
};
template <typename T>
class BTree
{
public:
BTree();
BTree(const T& initdata);
~BTree();
BTNode<T>* GetRoot() const;
BTNode<T>* GetLeftChild(const BTNode<T>* p) const;
BTNode<T>* GetRightChild(const BTNode<T>* p) const;
int IsEmpty() const;
void Destroy();
void PreOrderTraverse(void (*visit)(const T& data)) const;
void InOrderTraverse(void (*visit)(const T& data)) const;
void PostOrderTraverse(void (*visit)(const T& data)) const;
unsigned int GetNodesCount() const;
unsigned int GetLeafCount() const;
unsigned int GetDepth() const;
private:
void DestroyNode(BTNode<T> *p);
void PreOrderTraverse(const BTNode<T> *p, void (*visit)(const T& data)) const;
void InOrderTraverse(const BTNode<T> *p, void (*visit)(const T& data)) const;
void PostOrderTraverse(const BTNode<T> *p, void (*visit)(const T& data)) const;
void GetNodesCount(const BTNode<T> *p, unsigned int &count) const;
void GetLeafCount(const BTNode<T> *p, unsigned int &count) const;
void GetDepth(const BTNode<T> *p, unsigned int &depth) const;
BTNode<T> *m_pRoot;
};
#endif