Skip to content

Added node count and leaf count #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions traverals/binary_tree_traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def __init__(self, data):
self.right = None
self.left = None


def build_tree():
print("\n********Press N to stop entering at any point of time********\n")
print("Enter the value of the root node: ", end="")
Expand Down Expand Up @@ -43,19 +42,21 @@ def build_tree():


def pre_order(node):
no_of_nodes=0
if not isinstance(node, TreeNode) or not node:
return
return
print(node.data, end=" ")
pre_order(node.left)
pre_order(node.right)
return 1+pre_order(node.left)#counting number of left nodes
return 1+pre_order(node.right)#counting number of right nodes


def in_order(node):
leaf_count=0
if not isinstance(node, TreeNode) or not node:
return
return
in_order(node.left)
print(node.data, end=" ")
in_order(node.right)
return 1+in_order(node.right)#counting number of leaf nodes


def post_order(node):
Expand Down Expand Up @@ -90,14 +91,13 @@ def level_order(node):
input_function = raw_input
else:
input_function = input

node = build_tree()
print("\n********* Pre Order Traversal ************")
pre_order(node)
no_of_nodes=pre_order(node)
print("\n******************************************\n")

print("\n********* In Order Traversal ************")
in_order(node)
leaf_count=in_order(node)
print("\n******************************************\n")

print("\n********* Post Order Traversal ************")
Expand All @@ -107,3 +107,6 @@ def level_order(node):
print("\n********* Level Order Traversal ************")
level_order(node)
print("\n******************************************\n")
print("\nTotal number of nodes : ",no_of_nodes)
print("\nTotal number of leafs : ",leaf_count)