0% found this document useful (0 votes)
12 views

Week 10 Binary Trees (1)

binarry trees used in dsa

Uploaded by

Shahzeb Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 10 Binary Trees (1)

binarry trees used in dsa

Uploaded by

Shahzeb Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Binary Search Trees

CSC-211 Data Structures and Algorithms

Slides prepared by: Dr. Omar Ahmad


Lecture outline
• The Tree Data Structure
• Binary Trees and Terminology
• Binary Search Trees
• Tree Traversal
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
• Searching for a node in a Binary Search Tree
• Inserting a node into a Binary Search Tree
• Deleting nodes from a Binary Search Tree
2
The Tree Data Structure

3
Binary Trees and Terminology

4
Types of Binary Trees

The following are common types of Binary Trees.


• Full Binary Tree
• Complete Binary Tree
• Perfect Binary Tree
• Balanced Binary Tree
• A Degenerate Tree

5
Types of Binary Trees

6
Types of Binary Trees

7
Types of Binary Trees

8
Types of Binary Trees

9
Types of Binary Trees

10
Types of Binary Trees
Summary of common types of Binary Trees.
• Full Binary Tree: A Binary Tree is a full binary tree if every node has 0
or 2 children.
• Complete Binary Tree: A Binary Tree is a complete binary tree if all
the levels are completely filled except possibly the last level and the last
level has all keys as left as possible.
• Perfect Binary Tree: A Binary tree is perfect if all its internal nodes
have two children and all leaf nodes are at the same level.
• Balanced Binary Tree: A binary tree is balanced if the height of the
tree is O(Log n) where n is the number of nodes.
• A Degenerate Tree: A Tree where every internal node has one child.
Such trees are performance-wise same as linked list.

11
Binary Search Trees (BSTs)
Binary search tree is a data structure that quickly allows us to
maintain a sorted list of numbers.
• It is called a binary tree because each tree node has a
maximum of two children.
• It is called a search tree because it can be used to search for
the presence of a number in O(log(n)) time.

The properties that separate a binary search tree from a regular


binary tree are:
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are greater than the root node
3. Both subtrees of each node are also BSTs i.e. they have the
above two properties 12
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print
their values too. As all nodes are connected via edges, we always
start from the root node. That is, we cannot randomly access a node
in a tree. There are three ways which we use to traverse a tree:
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal

13
In-Order Traversal
In this traversal method, the left subtree is visited first, then the root
and later the right sub-tree. We should always remember that every
node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted
key values in an ascending order.

14
Pre-Order Traversal
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.

15
Post-Order Traversal
In this traversal method, the root node is visited last, hence the
name. First, we traverse the left subtree, then the right subtree and
finally the root node.

16
Search for a Node in a Binary Search
Tree
Whenever an element is to be searched, start searching from the
root node. Then if the data is less than the key value, search for the
element in the left subtree. Otherwise, search for the element in the
right subtree. Follow the same algorithm for each node.

17
Insert a Node in a Binary Search
Tree
Whenever an element is to be inserted, first locate its proper
location. Start searching from the root node, then if the data is less
than the key value, search for the empty location in the left subtree
and insert the data. Otherwise, search for the empty location in the
right subtree and insert the data.

18
Delete Nodes from a Binary Search
Tree
Case 1: When the node to be deleted is a leaf node.

Steps:
If the node is a leaf node then there is no problem. Since the leaf
nodes don’t have any nodes associated with them, we can simply
free the memory and setting parent node’s left or right pointer to
null.

19
Delete Nodes from a Binary Search
Tree
Case 2: When the node to be deleted has one sub-tree (or
child node).

Steps:
This case is also fairly simple. Since every subtree is again a binary
search tree, we can set the left or right pointer of the parent node of
the node that needs to be deleted to the child node’s address.

20
Delete Nodes from a Binary Search
Tree
Case 3: When the node to be deleted has two sub-trees (or
children).
Steps:
This case is the complex case in removing a node from a binary
search tree. The rule we need to remember all the time is that even
after removing the node the tree still need to be a binary search
tree. So the following process is followed.
The deleted node is replaced by either the largest value in the left
subtree or the smallest value in the right subtree.

21
References:
1. https://learnovercoffee.com/algorithms-datastructures/trees/
2. https://en.wikipedia.org/wiki/Binary_tree
3. https://www.tutorialspoint.com/data_structures_algorithms/tree_traversal.htm
4. https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder
/?ref=lbp
5. https://www.geeksforgeeks.org/how-to-handle-duplicates-in-binary-search-tre
e/

22

You might also like