Skip to content

Commit d343b9d

Browse files
committed
add tree/bianry_search_tree/*
1 parent 3c9c934 commit d343b9d

12 files changed

+415
-0
lines changed

tree/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### 树的基本概念
2+
3+
1. 树: 一种递归定义的数据结构。可以是一颗空树,没有任何结点。
4+
2. 子树: 非空树除根节点外,其余结点可以分为n(n>=0)个互不相交的集合,每个集合本身都是一颗树,他们就是子树。
5+
3. 结点的度: 结点拥有的子树数目。
6+
4. 深度: 数中结点最大层次的值。
7+
5. 有序树: 树中的各子树自左向右有序的称为有序树。

tree/binary_search_tree/a.out

98.5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#pragma once
2+
3+
#include "./binary_search_tree_node.hpp"
4+
#include "./binary_search_tree_interface.h"
5+
6+
#include <iostream>
7+
#include <queue>
8+
9+
namespace mtl
10+
{
11+
template <typename anytype>
12+
class BinarySearchTree : public BinarySearchTreeInterface<BinarySearchTreeNode<anytype>>
13+
{
14+
public:
15+
//构造函数
16+
BinarySearchTree() : m_root(nullptr) {}
17+
18+
//析构函数
19+
~BinarySearchTree()
20+
{
21+
erase();
22+
}
23+
24+
//创建搜索二叉树
25+
void create()
26+
{
27+
create(&m_root);
28+
}
29+
30+
//判空
31+
bool empty() const override
32+
{
33+
return 0 == m_size;
34+
}
35+
36+
//获得结点树
37+
int size() const override
38+
{
39+
return m_size;
40+
}
41+
42+
//前序遍历
43+
void preOrder(void(*func)(BinarySearchTreeNode<anytype> *))
44+
{
45+
visit = func;
46+
preOrder(m_root);
47+
std::cout << std::endl;
48+
}
49+
50+
//中序遍历
51+
void inOrder(void(*func)(BinarySearchTreeNode<anytype> *))
52+
{
53+
visit = func;
54+
inOrder(m_root);
55+
std::cout << std::endl;
56+
}
57+
58+
//后序遍历
59+
void postOrder(void(*func)(BinarySearchTreeNode<anytype> *))
60+
{
61+
visit = func;
62+
postOrder(m_root);
63+
std::cout << std::endl;
64+
}
65+
66+
//按层遍历
67+
void levelOrder(void(*func)(BinarySearchTreeNode<anytype> *))
68+
{
69+
visit = func;
70+
levelOrder(m_root);
71+
std::cout << std::endl;
72+
}
73+
74+
//获得高度
75+
int height() const
76+
{
77+
return height(m_root);
78+
}
79+
80+
//获得根结点
81+
BinarySearchTreeNode<anytype>* getRoot()
82+
{
83+
return m_root;
84+
}
85+
86+
//删除搜索树
87+
void erase()
88+
{
89+
postOrder(dispose);
90+
m_root = nullptr;
91+
m_size = 0;
92+
}
93+
94+
private:
95+
BinarySearchTreeNode<anytype> *m_root;
96+
static int m_size;
97+
98+
static void (*visit)(BinarySearchTreeNode<anytype> *);
99+
static void preOrder(BinarySearchTreeNode<anytype> *);
100+
static void inOrder(BinarySearchTreeNode<anytype> *);
101+
static void postOrder(BinarySearchTreeNode<anytype> *);
102+
static void levelOrder(BinarySearchTreeNode<anytype> *);
103+
static int height(BinarySearchTreeNode<anytype> *);
104+
static void dispose(BinarySearchTreeNode<anytype> *);
105+
static void output(BinarySearchTreeNode<anytype> *);
106+
static void create(BinarySearchTreeNode<anytype> **);
107+
};
108+
109+
//静态函数指针初始化
110+
template <typename anytype>
111+
void (*BinarySearchTree<anytype>::visit)(BinarySearchTreeNode<anytype> *) = nullptr;
112+
113+
//静态变量size初始化
114+
template <typename anytype>
115+
int BinarySearchTree<anytype>::m_size = 0;
116+
117+
//私有前序遍历
118+
template <typename anytype>
119+
void BinarySearchTree<anytype>::preOrder(BinarySearchTreeNode<anytype> *root)
120+
{
121+
if (nullptr != root)
122+
{
123+
BinarySearchTree<anytype>::visit(root);
124+
preOrder(root->m_left_child);
125+
preOrder(root->m_right_child);
126+
}
127+
}
128+
129+
//私有中序遍历
130+
template <typename anytype>
131+
void BinarySearchTree<anytype>::inOrder(BinarySearchTreeNode<anytype> *root)
132+
{
133+
if (nullptr != root)
134+
{
135+
inOrder(root->m_left_child);
136+
BinarySearchTree<anytype>::visit(root);
137+
inOrder(root->m_right_child);
138+
}
139+
}
140+
141+
//私有后序遍历
142+
template <typename anytype>
143+
void BinarySearchTree<anytype>::postOrder(BinarySearchTreeNode<anytype> *root)
144+
{
145+
if (nullptr != root)
146+
{
147+
postOrder(root->m_left_child);
148+
postOrder(root->m_right_child);
149+
BinarySearchTree<anytype>::visit(root);
150+
}
151+
}
152+
153+
//私有按层遍历
154+
template <typename anytype>
155+
void BinarySearchTree<anytype>::levelOrder(BinarySearchTreeNode<anytype> *root)
156+
{
157+
std::queue<BinarySearchTreeNode<anytype> *> q;
158+
while (nullptr != root)
159+
{
160+
BinarySearchTree<anytype>::visit(root);
161+
162+
if (nullptr != root->m_left_child);
163+
{
164+
q.push(root->m_left_child);
165+
}
166+
167+
if (nullptr != root->m_right_child)
168+
{
169+
q.push(root->m_right_child);
170+
}
171+
172+
if (q.empty())
173+
{
174+
return;
175+
}
176+
177+
root = q.front();
178+
q.pop();
179+
}
180+
}
181+
182+
//获得高度
183+
template <typename anytype>
184+
int BinarySearchTree<anytype>::height(BinarySearchTreeNode<anytype> *root)
185+
{
186+
if (nullptr == root)
187+
{
188+
return 0;
189+
}
190+
else
191+
{
192+
int height_left = height(root->m_left_child);
193+
int height_right = height(root->m_right_child);
194+
return height_left > height_right ? ++height_left : ++height_right;
195+
}
196+
}
197+
198+
//删除结点
199+
template <typename anytype>
200+
void BinarySearchTree<anytype>::dispose(BinarySearchTreeNode<anytype> *root)
201+
{
202+
delete root;
203+
}
204+
205+
//创建树
206+
template <typename anytype>
207+
void BinarySearchTree<anytype>::create(BinarySearchTreeNode<anytype> **root)
208+
{
209+
anytype data;
210+
std::cin.clear();
211+
std::cin >> data;
212+
213+
if ('#' != data)
214+
{
215+
++m_size;
216+
*root = new BinarySearchTreeNode<anytype>(data);
217+
218+
if (nullptr != *root)
219+
{
220+
create(&(*root)->m_left_child);
221+
create(&(*root)->m_right_child);
222+
}
223+
else
224+
{
225+
std::cerr << __FILE__ << ' ' << __LINE__ << "#" << __FUNCTION__ << " 内存申请失败\n";
226+
}
227+
}
228+
else
229+
{
230+
*root = nullptr;
231+
}
232+
}
233+
234+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*************************************************************************
2+
> File Name: binary_tree_interface.h
3+
> Author: JTrancender
4+
5+
> Github: JieTrancender
6+
> Created Time: Mon Oct 24 19:44:31 2016
7+
************************************************************************/
8+
#pragma once
9+
10+
namespace mtl
11+
{
12+
template <typename anytype>
13+
class BinarySearchTreeInterface
14+
{
15+
public:
16+
//构造函数
17+
virtual ~BinarySearchTreeInterface() {} //函数体无内容
18+
19+
//判空
20+
virtual bool empty() const = 0;
21+
22+
//查看元素个数
23+
virtual int size() const = 0;
24+
25+
//前序遍历
26+
virtual void preOrder(void(*)(anytype *)) = 0;
27+
28+
//中序遍历
29+
virtual void inOrder(void(*)(anytype *)) = 0;
30+
31+
//后序遍历
32+
virtual void postOrder(void(*)(anytype *)) = 0;
33+
34+
//按层遍历
35+
virtual void levelOrder(void(*)(anytype *)) = 0;
36+
};
37+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
5+
//mtl stand for My Test Library
6+
namespace mtl
7+
{
8+
template <typename anytype>
9+
struct BinarySearchTreeNode
10+
{
11+
anytype m_element;
12+
BinarySearchTreeNode<anytype> *m_left_child;
13+
BinarySearchTreeNode<anytype> *m_right_child;
14+
15+
//构造函数
16+
BinarySearchTreeNode()
17+
{
18+
m_left_child = m_right_child = nullptr;
19+
}
20+
21+
//构造函数
22+
BinarySearchTreeNode(anytype const& element) : BinarySearchTreeNode() //委托构造函数
23+
{
24+
m_element = element;
25+
}
26+
27+
//构造函数
28+
BinarySearchTreeNode(anytype const& element, BinarySearchTreeNode* left_child, BinarySearchTreeNode* right_child)
29+
: m_element(element), m_left_child(left_child), m_right_child(right_child) {} //函数体无内容
30+
};
31+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include "./binary_search_tree_node.hpp"
4+
5+
#include <iostream>
6+
7+
template <typename anytype>
8+
mtl::BinarySearchTreeNode<anytype>* treeToLinkedList(mtl::BinarySearchTreeNode<anytype> *root)
9+
{
10+
mtl::BinarySearchTreeNode<anytype> *head, *tail;
11+
helper(head, tail, root);
12+
13+
return head;
14+
}
15+
16+
template <typename anytype>
17+
void helper(mtl::BinarySearchTreeNode<anytype>* &head, mtl::BinarySearchTreeNode<anytype>*& tail, mtl::BinarySearchTreeNode<anytype>* root)
18+
{
19+
mtl::BinarySearchTreeNode<anytype>* left_tail, right_head;
20+
21+
if (nullptr == root)
22+
{
23+
head = tail = nullptr;
24+
return;
25+
}
26+
27+
helper(head, left_tail, root->m_left_child);
28+
helper(right_head, tail, root->m_right_child);
29+
30+
if (nullptr != left_tail)
31+
{
32+
left_tail->m_right_child = root;
33+
root->m_left_child = left_tail;
34+
}
35+
else
36+
{
37+
head = root;
38+
}
39+
40+
if (nullptr != right_head)
41+
{
42+
root->m_right_child = right_head;
43+
right_head->m_left_child = root;
44+
}
45+
else
46+
{
47+
tail = root;
48+
}
49+
}
Binary file not shown.

0 commit comments

Comments
 (0)