[go: up one dir, main page]

0% found this document useful (0 votes)
10 views5 pages

Binary Tree Traversal

The document provides a Python implementation of binary tree traversal, including functions for inserting elements and performing inorder, preorder, and postorder traversals. It defines a 'node' class and a series of functions to manipulate and display the tree structure. The program also includes a simple user interface for interacting with the binary tree through console inputs.

Uploaded by

Kasthuri L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Binary Tree Traversal

The document provides a Python implementation of binary tree traversal, including functions for inserting elements and performing inorder, preorder, and postorder traversals. It defines a 'node' class and a series of functions to manipulate and display the tree structure. The program also includes a simple user interface for interacting with the binary tree through console inputs.

Uploaded by

Kasthuri L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BINARY TREE TRAVERSAL

class node:

def __init__(self,data):

self.left=None

self.data=data

self.right=None

def insert_element(tree,n):

new_node=node(n)

if tree is None:

tree=new_node

else:

parent_ptr=None

node_ptr=tree

while node_ptr is not None:

parent_ptr=node_ptr

if n<node_ptr.data:

node_ptr=node_ptr.left

else:

node_ptr=node_ptr.right

if n<parent_ptr.data:

parent_ptr.left=new_node

else:

parent_ptr.right=new_node

return tree

def inorder(tree):
if tree is not None:

inorder(tree.left)

print(tree.data)

inorder(tree.right)

def postorder(tree):

if tree is not None:

postorder(tree.left)

postorder(tree.right)

print(tree.data)

def preorder(tree):

if tree is not None:

print(tree.data)

preorder(tree.left)

preorder(tree.right)

tree=None

print("\t\t\t BINARY TREE TRAVERSAL")

print("\t\t\t *********************")

print("1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n")

while True:

ch=int(input("Enter your choice:"))

if ch==1:

n=int(input("Enter your number:"))

tree=insert_element(tree,n)

elif ch==2:

print("IN-ORDER")
inorder(tree)

elif ch==3:

print("PRE-ORDER")

preorder(tree)

elif ch==4:

print("POST-ORDER")

postorder(tree)

elif ch==5:

break

else:

print("Invalid choice!!!")
OUTPUT:

BINARY TREE TRAVERSAL

*************************

1.Insert

2.Inorder

3.Preorder

4.Postorder

Enter your choice:1

Enter your number:4

Enter your choice:1

Enter your number:7

Enter your choice:1

Enter your number:0

Enter your choice:1

Enter your number:2

Enter your choice:1

Enter your number:5

Enter your choice:1

Enter your number:6

Enter your choice:2

IN-ORDER

5
6

Enter your choice:3

PRE-ORDER

Enter your choice:4

POST-ORDER

Enter your choice:5

You might also like