You are on page 1of 2

Implementation of Binary Trees

A Data Structures Project

Introduction
A binary tree is a data structure in which a node has no more than 2 offspring, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Any node in the data structure can be reached by starting at root node and repeatedly following references to either the left or right child. In this project we shall be implementing a simple example of a binary tree

Traversal in a Binary tree


There are 3 modes of traversing through a binary tree and all 3 of them have been implemented in our code.

1.Preorder traversal : The node is visited first. Next we move to the left subnode and finally the right subnode. 2.Inorder traversal : The Left subnode is visited first,followed by the the vertex and finally the right subnode. 3.Postorder traversal : We visit the left and right and finally reach the vertex. subnodes

Approach Explained
We are going to implement a Binary search tree using the above concepts. A Binary Search tree is nothing more than an ordered Binary tree. As a result, all algorithms can be easily implemented using inorder traversal. Working of Code is as follows : We create the root node in main. Next we accept a choice from the user to perform one of 5 options ie. 1.insertion 2.Preorder traversal 3.Inorder traversal 4.Postorder traversal 5.Display elements. The Insert function accepts the pointer to the tree and input as parameters. Then depending upon the info value we sort the new node into its correct position in the tree so as to keep the tree ordered. The display function performs inorder traversal and displays each element in the tree.

You might also like