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

Similar documents
Lecture 23, Fall 2018 Monday October 29

CS 231 Data Structures and Algorithms Fall Binary Trees, Comparator Lecture 22 October 26, Prof. Zadia Codabux

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

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

Data Structures and Algorithms

Programming II (CS300)

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

Chapter 20: Binary Trees

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

CS 151. Binary Search Trees. Wednesday, October 10, 12

Data Structures in Java

TREES Lecture 12 CS2110 Spring 2018

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

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

Binary Search Trees. Ravi Chugh March 28, 2014

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Tree Travsersals and BST Iterators

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CSI33 Data Structures

Building Java Programs

INF2220: algorithms and data structures Series 1

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

Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

CS24 Week 8 Lecture 1

Lecture 23: Binary Search Trees

TREES Lecture 12 CS2110 Fall 2016

CS Introduction to Data Structures Week 5, 2017

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

CS 206 Introduction to Computer Science II

Data Structures and Algorithms for Engineers

Computer Science II Fall 2009

CS350: Data Structures Tree Traversal

Programming Languages and Techniques (CIS120)

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

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

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

Binary Trees: Practice Problems

Lecture 13: Binary Search Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CSCS-200 Data Structure and Algorithms. Lecture

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

CS2012 Programming Techniques II

Principles of Computer Science

CMSC 341 Lecture 10 Binary Search Trees

3137 Data Structures and Algorithms in C++

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

CS302 - Data Structures using C++

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

COMP 250 Midterm #2 March 11 th 2013

Points off Total off Net Score. CS 314 Final Exam Spring 2017

TREES Lecture 10 CS2110 Spring2014

Fall, 2015 Prof. Jungkeun Park

We have the pointers reference the next node in an inorder traversal; called threads

CS2012 Programming Techniques II

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

Generic BST Interface

Binary Trees. Examples:

TREES. Trees - Introduction

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Largest Online Community of VU Students

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Recursion, Binary Trees, and Heaps February 18

Module 9: Binary trees

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

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

CSE 214 Computer Science II Heaps and Priority Queues

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

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

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

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

CSC 321: Data Structures. Fall 2012

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

CSC148-Section:L0301

Prelim One Solution. CS211 Fall Name. NetID

CSI33 Data Structures

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

CS 315 Data Structures mid-term 2

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

CSC 321: Data Structures. Fall 2016

CS 206 Introduction to Computer Science II

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

lecture14: Tree Traversals

CmpSci 187: Programming with Data Structures Spring 2015

Module 8: Binary trees

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

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

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

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

STUDENT LESSON AB30 Binary Search Trees

CSI33 Data Structures

CS 314 Exam 2 Fall 2017

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

Cpt S 122 Data Structures. Data Structures Trees

Transcription:

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

Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2

Administrative None. 3

Ternary Operator 4

Ternary Operator Java supports an interesting operator that can replace short if/else statements like this: if (<Condition>) <Evaluate if true> else <Evaluate if false> with <Condition>? <Evaluate if true> : <Evaluate if false>; 5

Ternary Operator This can make your code more concise. For example, let's say we had int a = 4; and int b = 5; and we wanted to determine which one was the minimum. We could write: int min = (a < b)? a : b; 6

Ternary Operator public static void main(string[] args) { int a = 4; int b = 5; int min = (a < b)? a : b; System.out.println(min); } 7

Binary Search Tree 8

Binary Search Trees (BST) A binary search tree is a sorted binary tree in which the key field value of the root node is greater than the key field values of all of the nodes in the root's left subtree, and less than the key field values of all of the nodes in the root's right subtree. Is each subtree in the tree also a BST? 9

Example The value of the key field of the tree's root node, 50, is greater than all the keys in its left subtree (40, 35, 47, and 43), and it is also less than all the keys in its right subtree (63, 55, 70, 68, and 80). In addition, all subtrees in the tree are also binary trees, which can be verified by inspecting them. For example, consider the subtree whose root node is 63. This subtree is also a binary search tree because all keys in 63's left subtree (55) are less than 63, and all of the keys in the root's right subtree (70, 68, and 80) are greater than 63. 10

Traversals of BST In-order traversal: 1 2 3 4 5 6 7 8 9 Post-order traversal: 1 2 4 3 6 7 9 8 5 Pre-order traversal: 5 3 2 1 4 8 7 6 9 11

Key-Value Pairs Binary search trees generally store more general data than just ints. They often store data as key-value pairs. Think of a BST node as a look-up table: It maps keys to values that we store. Key/value pairs are also called entries. Key Value Jane 18 Jack 25 12

Key-Value Pairs Often, we organize the BST according to the keys. Once we find the key in the tree (name), we look up the associated value (age). Key tend to be compact and helpful for looking up the value --- usually Integers or Strings. Unique keys map to unique values (i.e. duplicate keys return the same value). The value could be a single piece of data (a string, number, etc), arrays, or even whole data structures. 13

Key-Value Pairs class KeyValuePair { private K key; private V value; public KeyValuePair(K key, V value) { this.key = key; this.value = value; } public K getkey() { return key; } public V getvalue() { return value; } public void setvalue(v value) { this.value = value; } } 14

Complexity put(int n): O(n) get(int n): O(n) sum(): O(n) 15

Node-Based BST For the implementation, we ll use a Node class that will store int values and keep a reference to each child: class TreeNode { private int data; private TreeNode left; private TreeNode right; public TreeNode (int data, TreeNode left, TreeNode right) { this.data = data; this.left = left; this.right = right; } } 16

Node-Based BST Then, let s add the starting node (root) of our tree. public class BinaryTree { private TreeNode root; public BinaryTree() { root = null; } } 17

Node-Based BST Methods in Node-Based Tree put: if new value is larger than the root s value, it is the left child of the root; otherwise, right child. sum: sum all node values in this NodeBasedTree The sum of a tree is the sum of its left subtree plus the sum of its right subtree plus the root value. When calculate the sum of left subtree, it repeat the same process for the left subtree. It is a recursive function. printinorder: print the traverse of the tree in inorder 18

put(int data) First, we have to find the place where we want to add a new node in order to keep the tree sorted. We ll follow these rules starting from the root node: if the new node s value is lower than the current node s, we go to the left child if the new node s value is greater than the current node s, we go to the right child when the current node is null, we ve reached a leaf node and we can insert the new node in that position 19