You are on page 1of 2

DATA STRUCTURES AND ALGORITHMS

Assignment 2

Syed Muhammad Arsalan Jah


BSE 3B Enroll: 02-131212-078
a) Design an algorithm which gives the preorder file format of a
binary tree given a pointer to the root of a binary tree. Consider the
binary tree has nodes with fields "data", "left_child", and
"right_child".
{
Step 1: Start.
Step 2: Declare a String variable Data.
Step 3: Create a method PreOrder(node pointer).
Step 4: Call method PreOrder(node pointer).
Step 5: If pointer.Data is equal to null then store “.” In Data,
Else if pointer.Data in not equal to null then store pointer.Data in Data.
Step 6: Call method PreOrder(pointer.Left_Child) using recursion.
Step 7: Call method PreOrder(pointer.Right_Child) using recursion.
Step 8: End.
}

b) Design an algorithm which takes a preorder file format of a binary


tree and produces the binary tree. Use recursive approach.
{
STEP 1: Start.
STEP 2: Create a Node class that has a variable data.
STEP 3: Create Node left, right and initialize them with null.
STEP 4: Create a BinaryTree class and create a Node root an/d initialize with
null.
STEP 5: Create a function printPreorder(Node node).
STEP 6: IF node = null THEN print “.” and return.
STEP 7: Print the data containing in the node.
STEP 8: Call function printPreorder(node.left) using recursion.
STEP 9: Call function printPreorder(node.right) using recursion.
STEP 10: Create a function printPreorder() and has printPreorder(root) in the
body.
STEP 11: End.
}

You might also like