Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Binary Trees, Binary Search Trees,

Graphs:

Binary tree is a combination of edges and pointers. They


are arranged with the help of nodes from root to leaf
nodes. Every root has child and this child become parent
for other child. Child of same parent is called sibling.
Binary trees are those trees whose parent has maximum
two child.
a-> root node
b,c-> internal nodes
e,f,g,h are leaf nodes

b c

e f h
g
Example of binary tree.
Where 1 is root of the tree, 2 is left sub tree and 3 is right
sub tree.
Every node consists of three things:
Data, left pointer, right pointer
Left ptr Data field Right ptr

Some properties of Binary Trees:


1) Formula to calculate number of nodes at level “p”
Of a binary tree is “2p-1”
If we have at 4 levels, calculate the maximum nodes=24-
1=8

2) Formula to calculate number of node of a binary


tree whose height is h=“2h-1”
Height of a tree=3
23-1=7

3) Formula to calculate possible level with N nodes of


binary tree is Log2(N+1)
Example number of nodes=9
Log2(9+1)

Types of Binary tree:


1)Full Binary tree: A tree with maximum 0 or 2 child
2)Complete Binary tree: A tree whose all levels are
completely full except last level.
3)Perfect Binary Tree: A tree whose internal node has
two child and equal at same level
Insertion in Binary tree:
• The order to traverse a tree is done using a queue.
• Find the empty node in the tree
• Create a new key as left or right child of the node
• We keep traversing until we find any empty right or
left child.
Deletion in Binary tree:
• To delete a node from a tree
• It makes by shrinking it from the bottom
• Deleted node is replaced by bottom most or
rightmost node.

BFS and DFS in binary tree


BFS:
When we traverse(to visit) to all the of the tree as per
their levels one after another

1
23
4567
8 9 10 11 13 14
Depth first search:
Inoder (left-root-right)
Preoder (root left right)
post order(left right root)

b c

e f

Preoder (root left right)


To traverse data in the tree:
Output:a b e f c

Inorder: (left-root-right)
a

b c

e f

ebacf

Postorder: left right root

b c

e f

efbca
Binary Search tree:
Binary search tree: root and parent of the tree will have
0,1 or maximum 2 child
And child will be place in a way that left sub tree will be
lesser than the root and right sub tree will be greater
than the root

Example of binary search tree:

3 10

1 6

Root is 7
3 is left child which is lesser than 7
10 is greater than 7
1<3 which is in left whereas 6>3 which is in right
Left sub tree: 3,6,1
Right sub tree:10,14,13
Left subtree<8(root)< right subtree
7

3 10

1 6

How we are going to search :


Let say , search 6
1) 6 will be compared to 7
7>6, move to left
7->next(right ptr)->3<6
6->next(right ptr)->(6==6)-> yes
Search done 6 searched.
Graphs:
Graph is a non-linear data structure that has combination of edges and vertices
like tree but it also has loops, can also be closed which are unlike trees.

b c

Above figure is a closed and directed graph where a,b,c are vertices and ab,bc,ac
are edges.
When a edge is move over the same node is called a loop. That can be seen in
example where there is a loop over c.

b c

Difference between BFS and DFS in graphs


BFS DFS
Data Structure queue stack
used
structure Wide and short Narrow and long
Algorithm Vertex oriented Edge oriented
algo algo

In graph BFS visit various nodes of the graph which are unvisited and adjacent to
start node whereas DFS visit various nodes of the graph but it visit a complete
long visit adjacent to one direction.
a c

b e

BFS:b,a,e,c
DFS:b,a,c,e
Application of graphs:
Used in application like maps to know the direction and length of the path over
the maps.

You might also like