-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeproblem.py
More file actions
59 lines (44 loc) · 1.35 KB
/
treeproblem.py
File metadata and controls
59 lines (44 loc) · 1.35 KB
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
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
# order the tree
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
# recursivly print the whole tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print( self.data),
if self.right:
self.right.PrintTree()
def find(self, data):
# you can search for a node in a tree by comparing the data in each node
# to the data you are searching for.
# Lay it out like the insert method using if statements
# YOUR CODE HERE
return None
# Use the insert method to add nodes
root = Node(25)
root.insert(16)
root.insert(4)
root.insert(17)
root.insert(37)
root.insert(3)
# test the find method
print(root.find(3).data) # should print 3
print(root.find(4).data) # should print 4
print(root.find(17).data) # should print 17