Bachelor of Engineering Subject Code: 3130702 Semester - III Subject Name: Data Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 70

Bachelor of Engineering

Subject Code: 3130702


Semester – III
Subject Name: Data Structures
NONLINEAR DATA STRUCTURE :
Tree-Definitions and Concepts, Representation of binary tree, Binary tree traversal
(Inorder, postorder, preorder), Threaded binary tree, Binary search trees,
Conversion of General Trees To Binary Trees, Applications Of TreesSome balanced
tree mechanism, eg. AVL trees, 2-3 trees, Height Balanced, Weight Balance,
Graph-Matrix Representation Of Graphs, Elementary Graph operations,(Breadth
First Search, Depth First Search, Spanning Trees, Shortest path, Minimal spanning
tree )
Tree
Convert a Generic Tree(N-array Tree) to Binary Tree

Steps:
1. The root of the Binary Tree is the Root of the Generic Tree.
2. Find Root to Leftmost Child.
3. Connect Sibling to each Node L to R.
4. Delete Other Links.
 
1. Find Root to Leftmost Child
2. Connect Sibling to each Node L to R.
3. Delete Other Links.
4. Draw Left and Right Links and prepare Binary
Tree
Example: convert general tree to binary tree
Binary tree traversal (Inorder, postorder,
preorder)
Algorithm of RPREORDER(T)
T is root Node(pointer variable)
Root Left Right
Algorithm of RINORDER(T)
T is root Node(pointer variable)
Left Root Right
Algorithm of RPOSTORDER(T)
T is root Node(pointer variable)
Left Right Root
Construct a Binary Tree from given Inorder and Preorder traversals

• Preorder:
1,2,4,8,9,10,11,5,3,6,7(Root,Left,Right)
• Inorder:
8,4,10,9,11,2,5,1,6,3,7 (Left,Root,Right)
• First, choose the Root(Scan from Left to right,
which member comes first) from Preorder,
now check the root in Inorder and find the
left and right elements of that root.
• Do the above steps recursively.
Construct a Binary Tree from given Inorder and Postorder traversals

• Postorder:
9,1,2,12,7,5,3,11,4,8(Left,Right,Root)
• Inorder:
9,5,1,7,2,12,8,4,3,11 (Left,Root,Right)
• First, choose the Root(Scan from right to Left,
which member comes first) from Postorder,
now check the root in Inorder and find the
left and right elements of that root.
• Do the above steps recursively.
Binary Tree from Preorder and Post Order
Binary Search Tree
Binary Search Tree
• Basic Operations
• Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order
manner.
• In-order Traversal − Traverses a tree in an in-order
manner.
• Post-order Traversal − Traverses a tree in a post-order
manner.
Binary Search Tree
Illustration to search 6 in below tree: 
1. Start from the root. 
2. Compare the searching element with root, if less than root, then recurse for left, else
recurse for right. 
3. If the element to search is found anywhere, return true, else return false. 
 

Search 6 in this Binary Search Tree


Binary Search Tree - Insertion
Binary Search Tree – deletion
Three Possibilities arise (Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of the input
node. )
Convert Binary tree to Binary Search Tree
Convert Binary tree to BST
Convert Binary tree to BST
(step 1: Sort the inorder array)
(step 2: Replace the binary tree values with
the inorder array values)
(step 3: Replace the binary tree values with
the Inorder array values)
Complexity of BST
• Time complexity is nlogn because of the
sorting.
• Space complexity is n because of the extra
space we are using for the inorder array.
Threaded Binary Tree
• We know that the binary tree nodes may have at most two children. But if they have only
one children, or no children, the link part in the linked list representation remains null.
Using threaded binary tree representation, we can reuse that empty links by making
some threads.
• If one node has some vacant left or right child area, that will be used as thread. There are
two types of threaded binary tree. The single threaded tree or fully threaded binary tree. In
single threaded mode, there are another two variations. Left threaded and right threaded.
• In the left threaded mode if some node has no left child, then the left pointer will point to
its Inorder predecessor, similarly in the right threaded mode if some node has no right
child, then the right pointer will point to its Inorder successor. In both cases, if no successor
or predecessor is present, then it will point to header node.
• For fully threaded binary tree, each node has five fields. Three fields like normal binary tree
node, another two fields to store Boolean value to denote whether link of that side is actual
link or thread.
Left Threaded Tree
Right Threaded Tree
Fully Threaded Tree
Threaded Binary Tree
Threaded Binary Tree

You might also like