Skip to content

Commit 26b8bfd

Browse files
committed
update about tree
1 parent e82d4d7 commit 26b8bfd

File tree

11 files changed

+291
-126
lines changed

11 files changed

+291
-126
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,6 @@ ENV/
201201
.ropeproject
202202

203203
.idea
204+
205+
.vscode
206+
.pytest_cache

Diff for: Pipfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[[source]]
2-
url = "https://pypi.python.org/simple"
3-
verify_ssl = true
2+
url = "https://mirrors.ustc.edu.cn/pypi/web/simple"
43
name = "pypi"
4+
format = "columns"
55

66
[packages]
77

88
[dev-packages]
9+
pylint = "*"
10+
"autopep8" = "*"
11+
pytest = "*"
912

1013
[requires]
1114
python_version = "3.6"

Diff for: Pipfile.lock

+132-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: README.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
# Python Algorithm
2-
用Python实现各种算法
1+
# Python Algorithm and Data Structure
2+
3+
Python 算法与数据结构
4+
35
## 数据结构
4-
- 数组(Array)
5-
- 堆栈(Stack)
6-
- 队列(Queue)
7-
- 链表(Linked List)
8-
- 树(Tree)
9-
- 图(Graph)
10-
- 堆(Heap)
11-
- 散列表(Hash)
126

13-
## 排序算法
7+
###
8+
9+
## 算法
10+
11+
### 排序算法
1412

1513
- 冒泡排序
1614
- 插入排序
@@ -21,7 +19,7 @@
2119
- 快速排序
2220
- 计数排序
2321

24-
## 查找算法
22+
### 查找算法
2523

2624
- 顺序查找
2725
- 二分查找

Diff for: data_structure/linear/linked_list.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ def find(linked_list, index):
7979
return linked_list
8080

8181

82-
linked_list = init()
83-
trave(linked_list)
84-
# delete(linked_list)
85-
insert(linked_list, 0, 1)
86-
insert(linked_list, 0, 2)
87-
insert(linked_list, 0, 3)
88-
remove(linked_list,2)
89-
trave(linked_list)
90-
print(find(linked_list,3))
82+
if __name__ == '__main__':
83+
linked_list = init()
84+
trave(linked_list)
85+
# delete(linked_list)
86+
insert(linked_list, 0, 1)
87+
insert(linked_list, 0, 2)
88+
insert(linked_list, 0, 3)
89+
remove(linked_list, 2)
90+
trave(linked_list)
91+
print(find(linked_list, 3))

Diff for: data_structure/linear/queue.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,22 @@ def pop(queue):
4141
else:
4242
print('队列里已经没有元素了')
4343

44+
4445
def print_queue(queue):
4546
print(queue)
4647
if queue.behind is not None:
4748
print_queue(queue.behind)
4849

49-
queue = create_queue()
50-
push(queue,1)
51-
push(queue,2)
52-
push(queue,3)
53-
push(queue,4)
54-
print_queue(queue)
55-
print(pop(queue))
56-
print(pop(queue))
57-
print(pop(queue))
58-
print(pop(queue))
59-
print(pop(queue))
50+
51+
if __name__ == '__main__':
52+
queue = create_queue()
53+
push(queue, 1)
54+
push(queue, 2)
55+
push(queue, 3)
56+
push(queue, 4)
57+
print_queue(queue)
58+
print(pop(queue))
59+
print(pop(queue))
60+
print(pop(queue))
61+
print(pop(queue))
62+
print(pop(queue))

Diff for: data_structure/linear/stack.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def push(stack, ele):
2929
push_ele.before.behind = push_ele
3030

3131

32-
3332
def pop(stack):
3433
if isinstance(stack, MyStack):
3534
stack_top = top(stack)

Diff for: data_structure/tree/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from data_structure.tree.tree import Tree
2+
from data_structure.tree.binary_tree import BinaryTree
3+
from data_structure.tree.bst import BST

Diff for: data_structure/tree/binary_tree.py

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
from data_structure.tree.Tree import Tree
1+
from data_structure.tree.tree import Tree, TreeNode
2+
3+
4+
class BinaryTreeNode(TreeNode):
5+
"""
6+
一个二叉树节点
7+
"""
8+
9+
def __init__(self, value, left, right):
10+
self.left = left
11+
self.right = right
12+
super().__init__(value, [left, right])
213

314

415
class BinaryTree(Tree):
516
"""
617
二叉树:基本二叉树的数据结构
718
"""
8-
def __init__(self, key):
9-
self.key = key
10-
self._r_child = None
11-
self._l_child = None
12-
super().__init__(key=key, children=[self._l_child, self._r_child])
13-
14-
@property
15-
def l_child(self):
16-
return self._l_child
17-
18-
@l_child.setter
19-
def l_child(self, l_child):
20-
if not isinstance(l_child, BinaryTree): return
21-
self._l_child = l_child
22-
self.children = [self.l_child, self.r_child]
23-
24-
@property
25-
def r_child(self):
26-
return self._r_child
27-
28-
@r_child.setter
29-
def r_child(self, r_child):
30-
if not isinstance(r_child, BinaryTree): return
31-
self._r_child = r_child
32-
self.children = [self.l_child, self.r_child]
19+
20+
def inorder_traversal_while(self):
21+
"""
22+
二叉树的中序遍历
23+
:return: list of node values
24+
"""
25+
res = []
26+
27+
if not self.root:
28+
return res
29+
30+
stack = [self.root]
31+
node = self.root.left
32+
33+
while len(stack):
34+
while node:
35+
stack.append(node)
36+
node = node.left
37+
38+
res.append(stack[-1].value)
39+
40+
return res

Diff for: data_structure/tree/bst.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22

3-
from DataStructure.tree.BinaryTree import BinaryTree
3+
from data_structure.tree.binary_tree import BinaryTree
44

55

66
# 二叉查找树

0 commit comments

Comments
 (0)