Cinda Heeren / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Marc Smith and Jim Ten Eyck
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Binary Search Trees Chapter 7 Objectives
ADT description Implementations
Heap Chapter 9 Objectives Define and implement heap structures
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
CS 302 Data Structures Trees.
Review: recursion Tree traversals
Trees.
Binary Search Tree Chapter 10.
Cinda Heeren / Geoffrey Tien
COMP 103 Binary Search Trees.
Revised based on textbook author’s notes.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Search Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
ADT Heap data structure
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
B-Tree Insertions, Intro to Heaps
CMSC 341 Binary Search Trees.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Hassan Khosravi / Geoffrey Tien
CMSC 202 Trees.
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
Trees Chapter 10.
CMSC 341 Binary Search Trees.
Chapter 20: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Tree traversals BST properties Search Insertion
CMSC 341 Binary Search Trees 2/21/2006.
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Cinda Heeren / Geoffrey Tien Binary Search Trees Properties Insertion October 10, 2017 Cinda Heeren / Geoffrey Tien

Before we begin... Another type of tree traversal We have seen pre-order, in-order, post-order traversals What about a traversal that visits every node in a level before working on the next level? level-order traversal 41 33 87 21 74 36 45 78 25 Use some ADT to support this? October 10, 2017 Cinda Heeren / Geoffrey Tien

Motivation for an efficient lookup Dictionary ADT Stores values associated with user-specified keys Values may be any (homogenous) type Keys may be any (homogenous) comparable type Dictionary operations Create Destroy Insert Find Remove Stumpjumper The favourite baby of VanCity planners Z125 Pro Fun in the sun! GL1800 Quiet comfort Insert Feet Useful for something, presumably Find(Z125 Pro) Z125 Pro Fun in the sun! October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Search/Set ADT Stores only key values Keys may be any homogenous comparable type Tests quickly for membership Operations Create Destroy Insert Find Remove Chips Pizza Cookie Popcorn KD Peanuts Chocolate Insert Jolly Rancher Find(Nachos) NOT FOUND October 10, 2017 Cinda Heeren / Geoffrey Tien

Data structures for Dictionary ADT Naïve implementations, complexity Search Insert Remove Linked list Unsorted array Sorted array Ordered linked list October 10, 2017 Cinda Heeren / Geoffrey Tien

Binary search tree property A binary search tree is a binary tree with a special property For all nodes in the tree: All nodes in a left subtree have labels less than the label of the subtree's root All nodes in a right subtree have labels greater than or equal to the label of the subtree's root Binary search trees are fully ordered October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST example 17 13 27 9 16 20 39 11 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST inOrder traversal inOrder(nd->leftchild); inOrder traversal on a BST retrieves data in sorted order visit(nd); 5 inOrder(nd->rightchild); 17 3 7 inOrder(left) inOrder(left) 13 27 visit visit inOrder(right) inOrder(right) 1 4 6 8 inOrder(left) inOrder(left) inOrder(left) 9 visit 16 20 visit 39 visit inOrder(right) inOrder(right) inOrder(right) inOrder(left) 2 visit inOrder(right) inOrder(left) 11 visit inOrder(right) October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST implementation Binary search trees can be implemented using a reference structure Tree nodes contain data and two pointers to nodes Node* leftchild data Node* rightchild Data to be stored in the tree (usually an object) References or pointers to other tree Nodes October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is greater than the value in the node search its right subtree Otherwise return true, (or a pointer to the data, or …) How many comparisons? One for each node on the path Worst case: height of the tree + 1 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST search example search(27); 17 27 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST search example search(16); 17 13 16 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST search example search(12); 17 13 9 11 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST insertion The BST property must hold after insertion Therefore the new node must be inserted in the correct position This position is found by performing a search If the search ends at the (null) left child of a node make its left child refer to the new node If the search ends at the right child of a node make its right child refer to the new node The cost is about the same as the cost for the search algorithm, O(height) October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 41 54 79 43 10 23 37 44 53 59 96 7 12 30 43 57 91 97 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien BST insertion example Create new BST 3 Insert 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Find Min, Find Max Find minimum: From the root, keep following left child links until no more left child exists Find maximum: From the root, follow right child links until no more right child exists 43 18 68 12 7 9 33 52 56 67 27 39 50 21 October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Removal Notice that insertion into a BST always adds a new leaf node so the structure of the tree above the inserted node is not affected but generally, nodes existing in the tree may be internal nodes or leaf nodes Removal can be requested at any node in the tree, and removing an internal node may create difficulties with enforcing all the necessary structural properties (tree, binary tree, BST) more complicated logic required to maintain BST properties after removal – next class! October 10, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise Elements from a sorted list (ascending order) are inserted into a BST one by one. Which traversal method(s) will return the list in ascending order? descending order? neither ascending nor descending order? Write recursive and iterative implementations of findMin, findMax Assume key type is int Assume there is a member attribute Node* root int findMin(Node* nd); Write iterative and recursive implementations of insert void insert(int key); Node* insert(int key, Node* nd); October 10, 2017 Cinda Heeren / Geoffrey Tien

Readings for this lesson Koffman Chapter 8.3 – 8.4 (Binary trees, binary search trees) October 10, 2017 Cinda Heeren / Geoffrey Tien