Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Search Trees1 Part-F1 Binary Search Trees   
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
Binary Search Trees (BST)
Summary. Quiz #1 Program Introduction Complexity notion ADT – Stack – Queue – Tree (basics) CDS – Array – SLL.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
COMP 53 – Week Fourteen Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
Data Structures and Algorithms
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Multiway search trees and the (2,4)-tree
Trees.
Binary Search Tree Chapter 10.
Binary Search Trees < > = Binary Search Trees
Tree data structure.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Wednesday, April 18, 2018 Announcements… For Today…
Searching.
Tree data structure.
Binary Search Trees < > = Binary Search Trees
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Data Structures and Algorithms
Binary Tree Application Operations in Heaps
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Data Structures and Algorithms
Data Structures and Algorithms
Lecture 12 CS203 1.
Searching CLRS, Sections 9.1 – 9.3.
Dictionaries < > = /17/2019 4:20 PM Dictionaries
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Data Structures: Searching
Binary Search Trees < > =
Data Structures and Algorithms
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
CSC 143 Binary Search Trees.
1 Lecture 10 CS2013.
Yan Shi CS/SE 2630 Lecture Notes
AVL Tree Chapter 6 (cont’).
Search Sorted Array: Binary Search Linked List: Linear Search
1 Lecture 13 CS2013.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS 261 – Data Structures AVL Trees.
Presentation transcript:

Data Structures and Algorithms PLSD210(ii) Searching

Searching Sequential Searches Binary search Time is proportional to n We call this time complexity O(n) Pronounce this “big oh” of n Both arrays (unsorted) and linked lists Binary search Sorted array Time proportional to log2 n Time complexity O(log n)

Searching - Binary search Creating the sorted array AddToCollection adds each item in correct place Find position c1 log2 n Shuffle down c2 n Overall c1 log2 n + c2 n or c2 n Each add to the sorted array is O(n) Can we maintain a sorted array with cheaper insertions? Dominant term

Trees Binary Tree Consists of Node Left and Right sub-trees Both sub-trees are binary trees

Trees Binary Tree Consists of Node Left and Right sub-trees Both sub-trees are binary trees Note the recursive definition! Each sub-tree is itself a binary tree

Trees - Implementation Data structure struct t_node { void *item; struct t_node *left; struct t_node *right; }; typedef struct t_node *Node; struct t_collection { Node root; …… };

Trees - Implementation Find extern int KeyCmp( void *a, void *b ); /* Returns -1, 0, 1 for a < b, a == b, a > b */ void *FindInTree( Node t, void *key ) { if ( t == (Node)0 ) return NULL; switch( KeyCmp( key, ItemKey(t->item) ) ) { case -1 : return FindInTree( t->left, key ); case 0: return t->item; case +1 : return FindInTree( t->right, key ); } void *FindInCollection( collection c, void *key ) { return FindInTree( c->root, key ); Less, search left Greater, search right

Trees - Implementation Find key = 22; if ( FindInCollection( c , &key ) ) …. n = c->root; FindInTree( n, &key ); FindInTree(n->right,&key ); FindInTree(n->left,&key ); return n->item;

Trees - Performance Find Complete Tree Height, h Number of nodes, h Nodes traversed in a path from the root to a leaf Number of nodes, h n = 1 + 21 + 22 + … + 2h = 2h+1 - 1 h = floor( log2 n )

Trees - Performance Find Complete Tree Since we need at most h+1 comparisons, find in O(h+1) or O(log n) Same as binary search

Lecture 2 & 3 - Summary Arrays Linked List Trees Add Delete Find Simple, fast Inflexible O(1) O(n) inc sort O(n) O(logn) binary search Linked List Simple Flexible O(1) sort -> no adv O(1) - any O(n) - specific O(n) (no bin search) Trees Still Simple Flexible O(log n)

Trees - Addition Add 21 to the tree We need at most h+1 comparisons Create a new node (constant time) add takes c1(h+1)+c2 or c log n So addition to a tree takes time proportional to log n also Ignoring low order terms

Trees - Addition - implementation static void AddToTree( Node *t, Node new ) { Node base = *t; /* If it's a null tree, just add it here */ if ( base == NULL ) { *t = new; return; } else if( KeyLess(ItemKey(new->item),ItemKey(base->item)) ) AddToTree( &(base->left), new ); AddToTree( &(base->right), new ); } void AddToCollection( collection c, void *item ) { Node new, node_p; new = (Node)malloc(sizeof(struct t_node)); /* Attach the item to the node */ new->item = item; new->left = new->right = (Node)0; AddToTree( &(c->node), new );

Trees - Addition Find c log n Add c log n Delete c log n Apparently efficient in every respect! But there’s a catch ………..

Trees - Addition Take this list of characters and form a tree A B C D E F ??

Trees - Addition Take this list of characters and form a tree A B C D E F In this case Find Add Delete