-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
123 lines (105 loc) · 3.36 KB
/
main.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
# This script creates a binary tree iterator
#
# This script is a part of the Easy Python project which creates a number
# sample python scripts to answer simple programming questions. The
# entire project is accessible at https://github.com/okany/easypython.
# Copyright (c) 2021 Okan Yilmaz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
def create_btree(alist):
bt = None
if (alist != None and alist != []):
bt = btree(alist[0])
for each in alist [1:]:
bt.add(each)
return bt
class btree():
def __init__(self, data):
self.data = data
self.left = self.right = None
self.parent = None
def add(self, data):
ret = False
if data == self.data:
# error - skip it
pass
else:
next = None
if(self.data > data):
next = self.left
else:
next = self.right
if(next == None):
node = btree(data)
node.parent = self
if(self.data > data):
self.left = node
else:
self.right = node
ret = True
else:
ret = next.add(data)
return ret
def to_list(self):
alist = []
if(self.left):
alist.append(self.left.to_list())
alist.append(self.data)
if(self.right):
alist.append(self.right.to_list())
return alist
class iter():
def __init__(self, bt):
self.bt = bt
self.cur = None
def begin(self):
if(self.bt.left):
it = iter(self.bt.left)
return(it.begin())
else:
return self.bt
def end(self):
if(self.bt.right):
it = iter(self.bt.right)
return(it.end())
else:
return self.bt
def next(self):
bt = self.cur
self.cur = None
if(bt.right):
it = iter(bt.right)
self.cur = it.begin()
else:
while (bt.parent and self.cur == None):
if(bt.parent.right != bt):
self.cur = bt.parent
else:
bt = bt.parent
return self.cur
def setcurrent(self, node):
self.cur = node
def current(self):
return self.cur
if __name__=="__main__":
alist = [1, 2, 30, 4, 60, 34, 12, -1, 5, 23, 67, 35, 4, 99, -20, -45, 89, 78]
bt = create_btree(alist)
print("list is {}".format(alist))
print("btree is {}".format(bt.to_list()))
it = iter(bt)
it.setcurrent(it.begin())
print ("it begin = {} and it end {}".format(it.current().data, it.end().data))
while(it.current() != it.end()):
print ("in next= {}".format(it.next().data))