Algorithms and Data Structures

Size: px
Start display at page:

Download "Algorithms and Data Structures"

Transcription

1 Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016

2 Overview general linked lists stacks queues trees 2 2

3 General fundamentals data structure way of organising data for processing by some algorithm or more general by some computer program often the choice of proper data structures is the only major decision for implementations for the same information some data structures require more or less space for storing the information for the same operations (on the data) some data structures lead to more or less efficient algorithms hence, the choice of algorithms and data structures is closely intertwined design of algorithms also means design of sufficient data structures 2 3

4 Overview general linked lists stacks queues trees 2 4

5 Linked Lists fundamentals elementary data type, but not (yet) defined as primitive in all languages advantages compared to arrays easy growth and shrinkage in size over lifetime, hence, maximum size must not be known in advance high flexibility w.r.t. rearrangement of data items (nevertheless, this comes up with higher expenses for the access of arbitrary items) general description set of sequentially organised items, so called nodes explicit organisation of nodes via link to next node N 1 N 2 N 3 N M 1 N M linked list with nodes N i, 1 i M 2 5

6 Linked Lists fundamentals (cont d) each node contains one container for storing data item value (integer, float, boolean, ) one container for storing link to next node value i 1 value i value i 1 problem: what to specify as next node in case of last node hence, dummy node NIL to indicate end of list for reasons of convenience also dummy node HEAD at the other end HEAD N 1 N 2 N M 1 N M NIL linked list with nodes N i, 1 i M 2 6

7 Linked Lists fundamentals (cont d) assume, there s a primitive list with two containers value and link which can be accessed via list value and list link furthermore, a dummy node NIL should also be provided hence, the following program creates an empty list list L L link NIL which only consists of the two dummy nodes HEAD and NIL HEAD NIL for adding nodes to list L some further understanding is necessary 2 7

8 Linked Lists fundamentals (cont d) linked lists are dynamic structures easy rearrangement of nodes possible (just changing links instead of moving items in case of arrays) example: last node should be moved to the front (3 links to be changed) HEAD NIL HEAD NIL HEAD NIL 2 8

9 Linked Lists list operations typical operations on linked lists are insertion of new nodes (which makes it grow by one in length each) deletion of old nodes (which makes it shrink by one in length each) for inserting a new node only two links have to be changed 4 HEAD NIL 4 HEAD NIL 2 9

10 Linked Lists list operations (cont d) the following procedure appends a new node at the end of list L by recursively processing all nodes starting from HEAD procedure APPEND (L, new_node) if L link NIL then new_node link L link L link new_node else APPEND (L link, new_node) fi end changing the condition allows to insert a new node anywhere in the list 2 10

11 Linked Lists list operations (cont d) the following procedure inserts a new node before the one with value val procedure INSERT (L, new_node, val) case L link NIL: exit L link value val: new_node link L link L link new_node else INSERT (L link, new_node, val) esac end the first test for NIL is necessary in case there is no node with value val and the recursion reaches the end of list L 2 11

12 Linked Lists list operations (cont d) when deleting node N i from a list only one link has to be moved (i. e. linking from node N i 1 to node N i 1 ) this does not delete node N i itself (which still points to N i 1 ), but it is no longer accessible by following the links from HEAD HEAD NIL HEAD NIL 2 12

13 Linked Lists list operations (cont d) the following procedure deletes a node (with value val) from lists L procedure DELETE (L, next, val) case next NIL: exit next value val: L link next link else DELETE (L link, next link, val) esac end the temporary node next is necessary to link from N i 1 to N i 1 (again, the dummy element HEAD makes everything more convenient) for deleting some node from list L the procedure is called with parameters DELETE (L, L link, val) 2 13

14 Linked Lists doubly linked lists as shown, linked lists are very efficient for certain operations, but there is no direct access to single nodes hence, finding the kth item has a complexity of (N) as all nodes have to be processed successively until the kth item is reached (compared to (1) for arrays) furthermore, there is no possibility of going backwards in case a node that has already been processed is needed (such as N i 1 for deleting N i ) idea: maintain two links for each node, one to the item before and one to the item after so called doubly linked lists HEAD NIL N 1 N 2 N M 1 N M NIL doubly linked list with nodes N i, 1 i M TAIL 2 14

15 Overview general linked lists stacks queues trees 2 15

16 Stacks fundamentals for many applications rather hard restrictions concerning the accessibility of data structures can be made in order to hide (often complicated and, thus, error prone) implementation details such as links or indices allow more flexible implementations with fewer operations famous representatives of such restricted-access data structures are so called stacks or pushdown stacks, i.e. one-dimensional structures with LIFO (last in, first out) ordering that support two types of basic operations push (val): inserting one element with value val at the beginning of the stack pop: removing one element from the beginning of the stack and returning its value 2 16

17 Stacks fundamentals (cont d) stacks are, for instance, frequently used for evaluating arithmetic expressions (within pocket calculators, e.g.) arithmetic expressions can be written in different notations prefix: operands appear after the operator infix: the customary way of writing arithmetic expressions (but parentheses may be required) postfix or reverse Polish: operands appear before the operator therefore, stacks require arithmetic expressions to appear in postfix notation so the operands can be on the stack when the operator is encountered (as expression are processed from left to right) and, thus, intermediate results can again be pushed to the stack 2 17

18 Stacks implementation of stacks stacks to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists assume, there s a primitive stack (of fixed size N) implemented as array further assumptions and requirements are necessary it s not possible to push an element on a full stack it s not possible to pop an element from an empty stack index last indicates the last element in a stack stack of size N 10 with last 4 hence, operations push and pop ready to be implemented 2 18

19 Stacks implementation of stacks (cont d) push: variables N and last with global visibility procedure PUSH (S, val) if last N 1 then last last 1 S(last) val fi end where S indicates a stack and val the value to be pushed on the stack 2 19

20 Stacks implementation of stacks (cont d) pop: variables N and last with global visibility procedure POP (S) tmp 0 if last 0 then tmp S(last) last last 1 return (tmp) else return (false) fi end where S indicates a stack 2 20

21 Stacks example evaluation of an arithmetic expression A B C (D E) using a stack stack S push (S, B) push (S, C) push (S, D) push (S, E) push (S, pop (S) pop (S)) push (S, pop (S) pop (S)) push (S, pop (S) pop (S)) A pop (S) E D C B operators and have to switch operands due to LIFO ordering 2 21

22 Overview general linked lists stacks queues trees 2 22

23 Queues fundamentals other representatives of restricted-access data structures are queues one-dimensional structures with FIFO (first in, first out) ordering that support two types of basic operations insert (val): inserting one element with value val at the beginning of the queue remove: removing one element from the end of the queue and returning its value in comparison to stacks, the order of processing reflects the order of arrival of elements (also referred to as first come, first serve) queues to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists 2 23

24 Queues implementation of queues assume, there s a primitive queue (of fixed size N) implemented as array further assumptions and requirements are necessary it s not possible to insert an element into a full queue it s not possible to remove an element from an empty queue variable used indicates the amount of already used cells two indices first and last indicate the first and the last element in a queue, taking into account the wraparound back to 0 when the end of the array is encountered queue of size N 10 with used 5, first 7, and last 1 hence, operations insert and remove ready to be implemented 2 24

25 Queues implementation of queues (cont d) insert: variables N, used, first, and last with global visibility procedure INSERT (Q, val) if used N then last last 1 if last N then last 0 fi Q(last) val used used 1 fi end where Q indicates a queue and val the value to be inserted into the queue 2 25

26 Queues implementation of queues (cont d) remove: variables N, used, first, and last with global visibility procedure REMOVE (Q) tmp 0 if used 0 then tmp Q(first) used used 1 first first 1 if first N then first 0 fi return (tmp) else return (false) fi end 2 26

27 Overview general linked lists stacks queues trees 2 27

28 Trees fundamentals definition: a tree is a finite set of nodes such that 1) there is one specially designated node called root 2) remaining nodes are partitioned into N 0 disjoint sets T 1,, T N where each of these sets is a tree (so called subtree of the root) 3) there exists exactly one path between the root and each of the other nodes in the tree root links between nodes might point downwards (away from the root) or upwards (towards the root) depending upon the application 2 28

29 Trees fundamentals (cont d) further definitions (1) each node (except the root) has exactly one node above it which is called its parent hence, the nodes directly below a node are called its children nodes with no children are called leaves or terminal nodes hence, nodes with children are called internal or nonterminal nodes parent children internal nodes leaves 2 29

30 Trees fundamentals (cont d) further definitions (2) children of the same parent are said to be siblings a forest is a set of N 0 disjoint trees (removing the root of a tree leads to a forest) the level of a node is defined by initially letting the root be at level one; hence, if a node is at level K, then its children are at level K 1 the height or depth of a tree is the maximum level of any node in the tree level 1 siblings level 2 level

31 Trees representation of trees idea: using linked lists where one node in the list corresponds to one node in the tree problem: nodes can have a varying number of links difficult to write algorithms for data structures with varying sizes hence, already known list elements to be extended by one additional link as follows value siblings children 2 31

32 Trees representation of trees (cont d) example A B E G HEAD A C D F H I J B E G C D F H I J i. e. link pointing to NIL 2 32

33 Trees binary trees definition: a binary tree is a finite set of nodes which is either empty or consists of a root node and two disjoint binary trees called the left and right subtree hence, any node can have at most two children ( name binary tree ) examples of binary trees skewed binary tree binary tree full binary tree 2 33

34 Trees binary trees (cont d) properties a tree with N nodes has N 1 edges (i. e. links); this is true for all kind of trees and follows directly from the recursive definition the maximum number of nodes N in a full binary tree of depth K 0 is 2 K 1 the height of a full binary tree with N nodes is about ld N (log 2 N) for representing binary trees, linked lists can be used again (labelling one link as left and one link as right) value left right drawback: determination of a node s parent due to missing upward link 2 34

35 Trees binary trees (cont d) example HEAD A B F C G A D E B F C G D E i. e. link pointing to NIL 2 35

36 Trees binary trees (cont d) adding a node to a binary tree is a quite simple task 1) find a corresponding node X with at least one free link left or right 2) link from X to the new node Y, thus, X becomes parent of Y Y X X Y 2 36

37 Trees binary trees (cont d) deleting a node from a binary tree is a more complex task to be distinguished a) deleting a leaf node b) deleting an internal node with one child c) deleting an internal node with two children cases a) and b) are quite easy to process, but case c) requires further understanding case a) case b) case c) 2 37

38 Trees binary trees (cont d) case a: deleting a leaf node 1) find the corresponding parent of the leaf node to be deleted 2) set the parent s link to NIL node to be deleted 2 38

39 Trees binary trees (cont d) case b: deleting an internal node with one child 1) find the corresponding parent of the node to be deleted 2) set the parent s link to the child of the node to be deleted node to be deleted 2 39

40 Trees binary trees (cont d) case c: deleting an internal node with two children 1) find the corresponding parent of the node to be deleted 2) find the right-most child (X) from the node to be deleted its left child 3) set the parent s link to the left child of the node to be deleted 4) set X s right link to the right child of the node to be deleted node to be deleted right-most child X 2 40

Definition A tree is either empty or consists of one node called the root and zero or more subtrees.

Definition A tree is either empty or consists of one node called the root and zero or more subtrees. Binary Trees Introduction We extend the concept of linked data structures to structure containing nodes with more than one self-referenced field. Definition A tree is either empty or consists of one node

More information

Data Structures 2016

Data Structures 2016 Unit-1 Introduction Short Questions:- 1. Define data structure. 2. Define algorithm. 3. List out areas in which data structures are applied. 4. List out structure of algorithm. 5. List out properties of

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

More information

Introduction: Practical daily life: In computer world:

Introduction: Practical daily life: In computer world: Need of stack In the linear array, one can insert and delete elements at any place in the list-at the beginning, at the end or in the middle. There are certain situations in computer science when one wants

More information

MODULE 3: TREES BASIC TERMINOLOGY

MODULE 3: TREES BASIC TERMINOLOGY MODULE 3: TREES BASIC TERMINOLOGY Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand

More information

GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology

GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology Subject:Data Structures Subject Code:3330704 Semester: III Assignment-1 1.What is Data Structure? Explain Primitives & Non-Primitive data structure

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 2: stacks and queues Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International

More information

Priority Queue - Heap

Priority Queue - Heap Priority Queue - Heap Priority Queue Typical Queue First-In, First-Out. In certain situations Next-In, High Priority-Out. Examples: Print Queue smallest job exits the queue first. Operating System Process

More information

Data Structure [Question Bank]

Data Structure [Question Bank] Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

More information

CSE 530A. Binary Search Trees. Washington University Fall 2013

CSE 530A. Binary Search Trees. Washington University Fall 2013 CSE 530A Binary Search Trees Washington University Fall 2013 Indexing A common index type is based on the B- tree The B-tree data structure is a generalization of the Binary Search Tree A binary search

More information

AS-2342 M.C.A.1 st Semester, Examination-2013 Paper fourth Subject-Data structure with algorithm

AS-2342 M.C.A.1 st Semester, Examination-2013 Paper fourth Subject-Data structure with algorithm AS-2342 M.C.A.1 st Semester, Examination-2013 Paper fourth Subject-Data structure with algorithm Time: Three Hours] [Maximum Marks: 60 Note: attempt all questions. Q. no. 1 is compulsory. Answer any four

More information

MODULE 4: TREES DEFINITION TERMINOLOGY

MODULE 4: TREES DEFINITION TERMINOLOGY MODULE 4: TREES DEFINITION A tree is a finite set of one or more nodes such that There is a specially designated node called root. The remaining nodes are partitioned into n >= 0 disjoint set T1,,Tn, where

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture I: Chapter 1 R. Paul Wiegand George Mason University, Department of Computer Science January 25, 2006 Outline 1 Introduction 2 Algorithms & Problems

More information

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

More information

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

End-Term Examination Second Semester [MCA] MAY-JUNE 2006 Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1. is compulsory and is of 20 marks. Attempt one out of two questions from remaining four units.

More information

Data Structures 2014

Data Structures 2014 Unit-1 Introduction Short Questions:1. Define data structure. 2. Define algorithm. 3. List out areas in which data structures are applied. 4. List out structure of algorithm. 5. List out properties of

More information

! Linked Lists. ! Can we make a simple data structure that. ! Trees. ! edges are directed down (source is higher)

! Linked Lists. ! Can we make a simple data structure that. ! Trees. ! edges are directed down (source is higher) Trees and Binary Search Trees Chapters 18 and 19 Dynamic data structures! Linked Lists CS 3358 Summer II 2013 Jill Seaman Sections 18.1-4, 19.1-3 - dynamic structure, grows and shrinks with data - most

More information

Chapter 6. Binary Trees. Data Structures and Algorithms in Java

Chapter 6. Binary Trees. Data Structures and Algorithms in Java Chapter 6 Binary Trees Data Structures and Algorithms in Java Objectives Discuss the following topics: Trees, Binary Trees, and Binary Search Trees Implementing Binary Trees Searching a Binary Search Tree

More information

Hierarchical Collections: Trees

Hierarchical Collections: Trees 2501ICT/7421ICTNathan School of Information and Communication Technology Griffith University Semester 1, 2012 Outline Trees 1 Trees 2 3 Hierarchical Collections Tree definition Types of Trees Binary Expressions

More information

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

More information

Lecture 4: Binary Trees

Lecture 4: Binary Trees ECE4050/CSC5050 Algorithms and Data Structures Lecture 4: Binary Trees 1 Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together

More information

Trees and Binary Relations. Debdeep Mukhopadhyay IIT Madras

Trees and Binary Relations. Debdeep Mukhopadhyay IIT Madras Trees and Binary Relations Debdeep Mukhopadhyay IIT Madras Outline Tree : definition and its relation with binary relations Properties Expressions: Balancedness Infix, Postfix and Prefix Expression Trees

More information

Q1 Define searching process? A1 searching is the process of finding an element within the list of elements stored in any order or randomly.

Q1 Define searching process? A1 searching is the process of finding an element within the list of elements stored in any order or randomly. Model Viva Questions for Name of the Lab: Data Structure of lab Common to: CS 4 th sem Title of the Practical: Program to search an element of array using linear search. Q1 Define searching process? A1

More information

C241 Homework 8. Assigned: Apr. 16, 2009 Due: Apr. 22, 2009

C241 Homework 8. Assigned: Apr. 16, 2009 Due: Apr. 22, 2009 C241 Homework 8 Assigned: Apr. 16, 2009 Due: Apr. 22, 2009 As we have seen, a tree is a structure with nodes and edges. Each node is connected to 0 or more child nodes by one edge per child. Trees are

More information

Data Structures Interview / VIVA Questions and Answers This Content is Provided by JNTU World

Data Structures Interview / VIVA Questions and Answers This Content is Provided by JNTU World Data Structures Interview / VIVA Questions and Answers This Content is Provided by 1. What is data structure? The logical and mathematical model of a particular organization of data is called data structure.

More information

Note: The paper consists of two sections. Section A and Section B. Section A is compulsory. In Section B attempt any one question from each unit.

Note: The paper consists of two sections. Section A and Section B. Section A is compulsory. In Section B attempt any one question from each unit. Model Answer B.Tech. (Fourth Semester) Examination 2013 (Information Technology) Data Structure and Programming Methodology (IT2202) Time Allowed: Three hours Maximum Marks: 60. Note: The paper consists

More information

Figure 2 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) }

Figure 2 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) } Applications of the stack: 1. Simple Balanced Parentheses. 2. Converting Decimal Numbers to Binary Numbers. 3. Infix, Prefix and Postfix Expressions. 1- Simple Balanced Parentheses: Use of parentheses

More information

General Trees and Conversion to Binary Trees

General Trees and Conversion to Binary Trees General Trees and Conversion to Binary Trees General trees are those in which the number of subtrees for any node is not required to be,, or. The tree may be highly structured and therefore have 3 subtrees

More information

Trees and Binary Search Trees Chapters 18 and 19. Dynamic data structures. Tree: example. Tree: non-recursive definition.

Trees and Binary Search Trees Chapters 18 and 19. Dynamic data structures. Tree: example. Tree: non-recursive definition. Trees and Binary Search Trees Chapters 18 and 19 Dynamic data structures Linked Lists CS 3358 Summer I 2012 Jill Seaman - dynamic structure, grows and shrinks with data - most operations are linear time

More information

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : 572365, 576681 1

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : 572365, 576681 1 Data Structure By Ajay Raiyani Yogidham, Kalawad Road, Rajkot. Ph : 572365, 576681 1 Linked List 4 Singly Linked List...4 Doubly Linked List...7 Explain Doubly Linked list: -...7 Circular Singly Linked

More information

Trees. Lecture 8 CS2110 Fall 2011

Trees. Lecture 8 CS2110 Fall 2011 Trees Lecture 8 CS2110 Fall 2011 Tree Overview Tree: recursive data structure (similar to list) Each cell may have two or more successors (or children) Each cell has at most one predecessor (or parent)

More information

Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 10.1-10.2 Introduction to trees The data structures we have seen so far

More information

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1 Explain Array in detail One Dimensional Array Simplest data structure that makes use of computed address to locate its elements is the onedimensional array or vector; number of memory locations is sequentially

More information

KALASALINGAM UNIVERSITY

KALASALINGAM UNIVERSITY KALASALINGAM UNIVERSITY (Kalasalingam Academy of Research and Education) DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LAB MANUAL DATA STRUCTURES LABORATORY (CSE 295) 1 INTRODUCTION This manual is organized

More information

SEQUENCE CONTROL. Sequence Control may be categorized into four groups:

SEQUENCE CONTROL. Sequence Control may be categorized into four groups: SEQUENCE CONTROL SEQUENCE CONTROL Control Structure in a PL provides the basic framework within which operations and data are combined into a program and sets of programs. Sequence Control -> Control of

More information

Data Structures. Lecture 6 : Linked List (Part 1)

Data Structures. Lecture 6 : Linked List (Part 1) 0 Data Structures Lecture 6 : Linked List (Part 1) Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University http://bu.edu.eg/staff/esamhalim14 2 If the memory is allocated

More information

AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm

AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm Time: Three Hours] [Maximum Marks: 60 Note Attempts all the questions. All carry equal marks Section A

More information

Recursive Algorithms. Recursive procedure. Recurrence Equation. Data Structures

Recursive Algorithms. Recursive procedure. Recurrence Equation. Data Structures Recursive Algorithms Recursive procedure In this technique, we define a procedure that is allowed to make calls to itself as a subroutine Those calls are meant to solve sub-problems of smaller size Recursive

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 8: Graphs Michael Bader Winter 2011/12 Chapter 8: Graphs, Winter 2011/12 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set

More information

Trees. Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees?

Trees. Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees? Section 11.1 Trees Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees? Solution: G 1 and G 2 are trees both are connected and have no simple

More information

The Stack and Queue Types

The Stack and Queue Types The Stack and Queue Types Lecture 10 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/spring_2015/csc1254.html Programming Principle of the Day Do the simplest thing that could possibly

More information

Outline Trees CSCI Slide 2

Outline Trees CSCI Slide 2 2-3 Tree Outline Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 2 Why care about advanced implementations? Same entries, different insertion sequence: Not good! Would like to keep tree balanced. Slide

More information

CS 146 Exam 1 Grade:

CS 146 Exam 1 Grade: CS 146 Exam 1 Name Grade: The test will be open book, open notes, 75 minute time limit. Please write your answers on the exam sheet. Ten problems, 10 points per problem. 1. Analysis of non-recursive algorithms.

More information

AN EMPIRICAL COMPARISON OF PRIORITY-QUEUE AND EVENT-SET IMPLEMENTATIONS DOUGLAS W. JONES

AN EMPIRICAL COMPARISON OF PRIORITY-QUEUE AND EVENT-SET IMPLEMENTATIONS DOUGLAS W. JONES AN EMPIRICAL COMPARISON OF PRIORITY-QUEUE AND EVENT-SET IMPLEMENTATIONS DOUGLAS W. JONES 1 Basic Priority Queue Operations Enqueue (Insert) Places an item in the priority queue Dequeue (Delete-min) Removes

More information

Module 2 Stacks and Queues: Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which

More information

Module IV. Non Linear Data Structures and their Representation

Module IV. Non Linear Data Structures and their Representation Module IV Non Linear Data Structures and their Representation Tree is a non linear data structure which organizes data in a hierarchical structure. In tree data structure, every individual element is called

More information

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2). CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical

More information

Data Structures and Algorithms V22.0102. Otávio Braga

Data Structures and Algorithms V22.0102. Otávio Braga Data Structures and Algorithms V22.0102 Otávio Braga We use a stack When an operand is read, output it When an operator is read Pop until the top of the stack has an element of lower precedence Then push

More information

CSE 373 Lecture 5: Lists, Stacks, and Queues. We will review: Stack ADT and applications Queue ADT and applications Introduction to Trees

CSE 373 Lecture 5: Lists, Stacks, and Queues. We will review: Stack ADT and applications Queue ADT and applications Introduction to Trees CSE 373 Lecture 5: Lists, Stacks, and Queues We will review: Stack ADT and applications Queue ADT and applications Introduction to Trees Covered in Chapters 3 and 4 in the text 1 Stacks Stack ADT: Same

More information

Homework 8 due Tues 11/16. CLRS (red-black vs. BTrees) CLRS (complexity in t)

Homework 8 due Tues 11/16. CLRS (red-black vs. BTrees) CLRS (complexity in t) Homework 8 due Tues 11/16 CLRS 18.1-5 (red-black vs. BTrees) CLRS 18.2-6 (complexity in t) 1 Chapter 18: B-Trees A B-tree is a balanced tree scheme in which balance is achieved by permitting the nodes

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 8: Graphs Harald Räcke Winter 2015/16 Chapter 8: Graphs, Winter 2015/16 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set E

More information

UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE STUDENT IDENTIFICATION NO UNIVERSITI MALAYSIA SARAWAK 94300 KOTA SAMARAHAN SARAWAK FAKULTI SAINS KOMPUTER & TEKNOLOGI MAKLUMAT (Faculty of Computer Science & Information Technology) Diploma in Multimedia

More information

Algorithms and Data Structures Final Exam 100 Points

Algorithms and Data Structures Final Exam 100 Points Algorithms and Data Structures Exam 100 Points Fill in the Blank (1 point each) 1. After many insertions and deletions in a hash table, it is possible that every unused node is marked as having been deleted.

More information

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan Trees Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.twcgu Tree node (13) degree of a tree (3) height ht of a tree (4) degree of a node leaf (terminal)

More information

COSC242 Lecture 17 B-trees!

COSC242 Lecture 17 B-trees! COSC242 Lecture 17 B-trees! roblem - to maintain a growing search tree without allowing it to get tall and thin like a linked list. We ve used rotations to keep RBTs short and fat. Another approach is

More information

Red Black Tree. A balanced binary search tree

Red Black Tree. A balanced binary search tree Red Black Tree A balanced binary search tree Review Binary Search Tree (BST) is a good data structure for searching algorithm It supports Search, find predecessor, find successor, find minimum, find maximum,

More information

Recursive Code A program that calls itself and stops when a termination condition is achieved.

Recursive Code A program that calls itself and stops when a termination condition is achieved. Recursion and Trees Recursive Code A program that calls itself and stops when a termination condition is achieved. Recursive algorithms Solves a problem by solving one or more of smaller instances of the

More information

CS1201-Data Structures

CS1201-Data Structures SHRI ANGALAMMAN COLLEGE OF ENGINEERING & TECHNOLOGY (An ISO 9001:2008 Certified Institution) SIRUGANOOR,TRICHY-621105. CS1201-Data Structures UNIT I FUNDAMENTALS OF ALGORITHMS 1. Define Algorithm. 2. Define

More information

Paper Full Marks : 75

Paper Full Marks : 75 Paper 3 2008 Full Marks : 75 Paper- 4 2008 Full Marks : 75 Paper - 4 2010 Time: 3 hours Full Marks: 75 Candidates are required to give their answers in their own words as far as practicable. The questions

More information

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

More information

General trees Terminology Representation of trees Tree traversal-

General trees Terminology Representation of trees Tree traversal- UNIT IV TREES Syllabus General trees Terminology Representation of trees Tree traversal- Binary tree Representation Expression tree Binary tree traversal - Threaded Binary Tree - Binary Search Tree Construction

More information

Chapter 8, Graph Theory

Chapter 8, Graph Theory ECS 20 Chapter 8, Graph Theory 1. Introduction, Data Structures 1.1. The atom of computer memory is a byte. Each byte is capable of holding 256 different values, 0-255. Each byte has its own address. The

More information

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748 Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Review 2 How to compare big-ohs? u We say that f(n) = O(g(n)) if There exists positive integer n 0, and A positive constant

More information

Data Structures UNIT III. Model Question Answer

Data Structures UNIT III. Model Question Answer Data Structures UNIT III Model Question Answer Q.1. Define Stack? What are the different primitive operations on Stack? Ans: Stack: A stack is a linear structure in which items may be added or removed

More information

CS 2321 Data Structures and Algorithms. Trees. Dr. Mahdi Khemakhem

CS 2321 Data Structures and Algorithms. Trees. Dr. Mahdi Khemakhem CS 2321 Data Structures and Algorithms Trees Dr. Mahdi Khemakhem Outlines 1. Definitions and Terminologies 2. Binary Trees 3. Binary Search Trees 4. AVL-Trees CS 2321: D.S.A (-Trees-) 2 1. Defintions and

More information

Trees. Task Tree. Directory Tree. 22c:19 Fall 2009

Trees. Task Tree. Directory Tree. 22c:19 Fall 2009 Trees 22c:19 Fall 2009 Task Tree Directory Tree Good for directed search tasks subtree filtering (+/ ) Not good for learning structure No attributes Apx 50 items visible Lose path to root for deep nodes

More information

A list of type T. Why do you need a list? EECS 281: Data Structures and Algorithms. Stacks and Queues. Remember things you have to do

A list of type T. Why do you need a list? EECS 281: Data Structures and Algorithms. Stacks and Queues. Remember things you have to do EECS 281: Data Structures and Algorithms Stacks and Queues A list of type T Logical sequential storage an ordered collection of objects of type T each object in the list is called an element Must have:

More information

Tree. Discrete Mathematics (MA 2333) Faculty of Science Telkom Institute of Technology Bandung - Indonesia

Tree. Discrete Mathematics (MA 2333) Faculty of Science Telkom Institute of Technology Bandung - Indonesia Tree Discrete Mathematics (MA 2333) Faculty of Science Telkom Institute of Technology Bandung - Indonesia Definition A connected graph that contains no simple circuits is called a tree Trees were used

More information

Full and Complete Binary Trees

Full and Complete Binary Trees Full and Complete Binary Trees Binary Tree Theorems 1 Here are two important types of binary trees. Note that the definitions, while similar, are logically independent. Definition: a binary tree T is full

More information

Elementary Data Structures Stacks, Queues, Lists, and Related Structures

Elementary Data Structures Stacks, Queues, Lists, and Related Structures Elementary Data Structures Stacks, Queues, Lists, and Related Structures Stacks, lists and queues are primitive data structures fundamental to implementing any program requiring data storage and retrieval.

More information

SFU CMPT Lecture: Week 3

SFU CMPT Lecture: Week 3 SFU CMPT-307 2008-2 1 Lecture: Week 3 SFU CMPT-307 2008-2 Lecture: Week 3 Ján Maňuch E-mail: jmanuch@sfu.ca Lecture on May 20, 2008, 5.30pm-8.20pm SFU CMPT-307 2008-2 2 Lecture: Week 3 Important basic

More information

Review questions for Chapter 8

Review questions for Chapter 8 Answer first, then check at the end. Review questions for Chapter 8 True/False 1. The properties of an abstract data type are tightly related to the implementation of that data type. 2. The two general

More information

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

R13. II B. Tech I Semester Supplementary Examinations, June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, June - 2015 DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) Time: 3 hours Max. Marks: 70 Note: 1. Question Paper consists of two parts (Part-A

More information

INF 2220: algorithms and data structures Series 1

INF 2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk D. Karabeg, I. Yu INF 2220: algorithms and data structures Series 1 Høst 2014 19. 08. 2014 Topic: Function growth & estimation of running time, trees (Exercises

More information

channel: 41 session: cs2420 STACKS & QUEUES

channel: 41 session: cs2420 STACKS & QUEUES channel: 41 session: cs2420 STACKS & QUEUES cs2420 Introduction to Algorithms and Data Structures Fall 2016 1 channel: 41 session: cs2420 administrivia 2 channel: 41 session: cs2420 -assignment 6 on linked

More information

Decrease-and-Conquer. Dr. Yingwu Zhu

Decrease-and-Conquer. Dr. Yingwu Zhu Decrease-and-Conquer Dr. Yingwu Zhu Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance to obtain solution

More information

Data Structure. Lecture 3

Data Structure. Lecture 3 Data Structure Lecture 3 Data Structure Formally define Data structure as: DS describes not only set of objects but the ways they are related, the set of operations which may be applied to the elements

More information

Heidi C. Ellis and Gerard C. Weatherby. Trees: Overview

Heidi C. Ellis and Gerard C. Weatherby. Trees: Overview Trees: Overview Heidi. Ellis and Gerard. Weatherby inary Trees Tree Traversal inary Search Trees Expression Trees alancing inary Search Trees * Red black * VL * Weight balancing ata compression General

More information

Chapter 12: Advanced Implementations of Tables

Chapter 12: Advanced Implementations of Tables Chapter 12: Advanced Implementations of Tables Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano Balanced Search Trees The efficiency of the binary search tree implementation

More information

Multi-Way Search Trees (B Trees)

Multi-Way Search Trees (B Trees) Multi-Way Search Trees (B Trees) Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children. If n

More information

Standard Version of Starting Out with C++, 4th Edition. Binary Trees. Copyright 2003 Scott/Jones Publishing

Standard Version of Starting Out with C++, 4th Edition. Binary Trees. Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Binary Trees Copyright 2003 Scott/Jones Publishing Topics Definition and Application of Binary Trees Binary Search Tree Operations Template Considerations

More information

Chapter 20 -Part 3. Stacks

Chapter 20 -Part 3. Stacks Chapter 20 -Part 3 Stacks 1 Stacks A stack is a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top of the stack. Stack behavior is knows as LIFO.

More information

Binary Search Trees (BST) Dr. Yingwu Zhu

Binary Search Trees (BST) Dr. Yingwu Zhu Binary Search Trees (BST) Dr. Yingwu Zhu Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, x n Assume == and < operators defined for the type Linear search

More information

Question minutes. Which one of the following is the worst case time complexity of inserting an object into a binary search tree of n nodes?

Question minutes. Which one of the following is the worst case time complexity of inserting an object into a binary search tree of n nodes? UCS406 QUIZ1 for CML, CA 10 minutes - 1 Which one of the following is the worst case time complexity of inserting an object into a binary search tree of n nodes? O(1) O(log n) O(n) O(n log n) - 2 Which

More information

Binary Trees and Binary Search Trees C++ Implementations

Binary Trees and Binary Search Trees C++ Implementations Binary Trees and Binary Search Trees C++ Implementations Learning Goals: Apply basic tree definitions to classification problems. Describe the properties of binary trees, binary search trees, and more

More information

Data Structures Using C++ 2E. Chapter 7 Stacks

Data Structures Using C++ 2E. Chapter 7 Stacks Data Structures Using C++ 2E Chapter 7 Stacks Linked Implementation of Stacks Disadvantage of array (linear) stack representation Fixed number of elements can be pushed onto stack Solution Use pointer

More information

ABSTRACT DATA TYPES; LISTS & TREES

ABSTRACT DATA TYPES; LISTS & TREES ABSTRACT DATA TYPES; LISTS & TREES Lecture 10 CS2110 Fall 2014 References and Homework 2 Text: Chapters 10, 11 and 12 Homework: Learn these List methods, from http://docs.oracle.com/javase/7/docs/api/java/util/list.html

More information

The Tree ADT. The Tree ADT. Defining the Tree ADT Recursively. Class #14: Introduction to Trees T 2. T k T 1

The Tree ADT. The Tree ADT. Defining the Tree ADT Recursively. Class #14: Introduction to Trees T 2. T k T 1 The Tree DT lass #14: ntroduction to Trees Software Design (S 340): M. llen, 25 Feb. 15! Trees are collections of objects, related in a branching, ordered fashion! an represent a variety of real-world

More information

Fundamentals of Data Structures Trees Example test questions for the course

Fundamentals of Data Structures Trees Example test questions for the course 2008 November Tree Questions Page 1 of 6 Fundamentals of Data Structures Trees Example test questions for the course These questions or similar questions have been used in tests in previous years. The

More information

Trees Terminology. A tree is a collection of elements (nodes) Each node may have 0 or more successors

Trees Terminology. A tree is a collection of elements (nodes) Each node may have 0 or more successors Trees Terminology A tree is a collection of elements (nodes) Each node may have 0 or more successors Except for one node, called the root, each node has exactly one predecessor Links from node to its successors

More information

Nearly Complete Binary Trees and Heaps

Nearly Complete Binary Trees and Heaps Nearly Complete Binary Trees and Heaps DEFINITIONS: i) The depth of a node p in a binary tree is the length (number of edges) of the path from the root to p. ii) The height (or depth) of a binary tree

More information

Lecture 8: Priority Queue

Lecture 8: Priority Queue Lecture 8: Priority Queue CSC2100 Data Structure Yufei Tao CSE department, CUHK March 12, 2011 In this lecture, we will learn a structure called the priority queue, which is also known as the heap. It

More information

B-TREES B-tree example

B-TREES B-tree example B-TREES B-trees are balanced search trees designed to work well on magnetic disks or other direct-access secondary storage devices. B-tree nodes may have many children, from a handful to thousands. That

More information

Binary Trees and Binary Search Trees C++ Implementations

Binary Trees and Binary Search Trees C++ Implementations Binary Trees and Binary Search Trees C++ Implementations Learning Goals: Apply basic tree definitions to classification problems. Describe the properties of binary trees, binary search trees, and more

More information

William Stallings. Copyright 2012 L.1 STACKS... 2 L.2 STACK IMPLEMENTATION... 4 L.3 EXPRESSION EVALUATION... 7

William Stallings. Copyright 2012 L.1 STACKS... 2 L.2 STACK IMPLEMENTATION... 4 L.3 EXPRESSION EVALUATION... 7 APPENDIX O STACKS William Stallings Copyright 2012 L.1 STACKS... 2 L.2 STACK IMPLEMENTATION... 4 L.3 EXPRESSION EVALUATION... 7 Supplement to Computer Organization and Architecture, Ninth Edition Prentice

More information

Binary Trees. COMPSCI 355 Fall 2016

Binary Trees. COMPSCI 355 Fall 2016 Binary Trees COMPSCI 355 Fall 2016 Graphs A graph consists of objects called nodes and a binary relation defined on nodes. A B D E C G F Graphs A path is a sequence of nodes connected by edges. A B PATH:

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Linear Data Structures Department of Computer Science University of Maryland, College Park Linear data structures General properties Implementations Array Linked

More information

Binary Search Tree (Part 1)

Binary Search Tree (Part 1) Binary Search Tree (Part 1) Yufei Tao ITEE University of Queensland This and the next lecture will be devoted to the most important data structure of this course: the binary search tree (BST). This is

More information

Binary Trees. Chapter Trees. Goals

Binary Trees. Chapter Trees. Goals Chapter 20 Binary Trees The data structures presented so far are predominantly linear. Every element has one unique predecessor and one unique successor (except the first and last elements). Arrays, and

More information