Skip to content

Commit 4e9e072

Browse files
authored
Add files via upload
1 parent 28577da commit 4e9e072

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Binary tree do not store data in sorted manner, so to find an element we cant decide left or right subtree, we need to traverse whole tree,
3+
also as we do not know if the element is in left or right subtree, we cant store values on our way from root to node,
4+
we need to store only once it has been found.
5+
6+
eg : 1
7+
/ \
8+
2 3
9+
/ \
10+
4 5
11+
12+
path to 5 : 1 -> 2 -> 5
13+
14+
We are using recursive approach to traverse the tree and find the element and store the path on our way back.
15+
Time complexity: O(n) in worst case, where n is the number of nodes in the binary tree.
16+
17+
"""
18+
19+
class Node :
20+
"""Class to define nodes of the tree with data, left ptr, and right ptr as attributes"""
21+
def __init__(self, value):
22+
self.data = value
23+
self.left = None
24+
self.right = None
25+
26+
def getPath (node, n, path):
27+
"""The function returns the path from root node as a list, along with a bool ans if the node exists in tree or not
28+
input : current root node of subtree, element to be found, path storing list
29+
returns : ans boolean - True if it is in the tree and False if it doesn't exist
30+
path - list of nodes that occur on the path from node to the root
31+
"""
32+
if node == None:
33+
return False, path
34+
35+
if node.data == n: # if the element has been found, we return true and also add the element to the path
36+
path.append(node.data)
37+
return True, path
38+
39+
left, lpath = getPath(node.left, n, path) # element not yet found so look for it in left subtree
40+
right, rpath = getPath(node.right, n, path) # element not yet found so look for it in right subtree
41+
42+
if left == True: # element is in left subtree, so return true and add all the elements on our way back
43+
path.append(node.data)
44+
return True, lpath
45+
elif right == True: # element is in right subtree, so return true and add all the elements on our way back
46+
path.append(node.data)
47+
return True, rpath
48+
49+
if not (left and right): # element is in neither sub trees, so it does not exist in tree. Return false and an empty list
50+
return False, []
51+
52+
53+
54+
55+
56+
# ========================= DRIVER CODE =======================
57+
58+
if __name__ == "__main__":
59+
60+
# creating the tree
61+
root = Node(1)
62+
root.left = Node(2)
63+
root.right = Node(3)
64+
root.left.left = Node(4)
65+
root.left.right = Node(5)
66+
root.right.left = Node(6)
67+
root.right.right = Node(7)
68+
root.left.left.left = Node(8)
69+
70+
n = 8
71+
path = []
72+
ans, path = getPath(root, n, path)
73+
74+
if ans:
75+
print(f"Path to {n} : ", path[::-1]) # printing the path in reverse order, as the the returned value contains path from node to root
76+
else:
77+
print(f"{n} does not exist in tree") # printing a message if the element is not in the tree

0 commit comments

Comments
 (0)