0% found this document useful (0 votes)
49 views20 pages

Binary Search Tree

Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
49 views20 pages

Binary Search Tree

Copyright
© © All Rights Reserved
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

Data Structures

Binary Search Trees


Today’s Outline
• Basic concepts of BST
• BST operations
Basic Concept’s
• A binary search tree (BST) is a binary
tree with the following properties:
– All items in the left subtree are less than
the root.
– All items in the right subtree are greater
than or equal to the root.
– Each subtree is itself a binary search tree.
BST
BST
Invalid BST
BST Operations
• Traversals:
– BST traversal includes the pre-order, in-
order and post-order traversal.
– Pre-order and post-order traversal in BST
usually do not produce useful results.
– In-order traversal however produces a
sequential list.
Traverse the BST
BST Operations
• Searches
– Find the smallest node
• The node with the smallest value is the far-left
node in the tree.
• Simply follow the left branches until you get to a
leaf.
– Find the largest node
• Start at the tree root and follow the right
branches to the far-right node in the tree.
BST Operations
• Searches
• Find a requested node
(BST search):
– First compare node with
the root node
– If node is less than root
then the target node
would be in the left sub-
tree, search through the
left sub-tree.
– If it is greater then search
the right sub-tree
BST Operations
• Insertion
– The insert node function adds data to a BST.
– To insert data all you need to do is to follow the
branches to an empty subtree and then insert the
new node.
– In other words, all inserts take place at a leaf or at
a leaflike node—a node that has only one null
subtree.
– If the BST is empty the new node becomes the
root.
BST Operations
• Insertion: Non-empty BST
BST Operation
• Insertion: Empty BST
– Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 14, 23, 7, 10, 33, 56, 80, 66, 70
• 7, 10, 14, 23, 33, 56, 66, 70, 80
Insertion
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
11

6 19

4 43
8 17

5 31
10
Insertion
• 14, 23, 7, 10, 33, 56, 80, 66, 70
14

23
7

33
10
56

80

66

70
BST Operation
• Deletion
– To delete a node from a binary search tree, we
must first locate it.
– There are four possible cases when we delete a
node:
• The node to be deleted has no children. In this case, all
we need to do is delete the node.
• The node to be deleted has only a right subtree. We
delete the node and attach the right subtree to the
deleted node’s parent.
• The node to be deleted has only a left subtree. We
delete the node and attach the left subtree to the deleted
node’s parent.
BST Operation
• Deletion
– The node to be deleted has two subtrees.
• Find the largest node in the deleted node’s left
subtree and move its data to replace the
deleted node’s data.
• Or find the smallest node on the deleted node’s
right subtree and move its data to replace the
deleted node’s data.
BST Operation
• Deletion: Example
deletion

• Delete node 8
• Delete11
• Delete 31 11
17

6 19

4 43
8
10 17

5 31
10
End of Lecture

You might also like