Binary Trees, Binary Search Tree, Graph
Binary Trees, Binary Search Tree, Graph
Graphs:
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
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
Inorder: (left-root-right)
a
b c
e f
ebacf
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
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
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
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.