Skip to content

KITSCTCLab/exercise-10-binary-tree-traversal-URK21CS3021

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Problem

Given an array of integers, construct binary tree and print inorder, preorder and postorder traversals.

Implement the following functions:

insert(root, new_value) -> BinaryTreeNode:

  • If binary search tree is empty, make a new node, declare it as root and return the root.
  • If tree is not empty and if new_value is less than value of data in root, add it to left subtree and proceed recursively.
  • If tree is not empty and if new_value is >= value of data in root, add it to right subtree and proceed recursively.
  • Finally, return the root.

inorder(root) -> None :

  • Print inorder traversal.

preorder(root) -> None :

  • Print preorder traversal.

postorder(root) -> None :

  • Print postorder traversal.

Constraints

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= data <= 100

Sample Input - 1

15, -10, 25, -6, 14, -20, 60

Sample Output - 1

-20 -10 -6 14 15 25 60 
15 -10 -20 -6 14 25 60 
-20 14 -6 -10 60 25 15

Explanation
Construct the binary tree using the given input.
First line of output represents inorder traversal.
Second line of output represents preorder traversal.
Third line of output represents postorder traversal.

Sample Input - 2

15, 10, 25, 6, 14, 20, 60

Sample Output - 2

6 10 14 15 20 25 60 
15 10 6 14 25 20 60 
6 14 10 20 60 25 15

About

exercise-10-binary-tree-traversal-URK21CS3021 created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages