-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_structure.py
170 lines (126 loc) · 3.52 KB
/
data_structure.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
# -*- coding:UTF-8 -*-
# 队列和戰
class Queue(object):
# class Stack(object):
def __init__(self):
self.data_list=[]
def insert(self,data):
self.data_list.append(data)
def pop(self):
if len(self.data_list)==0:
return None
data=self.data_list[0]
# data=self.data_list[-1]
# del self.data_list[-1]
del self.data_list[0]
return data
def size(self):
return len(self.data_list)
# queue = Queue()
# stack = Stack()
# print stack.size()
# stack.insert(1)
# stack.insert(2)
# stack.insert(3)
# head = stack.pop()
# print head
# head = stack.pop()
# print head
# head = stack.pop()
# print head
# head = stack.pop()
# print head
# 二叉树
class Node(object):
def __init__(self,index):
self.index=index
self.left_child=None
self.right_child=None
class BinaryTree(object):
def __init__(self,root):
self.root=root
def pre_travel(self,node):
if not node:
return
print node.index
self.pre_travel(node.left_child)
self.pre_travel(node.right_child)
def cen_travel(self,node):
if not node:
return
self.cen_travel(node.left_child)
print node.index
self.cen_travel(node.right_child)
def next_travel(self,node):
if not node:
return
self.next_travel(node.left_child)
self.next_travel(node.right_child)
print node.index
node_dict={}
for i in range(1,12):
node_dict[i]=Node(i)
node_dict[1].left_child = node_dict[2]
node_dict[1].right_child = node_dict[3]
node_dict[2].left_child = node_dict[5]
node_dict[2].right_child = node_dict[6]
node_dict[6].left_child = node_dict[10]
node_dict[6].right_child = node_dict[11]
node_dict[3].left_child = node_dict[7]
node_dict[7].left_child = node_dict[8]
node_dict[7].right_child = node_dict[9]
tree=BinaryTree(node_dict[1])
# tree.pre_travel(tree.root)
# tree.cen_travel(tree.root)
# tree.next_travel(tree.root)
# 二分查找
def binary_search(search_list,target):
left=0
right=len(search_list)-1
while left <= right:
mid=(right+left)/2
if search_list[mid] < target:
left = mid + 1
continue
if search_list[mid] == target:
return mid
if search_list[mid] > target:
right=mid-1
return None
search_list=[1,4,7,8,9]
# print binary_search(search_list,8)
# 插入排序
def insert_sort(origin_list):
sorted_list=[]
for i in range(0,len(origin_list)):
if len(sorted_list) ==0:
sorted_list.append(origin_list[i])
continue
for j in range(len(sorted_list)-1,-1,-1):
if sorted_list[j]<=origin_list[i]:
sorted_list.insert(j+1,origin_list[i])
break
if j==0:
sorted_list.insert(0,origin_list[i])
origin_list[:]=sorted_list[:]
origin_list=[2,4,1,6,3]
# insert_sort(origin_list)
# print origin_list
# 冒泡排序
def bubble_sort(origin_list):
for i in range(len(origin_list),0,-1):
for j in range(0,i-1):
if origin_list[j] > origin_list[j+1]:
origin_list[j],origin_list[j+1] =origin_list[j+1],origin_list[j]
origin_list=[5,4,2,1,9,6]
bubble_sort(origin_list)
# print origin_list
def qucksort(a):
if len(a)<=1:
return a
l=[x for x in a[1:] if x <= a[0]]
r=[x for x in a[1:] if x >a[0]]
return qucksort(l)+[a[0]]+qucksort(r)
a=[20,10,30,70,50]
# print qucksort(a)