Binary Search Trees reading: 17.3 – 17.4

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture.
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
CSE 143 Lecture 19 Binary Search Trees read 17.3 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.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
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
1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
Homework 6 Shortest Paths!. The Basic Idea You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get.
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
Recursive Objects (Part 4)
Building Java Programs
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Trees.
ENEE150 Discussion 13 Section 0101 Adam Wang.
Binary Search Trees.
Building Java Programs
Building Java Programs Chapter 17
Rick Mercer, Allison Obourn, Marty Stepp
Building Java Programs
Search Sorted Array: Binary Search Linked List: Linear Search
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
Building Java Programs
Binary Search Trees.
Building Java Programs
Data Structures and Algorithms
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees continued; Tree Sets
Why did the programmer quit his job?
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CSE 373 Data Structures and Algorithms
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
Building Java Programs
BINARY TREE CSC248 – Data Structure.
CSC 143 Binary Search Trees.
Building Java Programs
Trees.
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Trees.
CS 6310 Advanced Data Structure Wei-Shian Wang
Lecture 6: References and linked nodes reading: 16.1
Lecture 9: Intro to Trees
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

Binary Search Trees reading: 17.3 – 17.4 CSE 143 Binary Search Trees reading: 17.3 – 17.4

Binary search trees binary search tree ("BST"): a binary tree where each non-empty node R has the following properties: elements of R's left subtree contain data "less than" R's data, elements of R's right subtree contain 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. System.out.println(contains(42)); 91 60 87 29 55 42 -3 overall root

Adding to a BST Suppose we want to add new values to the BST below. Where should the value 14 be added? Where should 3 be added? 7? If the tree is empty, where should a new value be added? What is the general algorithm? overall root 19 10 11 5 8 4 2 7 25 22

Change point, version 2 What is the state of the object referred to by p after this code? public static void main(String[] args) { Point p = new Point(1, 2); change(p); System.out.println(p); } public static void change(Point thePoint) { thePoint = new Point(3, 4); // answer: (1, 2) 2 y 1 x p 4 y 3 x

Changing references If a method dereferences a variable (with . ) and modifies the object it refers to, that change will be seen by the caller. public static void change(Point thePoint) { thePoint.x = 3; // affects p thePoint.setY(4); // affects p If a method reassigns a variable to refer to a new object, that change will not affect the variable passed in by the caller. thePoint = new Point(3, 4); // p unchanged thePoint = null; // p unchanged What if we want to make the variable passed in become null?

Change point, version 3 What is the state of the object referred to by p after this code? public static void main(String[] args) { Point p = new Point(1, 2); change(p); System.out.println(p); } public static Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint; // answer: (1, 2) 2 y 1 x p 4 y 3 x

Change point, version 4 What is the state of the object referred to by p after this code? public static void main(String[] args) { Point p = new Point(1, 2); p = change(p); System.out.println(p); } public static Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint; // answer: (3, 4) 2 y 1 x p 4 y 3 x

x = change(x); If you want to write a method that can change the object that a variable refers to, you must do three things: 1. pass in the original state of the object to the method 2. return the new (possibly changed) object from the method 3. re-assign the caller's variable to store the returned result p = change(p); // in main public static Point change(Point thePoint) { thePoint = new Point(99, -1); return thePoint; We call this general algorithmic pattern x = change(x); also seen with strings: s = s.toUpperCase();

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: node = change(node, parameters); node.left = change(node.left, parameters); node.right = change(node.right, parameters); overallRoot = change(overallRoot, parameters); node before parameter return your method node after