ITI Introduction to Computing II

Similar documents
ITI Introduction to Computing II

Programming II (CS300)

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

Binary Trees

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

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

Trees. Eric McCreath

ITI Introduction to Computing II

COMP : Trees. COMP20012 Trees 219

Tree Structures. A hierarchical data structure whose point of entry is the root node

CSE 214 Computer Science II Heaps and Priority Queues

ITI Introduction to Computing II

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

CSE 214 Computer Science II Introduction to Tree

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Introduction to Computer Science II (ITI 1121) Final Examination

ITEC2620 Introduction to Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary

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

CE 221 Data Structures and Algorithms

Priority Queues and Huffman Trees

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

Trees. A tree is a directed graph with the property

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

TREES. Trees - Introduction

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

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

CSI33 Data Structures

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

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

Chapter 20: Binary Trees

Priority queues. Priority queues. Priority queue operations

CS 206 Introduction to Computer Science II

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Building Java Programs

Binary Trees, Binary Search Trees

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

CSC148 Week 6. Larry Zhang

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Garbage Collection: recycling unused memory

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

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

Trees. CSE 373 Data Structures

Spring 2018 Mentoring 8: March 14, Binary Trees

Data Structures in Java

ITEC2620 Introduction to Data Structures

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

Binary Search Trees 1

Why Do We Need Trees?

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

- Alan Perlis. Topic 24 Heaps

Data Structures and Algorithms

Binary Trees. Quicksort. SorKng process with quicksort. University of Massachuse5s Amherst ECE 242 Data Structures and Algorithms Lecture 18

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

CS 151. Binary Trees. Friday, October 5, 12

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

ITI Introduction to Computing II

CS102 Binary Search Trees

BBM 201 Data structures

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Recursive Data Structures and Grammars

CS24 Week 8 Lecture 1

An Introduction to Trees

TREES Lecture 10 CS2110 Spring2014

INF2220: algorithms and data structures Series 1

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

JAVA NOTES DATA STRUCTURES

CS 314 Final Fall 2011

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CSC148H Week 8. Sadia Sharmin. July 12, /29

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

TREES Lecture 12 CS2110 Spring 2018

Binary Trees Fall 2018 Margaret Reid-Miller

Data Structure. IBPS SO (IT- Officer) Exam 2017

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Introduction to Computing II (ITI 1121) Final Examination

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Topic 14. The BinaryTree ADT

TREES Lecture 12 CS2110 Fall 2016

Postfix (and prefix) notation

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Priority Queues and Binary Heaps

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

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

12 Abstract Data Types

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CS302 - Data Structures using C++

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

EE 368. Weeks 5 (Notes)

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

CMSC 341 Lecture 15 Leftist Heaps

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Programming II (CS300)

Transcription:

ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

Binary tree A binary tree is a tree-like (hierarchical) data structure such that each node stores a value and has at most two children, which are called left and right. 8 5 15 9 29 11

Applications (general trees)

Applications (general trees) Representing hierarchical information such as hierarchical file systems (directories have sub-directories), programs (parse trees);

Applications (general trees) Representing hierarchical information such as hierarchical file systems (directories have sub-directories), programs (parse trees); Huffman trees are used for (de-)compressing information (files);

Applications (general trees) Representing hierarchical information such as hierarchical file systems (directories have sub-directories), programs (parse trees); Huffman trees are used for (de-)compressing information (files); An efficient data structure to implement abstract data types such as heaps, priority queues and sets.

8 5 15 9 29 11

8 5 15 9 29 11 All the nodes except one have exactly one parent.

8 5 15 9 29 11 All the nodes except one have exactly one parent. That node that has no parent is called the root (which is drawn at the top of the diagram).

8 5 15 9 29 11 All the nodes except one have exactly one parent. That node that has no parent is called the root (which is drawn at the top of the diagram). Each node has 0, 1 or 2 children.

8 5 15 9 29 11 All the nodes except one have exactly one parent. That node that has no parent is called the root (which is drawn at the top of the diagram). Each node has 0, 1 or 2 children. Nodes that have no children are called leaves (or external nodes).

8 5 15 9 29 11 All the nodes except one have exactly one parent. That node that has no parent is called the root (which is drawn at the top of the diagram). Each node has 0, 1 or 2 children. Nodes that have no children are called leaves (or external nodes). Links between nodes are called branches.

Binary tree 8 5 15 9 29 11 A node and its descendants is called a subtree.

Binary tree 8 5 15 9 29 11 A node and its descendants is called a subtree. The size of tree is the number of nodes in the tree.

Binary tree 8 5 15 9 29 11 A node and its descendants is called a subtree. The size of tree is the number of nodes in the tree. An empty tree has size 0.

Binary tree 8 5 15 9 29 11 A node and its descendants is called a subtree. The size of tree is the number of nodes in the tree. An empty tree has size 0. Since the discussion is restricted to binary trees, we will sometimes use the word tree to mean a binary tree.

Binary trees can be defined recursively, Binary tree A binary tree is empty, or; A binary tree consists of a value as well as two sub-trees;

Binary tree The depth of a node is the number of links starting from the root that must be followed to reach that node. The root is the most accessible node. 8 5 15 9 29 11

Binary tree The depth of a node is the number of links starting from the root that must be followed to reach that node. The root is the most accessible node. 8 5 15 9 29 11 What is the depth of the root?

Binary tree The depth of a node is the number of links starting from the root that must be followed to reach that node. The root is the most accessible node. 8 5 15 9 29 11 What is the depth of the root? The root always has a depth of 0.

Binary tree The depth of a node is the number of links starting from the root that must be followed to reach that node. The root is the most accessible node. 8 5 15 9 29 11 What is the depth of the root? The root always has a depth of 0. The depth of a tree is the depth of the deepest node.

Binary tree All the trees presented thus far exhibit a certain property, what is it? 8 5 15 2 7 12 29 1 4 13 50

Binary search tree A binary search tree is a binary tree such that, the nodes of a left sub-tree contain elements that are less than the element stored at the local root (or is empty); the nodes of a right sub-tree contain elements that are greater than the element stored at the local root (or is empty). 8 5 15 2 7 12 29 1 4 13 50

Binary search tree A binary search tree is a binary tree such that, the nodes of a left sub-tree contain elements that are less than the element stored at the local root (or is empty); the nodes of a right sub-tree contain elements that are greater than the element stored at the local root (or is empty). 8 5 15 2 7 12 29 1 4 13 50 The definition precludes duplicate values.

Binary search tree Implementing a binary search tree, what is needed?

Binary search tree Implementing a binary search tree, what is needed? That s right, we need a class Node.

Binary search tree Implementing a binary search tree, what is needed? That s right, we need a class Node. What are its instance variables?

Binary search tree Implementing a binary search tree, what is needed? That s right, we need a class Node. What are its instance variables? Its instance variables are value, left and right.

Binary search tree Implementing a binary search tree, what is needed? That s right, we need a class Node. What are its instance variables? Its instance variables are value, left and right. What are the types of these variables?

Binary search tree Implementing a binary search tree, what is needed? That s right, we need a class Node. What are its instance variables? Its instance variables are value, left and right. What are the types of these variables? value should be Comparable, left and right should be of type Node.

Binary search tree A static nested class to store the elements of the tree. public class BinarySearchTree< E extends Comparable<E> > { private static class Node<E> { private E value; private Node<E> left; private Node<E> right; }

Binary search tree Instance variable(s) of the class BinarySearchTree? public class BinarySearchTree< E extends Comparable<E> > { private static class Node<E> { private E value; private Node<E> left; private Node<E> right; }

Binary search tree Instance variable(s) of the class BinarySearchTree? public class BinarySearchTree< E extends Comparable<E> > { private static class Node<E> { private E value; private Node<E> left; private Node<E> right; } private Node<E> root;

Memory diagram t BinarySearchTree 8 Comparable root Node value left right

t BinarySearchTree 8 root Node 5 11 9 15

t 8 root 8 5 11 5 11 9 15 9 15

Observations A leaf is a Node such that both its descendant reference variables (left and right) are null. The reference root can be null, in which case the tree is empty, i.e. has size 0. For brevity, we will often use the more abstract representation on the right. t 8 root 8 5 11 5 11 9 15 9 15

boolean contains( E obj ) 8 5 11 9 15

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found;

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found;

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise? Where should you start looking?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise? Where should you start looking? 3. If obj is less than the value found at the root?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise? Where should you start looking? 3. If obj is less than the value found at the root? Look for obj in the left sub-tree;

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise? Where should you start looking? 3. If obj is less than the value found at the root? Look for obj in the left sub-tree; 4. Else (obj must be larger than the value stored at the root)?

boolean contains( E obj ) 8 5 11 9 15 1. Empty tree? obj not found; 2. The root contains obj? obj was found; Otherwise? Where should you start looking? 3. If obj is less than the value found at the root? Look for obj in the left sub-tree; 4. Else (obj must be larger than the value stored at the root)? Look for obj in the right sub-tree.

boolean contains( E obj ) 8 5 11 9 15 Exercise: apply the algorithm for finding the values 8, 9 and 7.

public boolean contains( E obj ) The above presentation suggests a recursive algorithm.

public boolean contains( E obj ) The above presentation suggests a recursive algorithm. signature of the method? What will be the

public boolean contains( E obj ) The above presentation suggests a recursive algorithm. signature of the method? What will be the public boolean contains( E obj ) { // pre-condition: if ( obj == null ) { throw new IllegalArgumentException( "null" ); } return contains( obj, root ); } Similarly to the methods for recursive list processing, these methods will consist of two parts, a starter method as well as a private method whose signature is augmented with a parameter of type Node.

Base case(s): boolean contains( Node<E> current, E obj )

Base case(s): boolean contains( Node<E> current, E obj ) if ( current == null ) { result = false; }

Base case(s): boolean contains( Node<E> current, E obj ) if ( current == null ) { result = false; } but also

Base case(s): boolean contains( Node<E> current, E obj ) if ( current == null ) { result = false; } but also if ( obj.compareto( current.value ) == 0 ) { result = true; }

General case: boolean contains( Node<E> current, E obj )

boolean contains( Node<E> current, E obj ) General case: Look left or right (recursively).

boolean contains( Node<E> current, E obj ) General case: Look left or right (recursively). if ( obj.compareto( current.value ) < 0 ) { result = contains( current.left, obj ); } else { result = contains( current.right, obj ); }

boolean contains( Node<E> current, E obj ) private boolean contains( Node<E> current, E obj ) { boolean result; if ( current == null ) { result = false; } else { int test = obj.compareto( current.value ); if ( test == 0 ) { result = true; } else if ( test < 0 ) { result = contains( current.left, obj ); } else { result = contains( current.right, obj ); } } return result; }

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive?

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No.

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy.

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node;

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree;

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end;

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end; 4. If current.value is the value we re looking for, end;

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end; 4. If current.value is the value we re looking for, end; 5. If the value is smaller than that of the current node,

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end; 4. If current.value is the value we re looking for, end; 5. If the value is smaller than that of the current node, current = current.left,

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end; 4. If current.value is the value we re looking for, end; 5. If the value is smaller than that of the current node, current = current.left, go to 3;

public boolean contains( E obj ) (take 2) Should the method boolean contains( Comparable o ) be necessarily recursive? No. Develop a strategy. 1. Use a temporary variable current of type Node; 2. Initialise this variable to designate the root of the tree; 3. If current is null the obj was not found, end; 4. If current.value is the value we re looking for, end; 5. If the value is smaller than that of the current node, current = current.left, go to 3; 6. Else current = current.right, go to 3.

public boolean contains( E obj ) (take 2) public boolean contains2( E obj ) { } boolean found = false; Node<E> current = root; while (! found && current!= null ) { int test = obj.compareto( current.value ); if ( test == 0 ) { found = true; } else if ( test < 0 ) { current = current.left; } else { current = current.right; } } return found;

Tree traversal Similarly to lists, it is often necessary to visit all the nodes of a tree and this is called traversing the tree.

Tree traversal Similarly to lists, it is often necessary to visit all the nodes of a tree and this is called traversing the tree. While traversing the tree, operations are applied to each node, we call these operations visiting the node.

Tree traversal Similarly to lists, it is often necessary to visit all the nodes of a tree and this is called traversing the tree. While traversing the tree, operations are applied to each node, we call these operations visiting the node. < Visit the root, traverse left sub-tree, traverse right sub-tree > is called pre-order traversal;

Tree traversal Similarly to lists, it is often necessary to visit all the nodes of a tree and this is called traversing the tree. While traversing the tree, operations are applied to each node, we call these operations visiting the node. < Visit the root, traverse left sub-tree, traverse right sub-tree > is called pre-order traversal; < Traverse left sub-tree, visit the root, traverse right sub-tree > is called in-order traversal;

Tree traversal Similarly to lists, it is often necessary to visit all the nodes of a tree and this is called traversing the tree. While traversing the tree, operations are applied to each node, we call these operations visiting the node. < Visit the root, traverse left sub-tree, traverse right sub-tree > is called pre-order traversal; < Traverse left sub-tree, visit the root, traverse right sub-tree > is called in-order traversal; < Traverse left sub-tree, traverse right sub-tree, visit the root > is called post-order traversal;

Exercises The simplest operation consists of printing the value of the node. 8 5 15 2 7 12 29 Show the result for each strategy: pre-order, in-order and post-order traversal.

Exercises The simplest operation consists of printing the value of the node. 8 5 15 2 7 12 29 Show the result for each strategy: pre-order, in-order and post-order traversal. Which strategy prints the values in increasing order?