Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Unit 5: Trees and Graphs

Binary Search Trees


Preorder, Post ORDER AND In ORDER TRAVERSING
Insertion AND DELETIONS IN Bst
B-trees
Insertions AND DELETIONS IN B-trees
Avl Trees
Graphs
REPRESENTATION OF GRAPHS
BREADTH FIRST AND DEPTH FIRST TRAVERSAL
APPLICATIONS OF GRAPHS.
Tree
Tree represents the nodes connected by edges.
Binary Tree is a special data structure used for data storage purposes.
A binary tree has a special condition that each node can have a maximum of two children.
Important Terms
Following are the important terms with respect to tree.
•Path − Path refers to the sequence of nodes along the edges of a tree.

•Root − The node at the top of the tree is called root. There is only one root per tree and one path from the
root node to any node.

•Parent − Any node except the root node has one edge upward to a node called parent.

•Child − The node below a given node connected by its edge downward is called its child node.

•Leaf − The node which does not have any child node is called the leaf node.
•Subtree − Subtree represents the descendants of a node.

•Visiting − Visiting refers to checking the value of a node when control is on the node.

•Traversing − Traversing means passing through nodes in a specific order.

•Levels − Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.

•keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
Binary Tree
Tree having children 0,1 or 2.

Binary Search Tree


Type of binary tree in which each left node is having a value less than root
node and each right node is having a value equal to or greater than root
node.
Traversing in the Binary Tree

Tree traversal is the process of visiting each node in the tree exactly once. Visiting each node in a
graph should be done in a systematic manner. If search result in a visit to all the vertices, it is called a
traversal. There are basically three traversal techniques for a binary tree that are,

1.Preorder traversal
2.Inorder traversal
3.Postorder traversal
1) Preorder traversal
To traverse a binary tree in preorder, following operations
are carried out:
1.Visit the root.
2.Traverse the left sub tree of root.
3.Traverse the right sub tree of root.
Note: Preorder traversal is also known as NLR traversal.
Practice questions
2) In order traversal
To traverse a binary tree in inorder traversal, following
operations are carried out:
1.Traverse the left most sub tree.
2.Visit the root.
3.Traverse the right most sub tree.
Note: Inorder traversal is also known as LNR traversal.
Practice questions
Therefore the in order traversal of above tree will
be: 0,1,2,3,4,5,6,7,8,9,10
3) Postorder traversal
To traverse a binary tree in postorder traversal, following operations
are carried out:
1.Traverse the left sub tree of root.
2.Traverse the right sub tree of root.
3.Visit the root.
Note: Postorder traversal is also known as LRN traversal.
Practice questions
Therefore the postorder traversal of the above tree will be: 0,2,4,6,5,3,1,8,10,9,7
Link for reference
Preorder, Inorder and Postorder in 5 minute | Tree Traversal | Easiest and Shortest Trick - YouTube

Simplest Binary Tree Traversal trick for preorder inorder postorder - YouTube
Binary Search Trees
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −
•The value of the key of the left sub-tree is less than the value of its parent (root)
node's key.
•The value of the key of the right sub-tree is greater than or equal to the value of its
parent (root) node's key.
SEARCHING, INSERTION AND
DELETION
Searching
Searching means finding or locating some specific element or node
within a data structure.
However, searching for some specific node in binary search tree is
pretty easy due to the fact that, element in BST are stored in a
particular order.

1.Compare the element with the root of the tree.

2.If the item is matched then return the location of the node.

3.Otherwise check if item is less than the element present on root,


if so then move to the left sub-tree.

4.If not, then move to the right sub-tree.

5.Repeat this procedure recursively until match found.


Insert function is used to add a new element in a binary search tree at appropriate location.

Insert function is to be designed in such a way that, it must node violate the property of
binary search tree at each value.

1.Allocate the memory for tree.

2.Set the data part to the value and set the left and right pointer of tree, point to NULL.

3.If the item to be inserted, will be the first element of the tree, then the left and right of
this node will point to NULL.

4.Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.

5.If this is false, then perform this operation recursively with the right sub-tree of the root.
Insert 12
Deletion
Delete function is used to delete the specified node from a binary search tree.

However, we must delete a node from a binary search tree in such a way, that the
property of binary search tree doesn't violate.

There are three situations of deleting a node from binary search tree.
FIRST:
The node to be deleted is a leaf node
Second: The node to be deleted has only one child.
The node to be deleted has two children.
In the following image, the node 50 is to be deleted which is the root node of the
tree.

The in-order traversal of the tree given below.


6, 25, 30, 50, 52, 60, 70, 75.

replace 50 with its in-order successor 52.

Now, 50 will be moved to the leaf of the tree, which will simply be deleted.
1. Which of the following is false about a binary search tree?
a) The left child is always lesser than its parent
b) The right child is always greater than its parent
c) The left and right sub-trees should also be binary search trees
d) In order sequence gives decreasing order of elements

2. What is the speciality about the inorder traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node

3. A Binary Tree can have


1.Can have 2 children
2.Can have 1 children
3.Can have 0 children
4.All
4. The number of edges from the root to the node is
called __________ of the tree.
a) Height
b) Depth
c) Length
d) Width

5. What is a full binary tree?


a) Each node has exactly zero or two children
b) Each node has exactly two children
c) All the leaves are at the same level
d) Each node has exactly one or two children

6. What is a complete binary tree?


a) Each node has exactly zero or two children
b) A binary tree, which is completely filled, with the possible exception of the bottom level, which is
filled from right to left
c) A binary tree, which is completely filled, with the possible exception of the bottom level, which is
filled from left to right
d) A tree In which all nodes have degree 2
Important

Preorder is also known as: NLR( node left right)

Postorder is also known as:LRN

Inorder is also known as:LNR


D,b,d, B,a,c

You might also like