Lecture Outline
✔
Introduction
✔
Basic Tree Operation
✔
Binary Search Tree
✔
AVL tree
✔
Heap tree
Introduction
✔
Tree represents nodes connected by edges.
✔
A
binary tree
(T) has a special condition that
each node can have two children at maximum.
✔
If T is empty called
null tree or empty tree
✔
T contains a distinguished node R, called the
root
of
T.
✔
If T contains a root R, then two trees T
1
and T
2
are
Binary Tree
✔
Root
− Node at the top of the tree is called root.
There is only one root per tree and one path from root
node to any node.
✔
Parent
− Any node except root node has one edge
upward to a node called parent.
✔
Child
− Node below a given node connected by its
edge downward is called its child node.
✔
Leaf
− Node which does not have any child node is
called leaf node.
Introduction: Binary Tree
✔
Construct a
binary tree
for the
following algebraic expression:
Binary Tree
✔
Complete binary tree
✔
All its level, except the last have the
maximum number of possible nodes.
✔
Extended binary tree
Tree Traversal
✔
Traversal is a process to visit all the
nodes of a tree.
✔
There are three ways to traverse a
tree:
✔
In-order
Traversal
✔
Pre-order
Traversal
Tree Traversal: In-order
✔
In this traversal method, the
left-subtree is visited first, then root and
then the right sub-tree.
✔
Every node may represent a subtree
itself.
✔
If a binary tree is traversed in-order,
Tree Traversal: In-order
Until all nodes are traversed
Step 1: Traverse left subtree.
Step 2: Process the root node.
Step 3: Traverse right subtree.
For this tree, the output will be:
Tree Traversal: Pre-order
Until all nodes are traversed
Step 1: Process the root node.
Step 2: Traverse the left subtree.
Step 3: Traverse the right
subtree.
Tree Traversal: Post-order
Until all nodes are traversed
Step 1: Traverse the left subtree.
Step 2: Traverse the right
subtree.
Step 3: Process the root node.
For this tree, the output will be:
Tree Traversal: Solution
✔
Pre-Order - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3
✔
In-Order - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
Tree Traversal: Exercise
✔
Draw a tree for the following
expression:
✔
[a + (b - c)]*[(d - e)/(f + g - h)]
✔
Find the Pre-order and Post-order
Binary Search Tree
✔
A binary search tree (BST) follows:
✔
The left sub-tree of a node has key less
than or equal to its parent node's key.
✔
The right sub-tree of a node has key
Binary Search Tree
✔
BST has data part and references to
its left and right child nodes.
struct node
{
int data;
Binary Search Tree
✔
Insert − insert an element in a tree /
create a tree.
✔
Search − search an element in a tree.
✔
Pre-order Traversal − traverse a tree in
a pre-order manner.
✔
In-order Traversal − traverse a tree in an
in-order manner.
✔
Post-order Traversal − traverse a tree in
BST: Search Algorithm
✔
If root.data is equal to search.data
✔
return ROOT
✔
else
✔
while DATA not found
✔
If DATA is greater than node.data
✔
goto RIGHT subtree
✔
else
✔
goto LEFT subtree
✔
If DATA found
✔
return NODE
BST: Insertion Algorithm
•
If ROOT is NULL then
–
create ROOT node
•
Return
•
If ROOT exists then
–
compare the DATA with node.data
–
while until insertion position is located
•
If DATA is greater than node.data
–
goto RIGHT subtree
•
else
BST: Exercise
✔
Construct a BST tree for the following
input:
BST: Exercise
BST: Exercise
✔
Construct a BST tree for the following
input:
BST: Exercise
AVL TREE
✔
An AVL tree is a balanced binary search tree. Named after
their inventors, Adelson-Velskii and Landis
✔
They are not perfectly balanced, but pairs of sub-trees differ
in height by at most 1, maintaining an O(logn) search time.
Addition and deletion operations also take O(logn) time.
✔
An AVL tree is a binary search tree which has the following
properties:
✔
The sub-trees of every node differ in height by at most
one.
✔
Every sub-tree is an AVL tree.
✔
What if the input to binary search tree comes in sorted
Heap
•
Heap is a special case of balanced
binary tree data structure where
root-node key is compared with its
children and arranged accordingly.
•
If α has child node β then
Heap: Min Heap
•
Min-Heap
− where the value of root
node is less than or equal to either of
its children.
Heap: Max Heap
•
Max-Heap
− where the value of root
node is greater than or equal to
either of its children.
Max Heap Insertion
•
Step 1 −
Create a new node at the end
of heap.
•
Step 2 −
Assign new value to the node.
•
Step 3 −
Compare the value of this
child node with its parent.
•
Step 4 −
If value of parent is less than
child, then swap them.
Max Heap Deletion
•
Step 1 −
Remove root node.
•
Step 2 −
Move the last element of last
level to root.
•
Step 3 −
Compare the value of this
child node with its parent.
•
Step 4 −
If value of parent is less than
child, then swap them.
•
Step 5 −
Repeat step 3 & 4 until Heap
Traversal Algorithm using
stack
•
In-order traversal
•
Pre-order traversal
Spanning Tree
✔
A spanning tree is a subset of Graph G,
which has all the vertices covered with
minimum possible number of edges.
✔
A spanning tree does not have cycles and
http://www.tutorialspoint.com/data_structures_algorithms/kruskals_spanning_tree_algorithm.htm
Copyright © tutorialspoint.com
KRUSKAL'S SPANNING TREE ALGORITHM
KRUSKAL'S SPANNING TREE ALGORITHM
Kruskal's algorithm to find minimum cost spanning tree uses greedy approach. This algorithm treats the graph as a forest and every node it as an individual tree. A tree connects to another only and only if it has least cost among all available options and does not violate MST properties.
To understand Kruskal's algorithm we shall take the following example −
Step 1 - Remove all loops & Parallel Edges
Remove all loops and parallel edges from the given graph.
Step 3 - Add the edge which has least weightage
Now we start adding edges to graph beginning from the one which has least weight. At all time, we shall keep checking that the spanning properties are remain intact. In case, by adding one edge, the spanning tree property does not hold then we shall consider not to include the edge in graph.
The least cost is 2 and edges involved are B,D and D,T so we add them. Adding them does not violate spanning tree properties so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. So we add them −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph −
Now we are left with only one node to be added. Between two least cost edges available 7, 8 we shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we have minimum cost spanning tree.
http://www.tutorialspoint.com/data_structures_algorithms/prims_spanning_tree_algorithm.htm
Copyright © tutorialspoint.com
PRIM'S SPANNING TREE ALGORITHM
PRIM'S SPANNING TREE ALGORITHM
Prim's algorithm to find minimum cost spanning tree asKruskal′salgorithm uses greedy approach. Prim's algorithm shares similarity with shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shall use the same example −
Step 1 - Remove all loops & Parallel Edges
In this case, we choose S node as root node of Prim's spanning tree. This node is arbitrarily chosen so any node can be root node. One may wonder why can any video be a root node, so the answer is, in spanning tree all the nodes of a graph are included and because it is connected then there must be at least one edge which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing root node S, we see that S,A and S,C are two edges with weight 7 and 8 respectively. And we choose S,A edge as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We select the one which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat is as a node and will check all the edges again and will choose only the least cost edge one. Here in this case C-3-D is the new edge which is less than other edges' cost 8, 6, 4 etc.