CA229 Unit 04
CA229 Unit 04
B.C.A. (Semester-3)
CA229 || Fundamentals of Data Structures and Algorithms
Definition:
Root: The topmost node of a tree is known as ROOT and it
does not have any parents.
Leaf Node: If the node does not have any child, it is known as the
Leaf Node. It is also known as the terminal node.
Sibling: If the two-child have the same parent node, then they
are called siblings.
Internal Nodes: A node is known as Internal Node, which has both
parents as well as children. It is also known as a Non-
Leaf Node.
Level of Node: The root has level zero, the child of the root has level
one, and the level of the child is one or more than that
of the parents.
Height: A tree's height is the maximum no. of nodes in a path
between root nodes and a leaf node. It is also called
DEPTH.
Basic Operations:
The basic operations that can be performed on a binary search tree data structure are the
following –
Insert − Insert an element in a tree/create atree.
Search − Searches an element in a tree.
Preorder Traversal − Traverses a tree in a pre-order manner.
Inorder Traversal − Traverses a tree in an in-order manner.
Postorder Traversal − Traverses a tree in a post-order manner.
Search Operation:
Whenever an element is to be searched, start searching from the root node, then if the
data is less than the key value, search for the element in the left sub-tree. Otherwise,
search for the element in the right sub-tree. Follow the same algorithm for each node.
Inorder: DBFEAGCLJHK
Preorder: ABDEFCGHJLK
Postorder: DFEBGLJKHCA
Example 2:
Let E denote the following algebraic expression:
[a+(b–c)]*[(d–e)/(f+g–h)]
The corresponding binary tree T appears in the below figure.
Inorder: a+b–c*d–e/f+g-h
Preorder: *+a–bc/-de-+fgh
Postorder: abc-+de–fg+h-/*
A B C D E F G H I J
To represent a binary tree of depth 'n' using array representation, we need a one-
In this example, node 50 is to be deleted, which is the tree's root node. The in-order
traversal of the tree is given below.
5, 25, 30, 50, 52, 60, 70, 75
Replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree,
which will simply be deleted.
Binary Space Partition: It is used in almost every 3D video game to determine what
objects need to be rendered.
Binary Trees: It is used in almost every high-bandwidth router for storing router-tables.
Huffman Coding Tree (Chip Uni): Used in compression algorithms, such as those used
by the .jpeg and .mp3 fileformats.
T-tree: Though most databases use some form of B-tree to store data on the drive,
databases that keep all (most) their data in memory often use T-trees to do so.
Q.3 Draw the binary tree from the following Inrder and Postorder.
Inorder n1 n2 n3 n4 n5 n6 n7 n8 n9
Postorder n1 n3 n5 n4 n2 n8 n7 n9 n6
Q.4 Draw the binary tree from the following Inorder and Preorder.
Inorder D B H E A I F J C G
Preorder A B D E H C F I J G