-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.py
More file actions
64 lines (55 loc) · 1.39 KB
/
Tree.py
File metadata and controls
64 lines (55 loc) · 1.39 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
60
61
62
63
64
"""
@created_by: Diego Quiroz
diego00aq@gmail.com
@created: 05-21-2020
@modified: 05-21-2020
"""
class Node:
"""Constructor of Node Class, the main component of a tree.
:param val: any type of data to store inside the node
:var left: reference to Nodes left child
:var right: reference to Nodes right child
"""
def __init__(self, val):
self.left = None
self.right = None
self.val = val
"""Function to insert values as child of a node.
:param root: Node instance to take as the root.
:param node: Node instance to take as the node to evaluate.
:return: if value is already in the tree, return None
"""
def insert(root, node):
if root.val == node.val:
return None
if root is None:
root = node
else:
if root.val < node.val:
if root.right is None:
root.right = node
else:
insert(root.right, node)
else:
if root.left is None:
root.left = node
else:
insert(root.left, node)
"""Function to get the Tree as inorder traversal.
:param root: root Node instance.
"""
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
"""Function to search a node value.
:param root: root Node instance.
:param
"""
def search(root, value):
if root is None or root.val == value:
return root
if root.val < value:
return search(root.right,value)
return search(root.left,value)