Building Java Programs Binary Search Trees; TreeSet.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
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
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
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.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
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.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
Chapter 20 Binary Search Trees
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
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Find Find 13: Find Min and Find Max Find Min: Find Max:
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
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.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? 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
BST Trees
Binary search tree. Removing a node
Trees.
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
CSE 373: Data Structures and Algorithms
Binary Search Trees (BSTs)
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
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 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
Trees.
Lecture 9: Intro to Trees
Trees Trees.
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
CS 261 – Data Structures AVL Trees.
Presentation transcript:

Building Java Programs Binary Search Trees; TreeSet

2

3 Recall: 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 ); your method node before node after parameterreturn

4 Exercise Add a method getMin to the IntTree class that returns the minimum integer value from the tree. Assume that the elements of the IntTree constitute a legal binary search tree. Throw a NoSuchElementException if the tree is empty. int min = tree.getMin(); // overall root

5 Exercise solution // Returns the minimum value from this BST. // Throws a NoSuchElementException if the tree is empty. public int getMin() { if (overallRoot == null) { throw new NoSuchElementException(); } return getMin(overallRoot); } private int getMin(IntTreeNode root) { if (root.left == null) { return root.data; } else { return getMin(root.left); } overallRoot

6 Exercise Add a method remove to the IntTree class that removes a given integer value from the tree, if present. Remove the value in such a way as to maintain BST ordering. tree.remove(73); tree.remove(29); tree.remove(87); tree.remove(55); overall root 73

7 Cases for removal 1 1.a leaf:replace with null 2.a node with a left child only:replace with left child 3.a node with a right child only:replace with right child overall root tree.remove(-3); overall root overall root 42 overall root tree.remove(55); tree.remove(29);

8 Cases for removal 2 4.a node with both children:replace with min from right (replacing with max from left would also work) overall root overall root tree.remove(55);

9 Exercise solution // Removes the given value from this BST, if it exists. public void remove(int value) { overallRoot = remove(overallRoot, value); } private IntTreeNode remove(IntTreeNode root, int value) { if (root == null) { return null; } else if (root.data > value) { root.left = remove(root.left, value); } else if (root.data < value) { root.right = remove(root.right, value); } else { // root.data == value; remove this node if (root.right == null) { return root.left; // no R child; replace w/ L } else if (root.left == null) { return root.right; // no L child; replace w/ R } else { // both children; replace w/ min from R root.data = getMin(root.right); root.right = remove(root.right, root.data); } return root; }

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

11 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)