We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Binary search tree
Binary Search tree
• Search tree: A tree data structure used for locating specific keys from within a set.
• For locating the keys, data in nodes must be in some order.
• Order of nodes: – Nodes in LEFT sub-tree must be smaller than ROOT – Nodes in RIGHT sub-tree must be larger than ROOT
• Binary search tree:
A search tree where each node can have maximum two children Binary Search tree • Advantages: Efficient search time given the tree is reasonably balanced. Leaves at both ends have reasonably similar depth
Reasonably balanced BST Unbalanced BST
Binary Search tree Representation: • A node in a Binary Search Tree is represented using – Key value – Pointer to LEFT sub-tree – Pointer to RIGHT sub-tree
• Creation of BST from an array of elements:
– If ROOT is NULL, Insert element at ROOT – Otherwise, compare element with ROOT node. – If ROOT is greater recurse on LEFT sub-tree – If ROOT is smaller recurse on RIGHT sub-tree – Finally insert element at the appropriate LEAF location that satisfies property of BST Binary Search tree A= 43, 10, 79, 90, 12, 54, 11, 9, 50 Binary Search tree A= 43, 10, 79, 90, 12, 54, 11, 9, 50 Binary Search tree A= 43, 10, 79, 90, 12, 54, 11, 9, 50
Inorder: 9, 10, 11, 12, 43, 50, 54, 79, 90
Binary Search tree Deletion in a Binary Search Tree (3 cases)