Professional Documents
Culture Documents
Bachelor of Engineering Subject Code: 3130702 Semester - III Subject Name: Data Structures
Bachelor of Engineering Subject Code: 3130702 Semester - III Subject Name: Data Structures
Bachelor of Engineering Subject Code: 3130702 Semester - III Subject Name: Data Structures
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.