Similar documents
ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Binary Trees, Binary Search Trees

Binary Trees

March 20/2003 Jayakanth Srinivasan,

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

TREES. Trees - Introduction

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Trees. (Trees) Data Structures and Programming Spring / 28

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CE 221 Data Structures and Algorithms

Trees. Truong Tuan Anh CSE-HCMUT

Trees Algorhyme by Radia Perlman

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Graph Algorithms Using Depth First Search

Introduction to Computers and Programming. Concept Question

CSI 604 Elementary Graph Algorithms

Programming II (CS300)

Tree Structures. A hierarchical data structure whose point of entry is the root node

CSE 230 Intermediate Programming in C and C++ Binary Tree

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

CSI33 Data Structures

CS24 Week 8 Lecture 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

CS 151. Binary Trees. Friday, October 5, 12

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS 206 Introduction to Computer Science II

Graph. Vertex. edge. Directed Graph. Undirected Graph

Data Structure. IBPS SO (IT- Officer) Exam 2017

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Trees. Tree Structure Binary Tree Tree Traversals

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Why Do We Need Trees?


A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

CSCI2100B Data Structures Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Trees. Trees. CSE 2011 Winter 2007

CSC Intro to Intelligent Robotics, Spring Graphs

Algorithms and Data Structures

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

12 Abstract Data Types

Foundations of Discrete Mathematics

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CS DATA STRUCTURES AND ALGORITHMS

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Undirected Graphs. Hwansoo Han

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Chapter 20: Binary Trees

Binary Trees. Height 1

Algorithms. Deleting from Red-Black Trees B-Trees

Data Structures Brett Bernstein

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

W4231: Analysis of Algorithms

Trees. Arash Rafiey. 20 October, 2015

Graphs & Digraphs Tuesday, November 06, 2007

Graph and Digraph Glossary

Graphs: basic concepts and algorithms

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

CS 8391 DATA STRUCTURES

CS8391-DATA STRUCTURES

CS 280 Problem Set 10 Solutions Due May 3, Part A

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

Trees. CSE 373 Data Structures

UNIT 14 TREES 14.1 INTRODUCTION 14.2 BASIC TERMINOLOGY. Page No. Structure. Objectives

22.1 Representations of graphs

CSI33 Data Structures

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Advanced Tree Data Structures

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Garbage Collection: recycling unused memory

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

IX. Binary Trees (Chapter 10)

Analysis of Algorithms

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Graph: representation and traversal

CS301 - Data Structures Glossary By

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Transcription:

UNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1, T2...Tk, each of whose roots are connected by a directed edge from Root R. Example ROOT: A node which doesn't have a parent. In the above tree the Root is A. NODE: Item of Information LEAF: A node which doesn't have children is called leaf or Terminal node. Here B, K, L, G, H, M, J are leafs. SIBLINGS: Children of the same parents are said to be siblings, Here B, C, D, E, F, G are siblings. Similarly I, J & K, L are siblings. PATH: A path from node n, to nk is defined as a sequence of nodes n1, n2, n3...nk such that ni is the parent of ni+1. There is exactly only one path from each node to root. EC6301 & Object oriented programming and Data Structures Unit IV Page 1

Sri vidya College of Engineering & Technology, Virudhunagar E.g : Path from A to Q is A, E, J, Q. where A is the parent for E, E is the parent of J and J is the parent of Q. LENGTH: The length is defined as the number of edges on the path. The length for the path A to Q is 3. DEGREE: The number of subtrees of a node is called its degree. Degree of A is 3 Degree of C is 0 Degree of D is 1 LEVEL: The level of a node is defined by initially letting the root be at level Zero, if a node is at level L then its children are at level L + 1. Level of A is 0. Level of B, C, D, is 1. Level of F, G, H, I, J is 2 Level of K, L, M is 3. DEPTH: For any node n, the depth of n is the length of the unique path from root to n. The depth of the root is zero. HEIGHT: For any node n, the height of the node n is the length of the longest path from n to the leaf. The height of the leaf is zero The height of the root A is 3. The height of the node E is 2. Note: The height of the tree is equal to the height of the root. Depth of the tree is equal to the height of the tree. ANCESTOR AND DESCENDANT: If there is a path from n1 to n2. n1 is an ancestor of n2, n2 is a descendant of n1. IMPLEMENTATION OF TREES Each node having a data, and pointer to each child of node. The number of children per node can vary and is not known in advance, so it might be infeasible to make a children direct links in the data structure, because there would be too much wasted space. Here the arrows that point downward are First Child pointers. Arrow that go left to right are Next Sibling pointers. Null pointers are not drawn, there are too many. EC6301 & Object oriented programming and Data Structures Unit IV Page 2

4.2 Binary tree and its representation Binary tree Definition: - Binary Tree is a tree in which no node can have more than two children. Maximum number of nodes at level i of a binary tree is 2i-1. Types of Binary Tress 1) Rooted Binary tree: - In which every node has atmost two children. 2) Full Binary Tree:-In which every node has zero or two children. 3) Perfect Binary Tree:- In which all the leaves are at same depth. 4) Complete Binary Tree:- In which all the leaves are at same depth n or n-1 for some n. REPRESENTATION OF A BINARY TREE There are two ways for representing binary tree, they are * Linear Representation * Linked Representation Linear Representation The elements are represented using arrays. For any element in position i, the left child is in position 2i, the right child is in position (2i + 1), and the parent is in position (i/2) A B C D E F G 1 2 3 4 5 6 7 E.g i=1, For node A, Left child at 2i=>2x1=2=>B, Right child at 2i+1=>(2x1)+1=3=>C Linked Representation CS2411 & Operating Systems Unit I Page 3

Sri vidya College of Engineering & Technology, Virudhunagar The elements are represented using pointers. Each node in linked representation has three fields, namely, * Pointer to the left subtree * Data field * Pointer to the right subtree In leaf nodes, both the pointer fields are assigned as NULL. 4.3 Tree Traversals Traversing means visiting each node only once. Tree traversal is a method for visiting all the nodes in the tree exactly once. There are three types of tree traversal techniques, namely 1. Inorder Traversal 2. Preorder Traversal 3. Postorder Traversal Inorder Traversal The inorder traversal of a binary tree is performed as * Traverse the left subtree in inorder * Visit the root * Traverse the right subtree in inorder. Example: Inorder: A B C D E G H I J K CS2411 & Operating Systems Unit I Page 4

The inorder traversal of the binary tree for an arithmetic expression gives the expression in an infix form. RECURSIVE ROUTINE FOR INORDER TRAVERSAL void Inorder (Tree T) { if (T! = NULL) { Inorder (T->left); printelement (T->Element); Inorder (T->right); } } Preorder Traversal The preorder traversal of a binary tree is performed as follows, * Visit the root * Traverse the left subtree in preorder * Traverse the right subtree in preorder. Preorder: D C A B I G E H K J The preorder traversal of the binary tree for the given expression gives in prefix form. RECURSIVE ROUTINE FOR PREORDER TRAVERSAL void Preorder (Tree T) { if (T! = NULL) { printelement (T->Element); Preorder (T->left); Preorder (T->right); } CS2411 & Operating Systems Unit I Page 5

Sri vidya College of Engineering & Technology, Virudhunagar Postorder Traversal The postorder traversal of a binary tree is performed by the following steps. * Traverse the left subtree in postorder. * Traverse the right subtree in postorder. * Visit the root. Post order: B A C E H G J K I D The postorder traversal of the binary tree for the given expression gives in postfix form. RECURSIVE ROUTINE FOR POSTORDER TRAVERSAL void Postorder (Tree T) { if (T! = NULL) { Postorder (T->Left); Postorder (T->Right); PrintElement (T->Element); } EXAMPLE FOR TREE TRAVERSAL Inorder : 5 10 15 20 25 30 40 Preorder : 20 10 5 15 30 25 40 Postorder : 5 15 10 25 40 30 20 CS2411 & Operating Systems Unit I Page 6

EXPRESSION TREE Expression Tree is a binary tree in which the leaf nodes are operands and the interior nodes are operators. Like binary tree, expression tree can also be travesed by inorder, preorder and postorder traversal. 4.4 Application of trees: Set representation and Union-Find operations Set Representation Trees can be used to represent sets. Disjoint set Union If S i and S j are two disjoint sets, then their union S i S j = {all elements x such that x is in S i or S j }. Find (i) Find the set containing element i. Possible Tree Representation of Sets Unions of Sets To obtain the union of two sets, just set the parent field of one of the roots to the other root. To figure out which set an element is belonged to, just follow its parent link to the root and then follow the pointer in the root to the set name. Possible Representations of S 1 S 2 CS2411 & Operating Systems Unit I Page 7

Analysis of Weighted Union and CollapsingFind The use of collapsing rule roughly double the time for an individual find. However, it reduces the worst-case time over a sequence of finds. CS2411 & Operating Systems Unit I Page 8

4.5 Graph and its representation A graph G = (V, E) consists of a set of vertices, V and set of edges E. Vertices are referred to as nodes and the arc between the nodes are referred to as Edges. Each edge is a pair (v, w) where u, w V. (i.e.) v = V1, w = V2... Here V1, V2, V3, V4 are the vertices and (V1, V2), (V2, V3), (V3, V4), (V4, V1), (V2, V4), (V1, V3) are edges. BASIC TERMINOLOGIES Directed Graph (or) Digraph Directed graph is a graph which consists of directed edges, where each edge in E is unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v) (V1, V2) # (V2, V1) CS2411 & Operating Systems Unit I Page 9

Sri vidya College of Engineering & Technology, Virudhunagar Undirected Graph An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undirected edge then (v,w) = (w, v) (V1, V2) = (V2, V1) Weighted Graph A graph is said to be weighted graph if every edge in the graph is assigned a weight or value. It can be directed or undirected graph. Complete Graph A complete graph is a graph in which there is an edge between every pair of vertices. A complete graph with n vertices will have n (n - 1)/2 edges. Degree The number of edges incident on a vertex determines its degree. The degree of the vertex V is written as degree (V). The indegree of the vertex V, is the number of edges entering into the vertex V. Similarly the out degree of the vertex V is the number of edges exiting from that vertex V. Number of vertics is 4 Number of edges is 6 (i.e) there is a path from every vertex to every other vertex. A complete digraph is a strongly connected graph. Strongly Connected Graph If there is a path from every vertex to every other vertex in a directed graph then it is said to be strongly connected graph. Otherwise, it is said to be weakly connected graph. CS2411 & Operating Systems Unit I Page 10

Sri vidya College of Engineering & Technology, Virudhunagar Representations of graph Following two are the most commonly used representations of graph. 1. Adjacency Matrix 2. Adjacency List 1. Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph.let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. The adjacency matrix for the above example graph is: Adjacency List: An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of the above graph. CS2411 & Operating Systems Unit I Page 11

Graph Traversals Depth-first search (DFS) for undirected graphs Depth-first search, or DFS, is a way to traverse the graph. Initially it allows visiting vertices of the graph only, but there are hundreds of algorithms for graphs, which are based on DFS. Therefore, understanding the principles of depth-first search is quite important to move ahead into the graph theory. The principle of the algorithm is quite simple: to go forward (in depth) while there is such possibility, otherwise to backtrack. Algorithm In DFS, each vertex has three possible colors representing its state: white: vertex is unvisited; gray: vertex is in progress; black: DFS has finished processing the vertex. NB. For most algorithms boolean classification unvisited / visited is quite enough, but we show general case here. Initially all vertices are white (unvisited). DFS starts in arbitrary vertex and runs as follows: 1. Mark vertex u as gray (visited). 2. For each edge (u, v), where u is white, run depth-first search for u recursively. CS2411 & Operating Systems Unit I Page 12

3. Mark vertex u as black and backtrack to the parent. Example. Traverse a graph shown below, using DFS. Start from a vertex with number 1. Source graph. Mark a vertex 1 as gray. There is an edge (1, 4) and a vertex 4 is unvisited. Go there. CS2411 & Operating Systems Unit I Page 13

Mark the vertex 4 as gray. There is an edge (4, 2) and vertex a 2 is unvisited. Go there. Mark the vertex 2 as gray. CS2411 & Operating Systems Unit I Page 14

There is an edge (2, 5) and a vertex 5 is unvisited. Go there. Mark the vertex 5 as gray. There is an edge (5, 3) and a vertex 3 is unvisited. Go there. CS2411 & Operating Systems Unit I Page 15

Mark the vertex 3 as gray. There are no ways to go from the vertex 3. Mark it as black and backtrack to the vertex 5. There is an edge (5, 4), but the vertex 4 is gray. CS2411 & Operating Systems Unit I Page 16

There are no ways to go from the vertex 5. Mark it as black and backtrack to the vertex 2. There are no more edges, adjacent to vertex 2. Mark it as black and backtrack to the vertex 4. There is an edge (4, 5), but the vertex 5 is black. CS2411 & Operating Systems Unit I Page 17

There are no more edges, adjacent to the vertex 4. Mark it as black and backtrack to the vertex 1. There are no more edges, adjacent to the vertex 1. Mark it as black. DFS is over. As you can see from the example, DFS doesn't go through all edges. The vertices and edges, which depth-first search has visited is a tree. This tree contains all vertices of the graph (if it is connected) and is called graph spanning tree. This tree exactly corresponds to the recursive calls of DFS. If a graph is disconnected, DFS won't visit all of its vertices. For details, see finding connected components algorithm. Breadth First Search The idea in a breadth first search is opposite to a depth first search. Instead of searching down a single path until you can go no longer, you search all paths at an uniform depth from the source before moving onto deeper paths. Once again, we'll need to mark both edges and vertices based on what has been visited. In essence, we only want to explore one "unit" away from a searched node before we move to a different node to search from. All in all, we will be adding nodes to the back of a queue to be ones to searched from in the future. In the implementation on the following page, a set of queues L i are maintained, each storing a list of vertices a distance of i edges from the starting vertex. One can implement this algorithm with a single queue as well. CS2411 & Operating Systems Unit I Page 18

Let L i be the set of vertices visited that are a path length of i from the source vertex for the algorithm. BFS(G,s): 1) Let L 0 be empty 2) Insert s into L 0. 3) Let i = 0 4) While L i is not empty do the following: A) Create an empty container L i+1. B) For each vertex v in L i do i) For all edges e incident to v a) if e is unexplored, mark endpoint w. b) if w is unexplored Mark it. Insert w into L i+1. Label e as a discovery edge. else Label e as a cross edge. C) i = i+1 The basic idea here is that we have successive rounds and continue with our rounds until no new vertices are visited on a round. For each round, we look at each vertex connected to the vertex we came from. And from this vertex we look at all possible connected vertices. This leaves no vertex unvisited because we continue to look for vertices until no new ones of a particular length are found. If there are no paths of length 10 to a new vertex, surely there can be no paths of length 11 to a new vertex. The algorithm also terminates since no path can be longer than the number of vertices in the graph. All BFS paths from A to others (assumes ABC edge order) A A -> B A -> C CS2411 & Operating Systems Unit I Page 19

A -> E A -> B -> D A -> B -> F - A -> C -> G Connected Components The connected components of an undirected graph are the equivalence classes of vertices under the ``is reachable from'' relation A graph with three connected components: {1,2,3,4}, {5,6,7}, and {8,9}. Perform DFS on the graph to get a forest - eac tree in the forest corresponds to a separate connected component. CS2411 & Operating Systems Unit I Page 20

Part (b) is a depth-first forest obtained from depth-first traversal of the graph in part (a). Each of these trees is a connected component of the graph in part (a). Partition the graph across processors and run independent connected component algorithms on each processor. At this point, we have p spanning forests. In the second step, spanning forests are merged pairwise until only one spanning forest remains. CS2411 & Operating Systems Unit I Page 21

CS2411 & Operating Systems Unit I Page 22