Skip to content

Commit 11d0d64

Browse files
author
Shivam Arora
committed
Binary graph algorithms to find height of binary tree and to check whether the given binary tree is full binary or not
1 parent 1a5df6b commit 11d0d64

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

binary_tree/basic_binary_tree.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.left = None
5+
self.right = None
6+
7+
8+
def depth_of_tree(tree):
9+
if tree is None:
10+
return 0
11+
else:
12+
depth_l_tree = depth_of_tree(tree.left)
13+
depth_r_tree = depth_of_tree(tree.right)
14+
if depth_l_tree > depth_r_tree:
15+
return 1 + depth_l_tree
16+
else:
17+
return 1 + depth_r_tree
18+
19+
20+
def is_full_binary_tree(tree):
21+
if tree is None:
22+
return True
23+
if (tree.left is None) and (tree.right is None):
24+
return True
25+
if (tree.left is not None) and (tree.right is not None):
26+
return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right))
27+
else:
28+
return False
29+
30+
31+
def main():
32+
tree = Node(1)
33+
tree.left = Node(2)
34+
tree.right = Node(3)
35+
tree.left.left = Node(4)
36+
tree.left.right = Node(5)
37+
tree.left.right.left = Node(6)
38+
tree.right.left = Node(7)
39+
tree.right.left.left = Node(8)
40+
tree.right.left.left.right = Node(9)
41+
42+
print(is_full_binary_tree(tree))
43+
print(depth_of_tree(tree))
44+
45+
46+
if __name__ == '__main__':
47+
main()

0 commit comments

Comments
 (0)