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

Similar documents
CSI33 Data Structures

CSI33 Data Structures

CSI33 Data Structures

CSI33 Data Structures

Binary Search Tree. Revised based on textbook author s notes.

CSI33 Data Structures

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Spring 2018 Mentoring 8: March 14, Binary Trees

TREES AND ORDERS OF GROWTH 7

Trees. Tree Structure Binary Tree Tree Traversals

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

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

CSI33 Data Structures

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

Lecture 16: Binary Search Trees

CSC148 Week 7. Larry Zhang

CS 206 Introduction to Computer Science II

Chapter 20: Binary Trees

n 1 i = n + i = n + f(n 1)

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

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

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

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

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

Binary Search Tree (3A) Young Won Lim 6/6/18

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

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

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

CS350: Data Structures Binary Search Trees

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/2/18

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

Fall, 2015 Prof. Jungkeun Park

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

TREES. Trees - Introduction

CS 350 : Data Structures Binary Search Trees

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

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

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

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

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

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

Programming Languages and Techniques (CIS120)

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

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

CS 206 Introduction to Computer Science II

COMP Analysis of Algorithms & Data Structures

STUDENT LESSON AB30 Binary Search Trees

If you took your exam home last time, I will still regrade it if you want.

COMP Analysis of Algorithms & Data Structures

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

B-Trees. Based on materials by D. Frey and T. Anastasio

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Trees, Binary Trees, and Binary Search Trees

Tree Travsersals and BST Iterators

Data Structures in Java

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Programming II (CS300)

Data Structures and Algorithms

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

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

CSCI-1200 Data Structures Fall 2014 Lecture 18 Trees, Part III

CS 206 Introduction to Computer Science II

CmpSci 187: Programming with Data Structures Spring 2015

Trees, Part 1: Unbalanced Trees

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

Transform & Conquer. Presorting

Generic BST Interface

CS 171: Introduction to Computer Science II. Binary Search Trees

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

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Data Structures and Algorithms

CSC148-Section:L0301

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

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

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Binary Search Trees. EECS 214, Fall 2018

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

8. Binary Search Tree

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

Heaps. Heaps. A heap is a complete binary tree.

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

CSC148-Section:L0301

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

3137 Data Structures and Algorithms in C++

Binary Trees. Examples:

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

Arrays aren t going to work. What can we do? Use pointers Copy a large section of a heap, with a single pointer assignment

Binary Search Trees. Chapter 21. Binary Search Trees

Topic 14. The BinaryTree ADT

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

TREES Lecture 12 CS2110 Spring 2018

Short Python function/method descriptions:

Transcription:

Outline Chapter 7: Trees 1 Chapter 7: Trees

Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists are efficient for insertion and deletion operations, while a sorted array allows for efficient searching (recall binary search algorithm), although requires Θ(n) for insertion and deletion operations. Let s combine the best of both worlds!

Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists are efficient for insertion and deletion operations, while a sorted array allows for efficient searching (recall binary search algorithm), although requires Θ(n) for insertion and deletion operations. Let s combine the best of both worlds!

Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists are efficient for insertion and deletion operations, while a sorted array allows for efficient searching (recall binary search algorithm), although requires Θ(n) for insertion and deletion operations. Let s combine the best of both worlds!

The Binary Search Property A Binary Tree is Sorted A Binary Search Tree, or BST, is a binary tree where every node has the following property: Each value in the left subtree is less than the value at the node. Each value in the right subtree is greater than the value at the node. No duplicates!

The Binary Search Property Binary Search With A Binary Tree Start at the root If the value is there, we are done If the value is less than the node value, search the left subtree If the value is greater than the node value, search the right subtree

The Binary Search Property Performance (Running Time) To Find A Value Average Performance is Θ(log n). If the tree is not too unbalanced, then we divide the number of items to search in half at each node. This is actually a binary search. Worst-Case Performance is Θ(n). If the tree branches only to one side (left or right) this is the same as linear search.

TreeNode Class Chapter 7: Trees Recursive Definition Of A TreeNode Class in Python class TreeNode(object): def init (self, data = None, left=none, right=none): self.item = data self.left = left # TreeNode or None self.right = right # TreeNode or None

TreeNode Class Chapter 7: Trees Recursive Definition of a TreeNode Class in C++ //TreeNode.h class TreeNode{ public: TreeNode(int item, TreeNode* left = NULL, TreeNode* right = NULL); private: TreeNode(const TreeNode &tnode); // no Copy Constructor void operator=(const TreeNode &tnode); // no assignment operator int item; TreeNode* left; TreeNode* right; }; See the files TreeNode.h, TreeNode.cpp, and usingtreenode.cpp.

Implementing A BST in Python: init (Constructor) from TreeNode import TreeNode class BST (object): def init (self): """ creates empty binary search tree """ self.root = None

Implementing A BST in C++ : BST.h // BST.h class BST{ public: BST() { root = NULL; } // creates empty tree BST(); // destructor private: TreeNode * root; };

Insertion into BST Inserting into BST For the following BST, let s insert values 15, 34, and 79.

Insertion into BST Inserting into BST After insertion of 15, 34, and 79:

Insertion into BST Insertion: iterative or recursive Trees are a naturally recursive data structure. Therefore insertion can be implemented recursively, but let s start with iterative version first. See the files BST.h and BST.cpp.

Insertion into BST Search in BST tree: Python def find(self, item): """ post: returns item from BST; None otherwise """ node = self.root while node is not None and not(node.item == item): if item < node.item: node = node.left else: node = node.right if node is None: return None else: return node.item

Insertion into BST Search in BST tree: C++

Deletion from BST Removing nodes from the tree Removing a specific item from a BST is a bit tricky. List of cases: the node to be removed is a leaf: then we can simply drop it off the tree (reference in its parent node is set to None) the node to be removed has a single child: then we can simply reset the reference from its parent to the reference to the node s child instead. the node to be removed has two children: leave the node in place, but replace its contents (i.e. value), i.e. find an easily deletable node whose contents can be transferred into the target node, while maintaining the tree s binary search property. (Two options there: rightmost node of the left subtree (our book), or leftmost node of the right subtree)

Deletion from BST Example 1 Let s delete 5 from a BST:

Deletion from BST Example 2 Let s delete 6 from a BST:

Deletion from BST Example 3 Let s delete 15 from the given BST:

Deletion from BST Example 3

Deletion from BST Example 3

Deletion from BST Example 3

Deletion from BST Example 3

Deletion from BST Example 3

Deletion from BST Conclusion In conclusion, we can say that when deleting a value from BTS, which is represented by a node with two children, our book follows the following procedure: step to the left sub-tree, locate the rightmost node(or a leaf) in it, and place its value in to the node with value to be deleted, making necessary adjustments of the references.

Deletion from BST Removing nodes from BST Let s practice more! Delete 30, 100, and 109 from the following tree:

Deletion from BST Removing nodes from BST After deletion of 30, 100, and 109:

Printing BST Chapter 7: Trees displaying BST as list We would like to see what is happenning to our tree after deletion and insertion operations. Let s generate an array representation for our tree. Convention: empty nodes will be represented by zeros (for now).

Run-Time Analysis Of BST Methods Methods aslist is Θ(n). insert, delete, find have Θ(log n) average behavior. insert, delete, find have Θ(n) worst-case behavior.

In-class work Chapter 7: Trees In-class work - Part 1 For the following, state whether each of them is a binary tree, a binary search tree (BST), or just a tree.

In-class work Chapter 7: Trees In-class work - on your own, part 2 Using class BST, insert the following numbers, one by one: 14, 10, 18, 25, 17, 7, 1, 12, 30. Draw this BST as you think it should look. Then print the BST to see it in array representation.