Data Structure Unit-4 Trees
Data Structure Unit-4 Trees
Data Structure Unit-4 Trees
• If T contains a root R,
– T1 is called the left successor of R.
– T2 is called the right successor of R.
• Binary trees T and T’ are said to be similar if they have the same structure
(or same shape).
• The trees are said to be copies if they are similar and having the same
contents at the corresponding nodes.
Example
Terminology
• Suppose N is a node in T with left successor S 1 and right successor
S2.
– N is called the parent (or father) of S1 and S2.
– S1 is the left child (or son) of N.
– S2 is the right child (or son) of N.
– S1 and S2 are said to be siblings (or brothers).
• Nodes with same level number are said to belongs to same generation.
• All the nodes at the last level should appear as far left
as possible.
• The nodes is above fig. are labeled by integers from left to
right, generation by generation.
• If tree T is empty,
– ROOT will contain Null value.
Linked Representation of Tree
Sequential representation of Binary Trees
• Preorder Traversal
– ABDECF
• In-order Traversal
– DBEACF
• Post-order Traversal
– DEBFCA
Example
Preorder : A B D E F C G H J L K
Inorder : D B F E A G C L J H K
Postorder : D F E B G L J K H C A
Pre-order : * + a – b c / - d e - + f g h
Post-order : a b c - + d e – f g + h - / *
Preorder Traversal
Algorithm: Preorder
In-order Traversal
Algorithm: In-order
Post-order Traversal
Algorithm: Post-order
Binary Search Tree
T is called a binary search tree if each node N has the
following property:
– The value at N is greater than every value in the left subtree
of N and is less than every value in the right subtree of N.
– According to this property, in-order traversal of T will be a
sorted list of elements.
Searching & Inserting in BST
• To find the location of ITEM in binary search tree T or to
insert ITEM as a new node in its appropriate place: