• No results found

Lecture 00 Array Stack Queue

N/A
N/A
Protected

Academic year: 2020

Share "Lecture 00 Array Stack Queue"

Copied!
50
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture Outline

Introduction

Basic Tree Operation

Binary Search Tree

AVL tree

Heap tree

(2)

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

(3)
(4)

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.

(5)

Introduction: Binary Tree

Construct a

binary tree

for the

following algebraic expression:

(6)

Binary Tree

Complete binary tree

All its level, except the last have the

maximum number of possible nodes.

Extended binary tree

(7)

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

(8)

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,

(9)

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:

(10)

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.

(11)

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:

(12)
(13)

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 

(14)

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

(15)

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

(16)

Binary Search Tree

BST has data part and references to

its left and right child nodes.

struct node

{

int data;

(17)

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

(18)

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

(19)
(20)

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

(21)
(22)

BST: Exercise

Construct a BST tree for the following

input:

(23)
(24)

BST: Exercise

(25)

BST: Exercise

Construct a BST tree for the following

input:

(26)
(27)

BST: Exercise

(28)
(29)

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

(30)
(31)
(32)
(33)
(34)
(35)
(36)
(37)

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

(38)

Heap: Min Heap

Min-Heap

 − where the value of root

node is less than or equal to either of

its children.

(39)

Heap: Max Heap

Max-Heap

 − where the value of root

node is greater than or equal to

either of its children.

(40)

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.

(41)

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

(42)
(43)

Traversal Algorithm using

stack

In-order traversal

Pre-order traversal

(44)

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

(45)
(46)

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.

(47)

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 −

(48)

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.

(49)

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 asKruskalsalgorithm 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

(50)

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.

References

Related documents

Capture the presence of existing control technology. For example, in the USA the units most amenable to CCS retrofit are likely to be the units that became operational after

Napier, Gary (2014) A Bayesian hierarchical model of compositional data with zeros: classification and evidence evaluation of forensic

This study aimed at assessing the proportions and risk factors of poor immune recovery in adult HIV- infected patients on 48 months of HAART attending care and treatment center (CTC)

Despite overall positive perceptions of the referral system, caregivers in three villages reported that health facilities utilised the VHT as a gatekeeper, noting that if they

According to the results of Tomas Li-Ping Tang’s research (Tomas Li-Ping Tang, 1996, p. Similar results were obtained by Kent T. 522-528) while researching attitude to money

South Broadway: A Qualitative Analysis of Legal Marijuana and South Broadway: A Qualitative Analysis of Legal Marijuana and Place in a Denver Commercial District.. Place in a

It was found that teaching experience, highest academic degree, science teaching efficacy, and the level of teachers’ adop- tion of a constructivist view about teaching and

Stalk dry mass measured (points plus bars) and simulated (lines) by APSIM-Sugar model after canopy parameters calibration and introduction of growth slowdown feature to account for