CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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:
Building Java Programs Chapter 17 Binary Trees. 2 Creative use of arrays/links Some data structures (such as hash tables and binary trees) are built around.
Building Java Programs Binary Search Trees; TreeSet.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Binary Trees reading: 17.1 – 17.3.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE 373 Binary search trees; tree height and balance
Building Java Programs
Trees.
slides created by Marty Stepp
Binary Search Trees.
Building Java Programs
Building Java Programs Chapter 17
Rick Mercer, Allison Obourn, Marty Stepp
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
CSE 373 AVL trees read: Weiss Ch. 4, section
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
Lecture 12 CS203 1.
Building Java Programs
Building Java Programs
CSE 373: Data Structures and Algorithms
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
CSE 373 Data Structures and Algorithms
Building Java Programs
CSC 143 Binary Search Trees.
Building Java Programs
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Binary Search Trees reading: 17.3 – 17.4
Hashing based on slides by Marty Stepp
slides adapted from Marty Stepp
Trees.
Lecture 9: Intro to Trees
slides created by Marty Stepp
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Presentation transcript:

CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp

2 Exercise Add a method named printSideways to the IntTree class that prints the tree in a sideways indented format, with right nodes above roots above left nodes, with each level 4 spaces more indented than the one above it. –Example: Output from the tree below: overall root

3 Exercise solution // Prints the tree in a sideways indented format. public void printSideways() { printSideways(overallRoot, ""); } private void printSideways(IntTreeNode root, String indent) { if (root != null) { printSideways(root.right, indent + " "); System.out.println(indent + root.data); printSideways(root.left, indent + " "); }

4 Binary search trees binary search tree ("BST"): a binary tree that is either: –empty ( null ), or –a root node R such that: every element of R's left subtree contains data "less than" R's data, every element of R's right subtree contains data "greater than" R's, R's left and right subtrees are also binary search trees. BSTs store their elements in sorted order, which is helpful for searching/sorting tasks overall root

5 Exercise Which of the trees shown are legal binary search trees? xk qg m e b

6 Searching a BST Describe an algorithm for searching the tree below for the value 31. Then search for the value 6. What is the maximum number of nodes you would need to examine to perform any search? overall root

7 Exercise Add a method contains to the IntTree class that searches the tree for a given integer, returning true if it is found in the tree and false if not. Assume that the elements of the tree constitute a legal binary search tree. –If an IntTree variable tree referred to the tree below, the following calls would have the following results: tree.contains(29)  true tree.contains(55)  true tree.contains(63)  false tree.contains(35)  false overall root

8 Exercise solution // Returns whether this tree contains the given integer. public boolean contains(int value) { return contains(overallRoot, value); } private boolean contains(IntTreeNode root, int value) { if (root == null) { return false; } else if (root.data == value) { return true; } else if (root.data > value) { return contains(root.left, value); } else { // root.data < value return contains(root.right, value); }

9 Adding to a BST Suppose we want to add the value 14 to the BST below. –Where should the new node be added? Where would we add the value 3? Where would we add 7? If the tree is empty, where should a new value be added? What is the general algorithm?

10 Adding exercise Draw what a binary search tree would look like if the following values were added to an initially empty tree in this order:

11 Exercise Add a method add to the IntTree class that adds a given integer value to the tree. Assume that the elements of the IntTree constitute a legal binary search tree, and add the new value in the appropriate place to maintain ordering. tree.add(49) ; overall root 49

12 An incorrect solution // Adds the given value to this BST in sorted order. public void add(int value) { add(overallRoot, value); } private void add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (root.data > value) { add(root.left, value); } else if (root.data < value) { add(root.right, value); } // else root.data == value; // a duplicate (don't add) } Why doesn't this solution work? overallRoot

13 The problem Much like with linked lists, if we just modify what a local variable refers to, it won't change the collection. private void add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); –In the linked list case, how did we correct this problem? How did we actually modify the list? overallRoot 49 root

14 x = change(x); All String object methods that modify a String actually return a new String object. –If we want to modify a string variable, we must re-assign it. String s = "lil bow wow"; s.toUpperCase(); System.out.println(s); // lil bow wow s = s.toUpperCase(); System.out.println(s); // LIL BOW WOW –We call this general algorithmic pattern x = change(x); –We will use this approach when writing methods that modify the structure of a binary tree.

15 Applying x = change(x) Methods that modify a tree should have the following pattern: –input (parameter):old state of the node –output (return):new state of the node In order to actually change the tree, you must reassign: root = change (root, parameters ); root.left = change (root.left, parameters ); root.right = change (root.right, parameters ); your method node before node after parameterreturn

16 A correct solution // Adds the given value to this BST in sorted order. public void add(int value) { overallRoot = add(overallRoot, value); } private IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (root.data > value) { root.left = add(root.left, value); } else if (root.data < value) { root.right = add(root.right, value); } // else a duplicate return root; } Think about the case when root is a leaf overallRoot

17 Searching BSTs The BSTs below contain the same elements. –What orders are "better" for searching? overall root overall root overall root 9 7 6

18 Trees and balance balanced tree: One whose subtrees differ in height by at most 1 and are themselves balanced. –A balanced tree of N nodes has a height of ~ log 2 N. –A very unbalanced tree can have a height close to N. –The runtime of adding to / searching a BST is closely related to height. –Some tree collections (e.g. TreeSet ) contain code to balance themselves as new nodes are added overall root height = 4 (balanced)