# Python Program to Print Boundary Traversal of Binary Tree

«
»

This is a Python program to print the border of a binary tree in anticlockwise direction.

Problem Description

The program creates a binary tree and prints its border in anticlockwise direction.

Problem Solution

1. Create a class BinaryTree with instance variables key, left and right.
2. Define methods set_root, insert_left, insert_right, inorder, search, print_left_boundary, print_right_boundary, print_leaves and print_border.
3. The method set_root takes a key as argument and sets the variable key equal to it.
4. The methods insert_left and insert_right insert a node as the left and right child respectively.
5. The method inorder displays the inorder traversal.
6. The method search returns a node with a specified key.
7. The method print_left_boundary prints the left border of the binary tree except the last leaf node.
8. The method print_right_boundary prints the right border of the binary tree in reverse except the last leaf node.
9. The method print_leaves prints the leaf nodes of the binary tree from left to right.
10. The method print_border prints the border of the binary tree by calling the above methods.

Program/Source Code

Here is the source code of a Python program to print the border of a binary tree in anticlockwise direction. The program output is shown below.

```class BinaryTree:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None

def set_root(self, key):
self.key = key

def inorder(self):
if self.left is not None:
self.left.inorder()
print(self.key, end=' ')
if self.right is not None:
self.right.inorder()

def insert_left(self, new_node):
self.left = new_node

def insert_right(self, new_node):
self.right = new_node

def search(self, key):
if self.key == key:
return self
if self.left is not None:
temp =  self.left.search(key)
if temp is not None:
return temp
if self.right is not None:
temp =  self.right.search(key)
return temp
return None

def print_left_boundary(self):
current = self
while True:
if current.left is not None:
print(current.key, end=' ')
current = current.left
elif current.right is not None:
print(current.key, end=' ')
current = current.right
else:
break

def print_right_boundary(self):
if self.right is not None:
self.right.print_right_boundary()
print(self.key, end=' ')
elif self.left is not None:
self.left.print_right_boundary()
print(self.key, end=' ')

def print_leaves(self):
if self.left is not None:
self.left.print_leaves()
if self.right is not None:
self.right.print_leaves()
if (self.left is None
and self.right is None):
print(self.key, end=' ')

def print_border(self):
print(self.key, end=' ')
if self.left is not None:
self.left.print_left_boundary()
self.left.print_leaves()
if self.right is not None:
self.right.print_leaves()
self.right.print_right_boundary()

btree = None

print('Menu (this assumes no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('border')
print('quit')

while True:
do = input('What would you like to do? ').split()

operation = do.strip().lower()
if operation == 'insert':
data = int(do)
new_node = BinaryTree(data)
suboperation = do.strip().lower()
if suboperation == 'at':
btree = new_node
else:
position = do.strip().lower()
key = int(position)
ref_node = None
if btree is not None:
ref_node = btree.search(key)
if ref_node is None:
print('No such key.')
continue
if suboperation == 'left':
ref_node.insert_left(new_node)
elif suboperation == 'right':
ref_node.insert_right(new_node)

elif operation == 'border':
if btree is not None:
print('Border of tree: ')
btree.print_border()
print()

elif operation == 'quit':
break```
Program Explanation

1. A variable is created to store the binary tree.
2. The user is presented with a menu to perform operations on the tree.
3. The corresponding methods are called to perform each operation.
4. The method print_border is called on the binary tree to print its border.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
```Case 1:
Menu (this assumes no duplicate keys)
insert <data> at root
insert <data> left of <data>
insert <data> right of <data>
border
quit
What would you like to do? insert 1 at root
What would you like to do? insert 2 left of 1
What would you like to do? insert 3 right of 1
What would you like to do? border
Border of tree:
1 2 3
What would you like to do? insert 4 left of 2
What would you like to do? insert 5 right of 2
What would you like to do? insert 6 left of 3
What would you like to do? insert 7 right of 3
What would you like to do? border
Border of tree:
1 2 4 5 6 7 3
What would you like to do? insert 8 left of 4
What would you like to do? insert 9 right of 4
What would you like to do? insert 10 left of 5
What would you like to do? insert 11 right of 5
What would you like to do? insert 12 left of 6
What would you like to do? insert 13 right of 6
What would you like to do? insert 14 left of 7
What would you like to do? insert 15 right of 7
What would you like to do? border
Border of tree:
1 2 4 8 9 10 11 12 13 14 15 7 3
What would you like to do? quit

Case 2:
Menu (this assumes no duplicate keys)
insert <data> at root
insert <data> left of <data>
insert <data> right of <data>
border
quit
What would you like to do? insert 1 at root
What would you like to do? insert 2 left of 1
What would you like to do? insert 3 left of 2
What would you like to do? insert 4 right of 2
What would you like to do? insert 5 left of 4
What would you like to do? insert 6 right of 4
What would you like to do? insert 7 right of 1
What would you like to do? insert 8 right of 7
What would you like to do? border
Border of tree:
1 2 3 5 6 8 7
What would you like to do? quit```

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions. 