BINARY SEARCH TREE.pptx all about trees how hey grow
1. BINARY SEARCH TREE
( USING ARRAY AND LINKED LIST)
REPRESENTED BY :-
AYESHA NADEEM
MAHNOOR JABBAR
UNAIZA MATLOOB
TEHREEM IQBAL
2. BINARY TREE :-
A data structure where each node has at most two children…
Array Representation of Binary tree:-
• Start from root index..
• Filling array level by level..
• From left to right..
3. INDEX MAPPING:-
• The root is at index is ‘0’.
• For a node at index ‘I’.
a.Left child at index ( 2i +1)
b.Right child at index (2i+2)
c.Parent is at index (i- ½)
4. BINARY TREES USING LINKED LIST
• REPRESENTATION OF BINARY TREE IN MEMORY :
BINARY TREE WILL MAINTAIN IN MEMORY BY LINKED REPRESENTATION USING
THREE PARALLEL ARRAYS..
1. INFO (CONTAIN DATA OF NODE )
2. LEFT (CONTAIN LOCATION OF LEFT SIDE)
3. RIGHT (CONTAIN LOCATION OF RIGHT SIDE)
4. ROOT (CONTAIN LOCATION OF ROOT ELEMENT )
LEFT INFO RIGHT
6. INSERTION IN BINARY SEARCH TREE :-
Definition:-
“ Insertion in a binary search tree is the process of adding a new node to
the tree while maintaining the BST property..”
• A new key is always inserted at the leaf by maintaining the property of the
binary search tree . We start searching for a key from the root until we hit a
leaf node . Once a leaf node is identified , the new node is added as a child of
the leaf node.
• Types :
i. Inserting a leaf node : New nodes are inserted as leaf nodes…
ii. Inserting an internal node : A new node can be inserted as an internal node..
7. STEPS FOR INSERTING A NODE :-
i. Initialize a current node with the root of the tree..
ii. Compare the value to be inserted with the node’ s element..
iii. If the new element is less than current element, the current node becomes the left child of
the parent and continue recursively to find the parent node for the new element ..
iv. If the new element is greater than the current element , the current node becomes the right
child of the parent and continue recursively ..
v. Insert the value at the leaf node or null position..
8
3
10
14
13
1 6
4 7
9. DELETION IN BINARY SEARCH TREE:-
Definintion :
“ In a binary search tree , deletion involves removing a node while maintaining the BST
properties.. ”
CASES :-
• Node to be deleted has no children.
• Node to be deleted has one children.
• Node to be deleted has two children.
Types:-
In a Binary Search Tree (BST), there are three types of deletion:
1. Deletion of a leaf node: Removing a node with no children.
2. Deletion of a node with one child: Removing a node with either a left or right child.
3. Deletion of a node with two children: Removing a node with both left and right children.
10. STEPS FOR DELETING ANY NODE:-
Here are the steps for deletion in a Binary Search Tree (BST):
1. Search for the node to be deleted: Find the node with the given key.
2. Check the number of children: Determine if the node has zero, one, or two children.
3. Delete the node based on the number of children: - Zero children: Remove the node. -
One child: Replace the node with its child. - Two children: Find the in-order successor or
predecessor, and replace the node with it.
4. Rebalance the tree (if necessary): Ensure the BST properties are maintained after deletion.