CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Similar documents
Data Structures in Java

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CMSC 341 Lecture 10 Binary Search Trees

Binary Trees, Binary Search Trees

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

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

ITI Introduction to Computing II

Binary Search Trees. See Section 11.1 of the text.

ITI Introduction to Computing II

Programming II (CS300)

CS F-11 B-Trees 1

Trees, Binary Trees, and Binary Search Trees

Generic BST Interface

CMSC 341. Binary Search Trees CMSC 341 BST

Spring 2018 Mentoring 8: March 14, Binary Trees

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

CS102 Binary Search Trees

Data Structures and Algorithms

JAVA NOTES DATA STRUCTURES

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CmpSci 187: Programming with Data Structures Spring 2015

CS302 - Data Structures using C++

Data Structures and Algorithms

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Priority Queues. 04/10/03 Lecture 22 1

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

INF2220: algorithms and data structures Series 1

CS 350 : Data Structures Binary Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

The Binary Search Tree ADT

CS 315 Data Structures mid-term 2

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

CS350: Data Structures Binary Search Trees

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

protected BinaryNode root; } 02/17/04 Lecture 11 1

Binary Search Trees. Chapter 21. Binary Search Trees

CIS 121. Binary Search Trees

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS24 Week 8 Lecture 1

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

TREES Lecture 12 CS2110 Spring 2018

Trees, Part 1: Unbalanced Trees

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

Priority Queues and Huffman Trees

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

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

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

CS 206 Introduction to Computer Science II

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

CMSC 341 Leftist Heaps

Binary Tree Applications

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

TREES Lecture 10 CS2110 Spring2014

void insert( Type const & ) void push_front( Type const & )

TREES. Trees - Introduction

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

Binary Search Tree. Revised based on textbook author s notes.

COMP : Trees. COMP20012 Trees 219

A set of nodes (or vertices) with a single starting point

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CMSC 341 Lecture 15 Leftist Heaps

Lecture 23: Binary Search Trees

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

TREES Lecture 12 CS2110 Fall 2016

Module 9: Binary trees

STUDENT LESSON AB30 Binary Search Trees

CMSC 341 Lecture 15 Leftist Heaps

CS 171: Introduction to Computer Science II. Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Binary Trees. Examples:

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Chapter 20: Binary Trees

CS302 - Data Structures using C++

Recall: Properties of B-Trees

ECE 242. Data Structures

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Data Structures And Algorithms

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

CS S-06 Binary Search Trees 1

Associate Professor Dr. Raed Ibraheem Hamed

Module 8: Binary trees

COM S 211/ENGRD 211 May 15, 2003

Unit 9 Practice Test (AB27-30)

Transcription:

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS: OVERVIEW: motivation naive tree search sorting for trees and binary trees new tree classes search insert delete 1. Motivation 1.1 Search Structure continuing did linear; now want hierarchical so, use trees 1.2 Search Trees special trees organized for searching focus on binary search trees because of binary tree focus advanced topics later: balanced trees 1. Interface to implement interface SearchStructure { void insert(object o); void delete(object o); boolean search(object o); int size(); 1 2 2. Searching A Tree 2.1 Assume built at random (no sorting) 2.2 Tree Traversal visit each node and do something with it depth-first: pre, post, in (for binary trees) breadth-first (also, level-order): each level at a time 2. Searching could do a traversal for search expensive: might have to search every node 2. Example of complete traversal Algorithm: - if current node is null, return false - if current node has data, return true - search left branch and search right branch; return result of checking if data is at any node Use CONDITIONAL OR ( ) to help Code: // In BinaryNode class: public boolean naivefind(object o,binarynode n) { if (n==null) return false; if (o.equals(n.item)) return true; return naivefind(o,n.left) naivefind(o,n.right);

2. Improvements use parent links (no recursion needed) sort the tree, as we did with arrays (rest of lecture!) 2.6 More info on traversal: free DS&A! http://www.brpreiss.com/books/opus/html/ see Chap (Tree Traversal) 2. General Trees? using binary trees (see Lecture 18) searching general tree anyway? (Preiss: M-Way search tree in 10.6) 6. Binary Search Tree.1 Properties of BST: For given data at a node (root, root of subtree), - all nodes with data smaller than data are stored in the left subtree - all nodes with data bigger than data are stored in the right subtree What about nodes with data equal to data? no repeats. Binary Search Tree Intuition sort a list grab middle of list (search halves instead of whole) middle becomes root of tree sublists to left and right of root flop down detach the sublists and repeat process of grab & lift hook roots of the sublists to the previous root repeat until no more elements 6 BT? 2 6 BT? 2 8

. Stubbed Out BST abstract public class BinarySearchTree implements SearchStructure { private BinaryNode root; // more fields? public BinarySearchTree( ) { root = null; public void insert(object o); public void delete(object o); public boolean search(object o); public int size(); // helper methods? // more methods? 6. Search 6.1 Algorithm: if tree is empty, return false if root object == search object, return true if root object < search object, search right subtree if root object > search object, search left subtree 6.2 Code: public boolean search(object o) { BinaryNode n = mysearch(o,root); // no empty tree, double check data: return ( n!=null && n.getitem().equals(o) ); // search helper: private BinaryNode mysearch(object o, BinaryNode r) { if (r == null) return null; int test = ((Comparable) r.getitem()).compareto(o); if (test > 0) return mysearch(o, r.getleft()); if (test < 0) return mysearch(o, r.getright()); return r; 10. Search for Max.1 Algorithm: If empty, return null Check right: if empty, return root value; otherwise, find rightmost leaf on the rightmost subtree.2 Code: public Comparable findmax() { return findmax(root); public Comparable findmax(binarynode r) { if (r==null) return null; if (r.getright()==null) return (Comparable) r.getitem(); return findmax(r.getright()); 8. Checking if Tree is BST 8.1 Algorithm: If tree is empty or leaf -> BST Else, internal node: - compute smallest and largest values in left and right subtrees - check if subtrees are also BSTs - BST if both subtress are BSTs and largest object in left subtree is < root object and smallest object in right subtree is > root object 8.2 Advice perform post-order walk of tree: 6 6 8 60 80 11 12

. Insertion.1 Algorithm: Search for object o in BST (assume no dups!) if at leaf (and empty root) make a new node with o, if o < subtree root item, insert into left subtree, if o > subtree root item, insert into right subtree.2 Code: public void insert (Object o) { root = insert(o,root); size++;. Alternative Algorithm Search for o during search, set these cursors/fingers: - current: node where search ends - previous: parent of current node if at leaf or empty tree, make new node with o insert at previous node: based on o and root s item, - insert left or - insert right see testbst.java for original version private BinaryNode insert(object o, BinaryNode r) { if (r == null) r = new BinaryNode(o); if (((Comparable) r.getitem()).compareto(o) > 0) r.setleft(insert(o,r.getleft())); if (((Comparable) r.getitem()).compareto(o) < 0) r.setright(insert(o,r.getright())); return r; // actually, not good (no dups!) 1 1. Example (see TestBST.java) public class TestBST { public static void main( String [ ] args ) {. Session Note: I modified totree() in BinaryNode! BST bst = new BST(); BinaryNode b = new BinaryNode(new Integer()); bst.setroot(b); BinaryNode b2 = new BinaryNode(new Integer(2)); BinaryNode b = new BinaryNode(new Integer()); BinaryNode b = new BinaryNode(new Integer()); BinaryNode b6 = new BinaryNode(new Integer(6)); BinaryNode b = new BinaryNode(new Integer()); b.setleft(b); b.setright(b6); b.setleft(b2); b.setright(b); b6.setright(b); bst.insert(new Integer(0)); bst.insert(new Integer(1)); bst.insert(new Integer()); bst.insert(new Integer(8)); bst.insert(new Integer(10)); System.out.println(bst.toTree()); 2 0 1 6 8 10 1 16

.6 Modified totree now Tree-i-fying leaves can distinguish between left and right subtrees code could be cleaned up Code: public String totree(string blank,string spacing) { String s = "" + item + "\n"; if (left == null) s += spacing + "\n"; s += spacing + left.totree(blank, blank+spacing); if (right == null) s += spacing + "\n"; s += spacing + right.totree(blank, blank+spacing); return s; 10. Deleting 10.1 Easy cases: leaf (): change ref in parent (8) to null 1 child (6): change parent s () child to child s child () (so, s child becomes ) 8 1 18 10.2 Node has two children? a bit harder more general algorithm for deleting node N with item i: - walk down tree until you find N with i - let p be parent of N - if left subtree of N is null, make right subtree of N into subtree of p - if left subtree of N is not empty, extract max value from left subtree of N and stick that into N 11. Exercises Write a recursive findmin. Write iterative versions of search, findmax, findmin, and insert. Write method checkbst to check if a tree is a BST. Write delete see testbst and textbooks for course. 12 10 1 12 10 1 1 20