-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-binary-tree.py
106 lines (103 loc) · 3.07 KB
/
basic-binary-tree.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
class Node:
def __init__(self, val):
self.value = val
self.left = None
self.right = None
def insert(self, data):
#if data already exists - DO NOT insert
#if data at root greater than data, insert at left
#if data at root smaller than data, insert at right
if self.value == data:
return False
elif self.value > data:
if self.left:
return self.left.insert(data)
else:
self.left = Node(data)
return True
else:
if self.right:
return self.right.insert(data)
else:
self.right = Node(data)
return True
def delete(self, data):
#CASE 1 : if the node has no children, kill it!
#CASE 2 : if the node has one child/sub-tree, make the parent point to that child/sub-tree
#CASE 3 : if the node has two children, find max in left sub-tree OR find min in right sub-tree
def find(self, data):
if self.value == data:
return True
elif self.value > data:
if self.left:
return self.left.find(data)
else:
return False
else:
if self.right:
return self.right.find(data)
else:
return False
def preOrder(self):
if self:
print self.value
if self.left:
self.left.preOrder()
if self.right:
self.right.preOrder()
def inOrder(self):
if self:
if self.left:
self.left.inOrder()
print self.value
if self.right:
self.right.inOrder()
def postOrder(self):
if self:
if self.left:
self.left.postOrder()
if self.right:
self.right.postOrder()
print self.value
class Tree:
def __init__(self):
self.root = None
def insert(self,data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preOrder(self):
if self.root:
print "PRE-ORDER"
self.root.preOrder()
def inOrder(self):
if self.root:
print "IN-ORDER"
self.root.inOrder()
def postOrder(self):
if self.root:
print "POST-ORDER"
self.root.postOrder()
obj = Tree()
print "INSERTION:", obj.insert(7)
print "INSERTION:", obj.insert(2)
print "INSERTION:", obj.insert(6)
print "INSERTION:", obj.insert(4)
print "INSERTION:", obj.insert(5)
print "INSERTION:", obj.insert(1)
print "INSERTION:", obj.insert(9)
print "INSERTION:", obj.insert(10)
print "INSERTION:", obj.insert(8)
print "INSERTION:", obj.insert(3)
obj.preOrder()
obj.postOrder()
obj.inOrder()
print "Element found:", obj.find(28)
print "Element found:", obj.find(20)