NANA
binarytree.hpp
浏览该文件的文档.
1#pragma once
17#include <queue>
18
19namespace NANA {
20namespace GRAPH {
23
29template<typename T>
31{
35 BinTreeNode() :
36 leftChild(nullptr),
37 rightChild(nullptr) {
38 }
39 BinTreeNode(T x, BinTreeNode<T>* l = nullptr, BinTreeNode<T>* r = nullptr) :
40 data(x),
41 leftChild(l),
42 rightChild(r) {
43 }
44};
45
46
54template<typename T>
55void PreOrder(BinTreeNode<T>* node,std::queue<T> & curQueue)
56{
57 if (nullptr == node)
58 return;
59
60 curQueue.push(node->data);
61 PreOrder(node->leftChild, curQueue);
62 PreOrder(node->rightChild, curQueue);
63
64}
65
74template<typename T>
75void InOrder(BinTreeNode<T>* node, std::queue<T>& curQueue)
76{
77 if (nullptr == node)
78 return;
79
80 InOrder(node->leftChild, curQueue);
81 curQueue.push(node->data);
82 InOrder(node->rightChild, curQueue);
83
84}
85
86
95template<typename T>
96void PostOrder(BinTreeNode<T>* node, std::queue<T>& curQueue)
97{
98 if (nullptr == node)
99 return;
100 PostOrder(node->leftChild, curQueue);
101 PostOrder(node->rightChild, curQueue);
102 curQueue.push(node->data);
103
104}
105
106
107
112template<typename _T>
114
115public:
116 CBinaryTree() :
117 m_root(nullptr) {
118
119 }
120
126 bool empty() {
127 if (nullptr == m_root)
128 return true;
129 return false;
130 }
131
138 static int maxDepth(BinTreeNode<T>* root) {
139 if (nullptr == root)
140 return 0;
141 int left = maxDepth(root->leftChild) + 1;
142 int right = maxDepth(root->rightChild) + 1;
143 return std::max(left, right);
144 }
145
146protected:
148
149
150};
151
153
154
155}
156}
BinTreeNode< T > * m_root
根节点
Definition: binarytree.hpp:147
bool empty()
当前树是否维空
Definition: binarytree.hpp:126
static int maxDepth(BinTreeNode< T > *root)
二叉树的最大深度
Definition: binarytree.hpp:138
void PostOrder(BinTreeNode< T > *node, std::queue< T > &curQueue)
Definition: binarytree.hpp:96
void PreOrder(BinTreeNode< T > *node, std::queue< T > &curQueue)
基于递归的二叉树先序遍历(根->左->右)
Definition: binarytree.hpp:55
void InOrder(BinTreeNode< T > *node, std::queue< T > &curQueue)
Definition: binarytree.hpp:75
二叉树的节点
Definition: binarytree.hpp:31
BinTreeNode< T > * rightChild
右子树
Definition: binarytree.hpp:34
BinTreeNode< T > * leftChild
左子树
Definition: binarytree.hpp:33
T data
结点中存储的数据
Definition: binarytree.hpp:32