Each item in the list is examined in the order it occurs in the list until the desired item is found or the end of the list is reached.

Similar documents
Binary Search Trees (BST)

Data Structure with C

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

Converting a Number from Decimal to Binary

Ordered Lists and Binary Trees

Binary Search Trees CMPSC 122

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

DATA STRUCTURES USING C

TREE BASIC TERMINOLOGIES

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Binary Trees and Huffman Encoding Binary Search Trees

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

Data Structures and Algorithms

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

From Last Time: Remove (Delete) Operation

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

The ADT Binary Search Tree

Data Structure [Question Bank]

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Lecture Notes on Binary Search Trees

Binary Heap Algorithms

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Parallelization: Binary Tree Traversal

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Chapter 14 The Binary Search Tree

How To Create A Tree From A Tree In Runtime (For A Tree)

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

10CS35: Data Structures Using C

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

PES Institute of Technology-BSC QUESTION BANK

Data Structures. Level 6 C Module Descriptor

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

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

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Data Structures Using C++

Lecture 12 Doubly Linked Lists (with Recursion)

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures and Algorithms(5)

Why Use Binary Trees?

Lecture Notes on Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Chapter 8: Binary Trees

Analysis of Algorithms I: Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

Full and Complete Binary Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures Fibonacci Heaps, Amortized Analysis

Heaps & Priority Queues in the C++ STL 2-3 Trees

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Big Data and Scripting. Part 4: Memory Hierarchies

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Exam study sheet for CS2711. List of topics

Sample Questions Csci 1112 A. Bellaachia

DATABASE DESIGN - 1DL400

Lecture 11 Array of Linked Lists

OPTIMAL BINARY SEARCH TREES

Introduction to data structures

Symbol Tables. Introduction

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Data Structures. Algorithm Performance and Big O Analysis

Chapter 13: Query Processing. Basic Steps in Query Processing

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Alex. Adam Agnes Allen Arthur

CSE 326: Data Structures B-Trees and B+ Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Algorithms and Data Structures

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

LINKED DATA STRUCTURES

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

CS473 - Algorithms I

6 March Array Implementation of Binary Trees

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Physical Data Organization

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Binary Heaps. CSE 373 Data Structures

Binary Coded Web Access Pattern Tree in Education Domain

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Binary Search Trees 3/20/14

2. FINDING A SOLUTION

arrays C Programming Language - Arrays

Java Software Structures

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Transcription:

Selection / linear search Each item in the list is examined in the order it occurs in the list until the desired item is found or the end of the list is reached. start 1 3 5 6 8 12 list struct email **list; struct email *start; *list = start; list = &start; PrProg10a.1 Sequential / linear search (code) preprocessor directives function prototypes int main(){ struct email *start, *temp; int messno; createlist(&start); Struct email { struct email *fwd; int message_no; int delete; char [40]; printf( Enter the message No. for the search.\n ); scanf( %d,&messno); temp = searchlist(start,messno); if (temp) printf( The list is empty or the message is not in the list. ); else printf( The of the message is %s.\n,temp->); return 0; PrProg10a.2

Searching a list -- searchlist() struct email *searchlist (struct email *p, int mno){ struct email *temp; temp = p; while (temp!= NULL && (temp->message_no!= n)) { temp = temp->fwd; return temp; or struct email *searchlist (struct email *p, int mno){ struct email *temp; for(temp=p; (temp!= NULL)&&(temp->message_no!= n); temp =temp->fwd){ ; return temp; PrProg10a.3 Time complexity analysis Active operation: Comparison of target message No and the message No in a node. There are n nodes in a list. Time complexity function f(n). The best case scenario: search for message No.1. Or the list is empty. f(n)_best = 1 The worst case scenario: search for message No.12 or message No.7. f(n)_worst = n Average f(n)_avg = n/2 PrProg10a.4

Binary search search Starting with an ordered array, the desired item is first compared to the middle element in the list (for lists with even number of elements, either of the two middle elements can be used). array 1 3 5 6 8 12 PrProg10a.5 Binary search an array of emails (code) preprocessor directives function prototypes int main(){ struct email *array; int messno, temp; createarray(array); Struct email { int message_no; char [40]; printf( Enter the message No. for the search.\n ); scanf( %d,&messno); temp = searcharray(start,messno); if (temp==-1) printf( the message is not in the array. ); else printf( The of the message is %s.\n,array[temp].); return 0; PrProg10a.6

Searching an array -- searcharray() /* Pre: The contiguous array has been created Post: If an entry in the array has key equal to the target, then the function returns the index of the entry (success). Otherwise the function returns 1 (target is not found. */ int searcharray (struct email *p, int mno, int size){ int bottom, top, middle=-1; bottom = 0; top = size-1; while (bottom <= top){ middle = (top + bottom)/2; if (mno == p[middle].messageno) return index; else if (mno < p[middle].messageno) top = middle-1; else bottom = middle +1; return 1; PrProg10a.7 Time complexity analysis Active operation: Comparison of target message No and the message No in a node. There are n nodes in the array. Time complexity function f(n). The best case scenario: search for message No.5 (for the implementation). f(n)_best = 1 The worst case scenario: search for message No.12 or message No.7. f(n)_worst = log 2 n Average f(n)_avg=log 2 n-1 PrProg10a.8

Comparison of binary and sequential search 450 400 350 300 250 200 n log(n,2) 150 100 50 0 1 41 81 121 161 201 241 281 321 361 PrProg10a.9 More examples Free trees rooted trees PrProg10a.10

Decisions Up to 2 children and interior nodes containing data ---- binary trees Up to M children and interior nodes containing only search info ---- B-trees Other decisions ---- other types of trees PrProg10a.11 Binary trees Maximum number of children is 2. Interior nodes contain actual data. Definition: A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root. A node having no a parent node ---- rootnode A node having no a child node ---- leafnode A node having both a parent node and a child node ---- interior node PrProg10a.12

Binary trees (cont.) Struct BinNode *treestart; Empty tree : treestart NULL Tree having a single node : treestart -> leftchild = treestart -> rightchild = NULL treestart Normal binary tree : treestart treestart PrProg10a.13 Traversal of binary trees Traversal moving through all the nodes of a binary tree, visiting each one in turn. The action we shall take when we visit each node will depend on the application. Standard traversal orders: VLR (preorder) LVR (inorder) LRV (postorder) Where V,L,and R indicate the task of visiting a node, traversing the left subtree and traversal the right subtree, respectively. PrProg10a.14

Traversal of binary trees (code) Void travinorder (BinNode *root, void (*Vist)(struct trec x)) { if (root) { travinorder(root->leftchild, Visit); Visit(root -> entry); travinorder(root -> rightchild, Visit); Note: a function Visit handling the action in a visit to a node, we can declare a pointer Visit pointing to the function as: void (*Visit)(struct trec x); PrProg10a.15 Recursion Recursion is an important programming mechanism. A definition of a function or other objects refers to the function or the object itself. ---- recursive definition A call to a recursive function leads to a recursive function call, in which the called function calls itself. There are two cases in a recursive definition: Base rule describes the term to be defined directly, and Recursive rule contains the term to be defined in its own definition. The base rule is for the termination or stopping. The recursive rule is converting the big problem to a similar but simpler problem. PrProg10a.16

Examples of recursive definitions Int fac(int n) { if (n==0) return 1 else return (fac(n-1)*n) Int sum(int n) { if (n==0) return 0; else return (sum(n-1)+n); fac(5) = fac(4)*5 = fac(3)*4*5 = fac(2)*3*4*5 = fac(1)*2*3*4*5 = fac(0)*1*2*3*4*5 = 1*1*2*3*4*5 Can you try sum(5)? PrProg10a.17 More example of recursive function call Void travinorder (BinNode *root, void (*Vist)(struct trec x)); A note about this function is in the notice board for this lecture. cdecl> explain void (*vist)(struct trec); declare vist as pointer to function (struct trec) returning void cdecl> declare visit as pointer to function(x as struct trec) returning void; void (*visit)(struct trec x) PrProg10a.18

Binary search tree A binary search tree is a binary tree that is either empty or in which every node contains a key and satisfies the following conditions: 1. The key in the left child of a node (if exists) is less than the key in its parent node. 2. The key in the right child of a node (if exists) is greater than the key in its parent node. 3. The left and right subtrees of the root are again binary search trees. PrProg10a.19 An EXAMPLE of a binary search tree 30 20 50 10 23 15 21 26 PrProg10a.20

SEARCH EXAMPLES Consider the tree below, search for keys 21, 40 p 30 20 50 10 23 15 21 26 PrProg10a.21 Time complexity analysis for searing a B-tree Active operation: Comparison of target key value and the key value in a node. There are n nodes in the B-tree with M being the maximum number of children for each node. Time complexity function f(n). For k entries per node, the average hits f(n)_avg = log k (n). PrProg10a.22