Binary Trees. Recursive definition. Is this a binary tree?

Similar documents
Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Binary search trees :

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

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

Data Structures. Dynamic Sets

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

12 Binary Search Tree

Lecture 6: Analysis of Algorithms (CS )

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Partha Sarathi Mandal

Binary Search Trees, etc.

Lecture: Analysis of Algorithms (CS )

Theory & Algorithms 15/01/04. Red-Black Trees. Nicolas Wack, Sylvain Le Groux

Red-Black Trees. Every simple path from a node to one of its descendant leaf nodes contains the same number of BLACK nodes.

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

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

Animations. 01/29/04 Lecture

Binary search trees (BST) Binary search trees (BST)

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

CMSC 441: Algorithms. Hillol Kargupta, Professor.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

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

External Sorting Methods

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

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

SFU CMPT Lecture: Week 9

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

Data Structures and Algorithms. Dr. Amit Kumar and Dr. Amitabha Bagchi IIT Delhi

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

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Different binary search trees can represent the same set of values (Fig.2). Vladimir Shelomovskii, Unitech, Papua New Guinea. Binary search tree.

Binary Search Tree. Sorting and Searching in a Dynamic Set. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Lecture 7. Binary Search Trees / AVL Trees

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

Algorithms, Spring 2014, CSE, OSU Lecture 4: Binary search trees

CS102 Binary Search Trees


Lecture 5: Scheduling and Binary Search Trees

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

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

Binary Search Trees. Analysis of Algorithms

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Binary Search Trees. CSE2100 DS & Algorithms 1

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Searching: Introduction

Analysis of Algorithms

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Balanced search trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

CS 3343 Fall 2007 Red-black trees Carola Wenk

A set of nodes (or vertices) with a single starting point

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

Binary Search Trees. What is a Binary Search Tree?

Augmenting Data Structures

COMP251: Red-black trees

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

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Algorithms. AVL Tree

CSCI Trees. Mark Redekopp David Kempe

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

COMP Analysis of Algorithms & Data Structures

CMSC 341 Lecture 14: Priority Queues, Heaps

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Range Searching and Windowing

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

TREES. Trees - Introduction

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

We assume uniform hashing (UH):

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

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

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

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

CS 241 Analysis of Algorithms

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

DATA STRUCTURES AND ALGORITHMS

Lecture 3: B-Trees. October Lecture 3: B-Trees

CMSC 341 Leftist Heaps

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Binary Trees, Binary Search Trees

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Algorithms and Data Structures

Friday Four Square! 4:15PM, Outside Gates

CS350: Data Structures Binary Search Trees

Binary Trees

Data Structures and Algorithms

catch(...){ printf( "Assignment::SolveProblem() AAAA!"); }

COMP Analysis of Algorithms & Data Structures

1 A node in a tree. Trees. Datatypes ISC

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Module 4: Priority Queues

Self-Balancing Search Trees. Chapter 11

Priority Queues Heaps Heapsort

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

Transcription:

Binary Search Trees

Binary Trees Recursive definition 1. An empty tree is a binary tree 2. A node with two child subtrees is a binary tree 3. Only what you get from 1 by a finite number of applications of 2 is a binary tree. 56 26 200 Is this a binary tree? 18 28 190 213 12 24 27

Binary Search Trees View today as data structures that can support dynamic set operations. Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. Can be used to build Dictionaries. Priority Queues. Basic operations take time proportional to the height of the tree O(h).

BST Representation Represented by a linked data structure of nodes. root(t) points to the root of tree T. Each node contains fields: key left pointer to left child: root of left subtree. right pointer to right child : root of right subtree. p pointer to parent. p[root[t]] = NIL (optional).

Binary Search Tree Property Stored keys must satisfy the binary search tree property. y in left subtree of x, then key[y] key[x]. y in right subtree of x, then key[y] key[x]. 56 26 200 18 28 190 213 12 24 27

Inorder Traversal The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively. Inorder-Tree-Walk (x) 1. if x NIL 2. then Inorder-Tree-Walk(left[p]) 3. print key[x] 4. Inorder-Tree-Walk(right[p]) 56 26 200 18 28 190 213 12 24 27 How long does the walk take? Can you prove its correctness?

Correctness of Inorder-Walk Must prove that it prints all elements, in order, and that it terminates. By induction on size of tree. Size=0: Easy. Size >1: Prints left subtree in order by induction. Prints root, which comes after all elements in left subtree (still in order). Prints right subtree in order (all elements come after root, so still in order).

Querying a Binary Search Tree All dynamic-set search operations can be supported in O(h) time. h = (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.) h = (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.

Tree Search Tree-Search(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) 56 26 200 Running time: O(h) 18 28 190 213 Aside: tail-recursion 12 24 27

Iterative Tree Search Iterative-Tree-Search(x, k) 1. while x NIL and k key[x] 2. do if k < key[x] 3. then x left[x] 4. else x right[x] 5. return x 56 26 200 18 28 190 213 12 24 27 The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward.

Finding Min & Max The binary-search-tree property guarantees that:» The minimum is located at the left-most node.» The maximum is located at the right-most node. Tree-Minimum(x) Tree-Maximum(x) 1. while left[x] NIL 1. while right[x] NIL 2. do x left[x] 2. do x right[x] 3. return x 3. return x Q: How long do they take?

Predecessor and Successor Successor of node x is the node y such that key[y] is the smallest key greater than key[x]. The successor of the largest key is NIL. Search consists of two cases. If node x has a non-empty right subtree, then x s successor is the minimum in the right subtree of x. If node x has an empty right subtree, then: As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. x s successor y is the node that x is the predecessor of (x is the maximum in y s left subtree). In other words, x s successor y, is the lowest ancestor of x whose left child is also an ancestor of x.

Pseudo-code for Successor Tree-Successor(x) if right[x] NIL 2. then return Tree-Minimum(right[x]) 3. y p[x] 4. while y NIL and x = right[y] 5. do x y 6. y p[y] 7. return y 56 26 200 Code for predecessor is symmetric. 18 28 190 213 Running time: O(h) 12 24 27

BST Insertion Pseudocode Change the dynamic set represented by a BST. Ensure the binarysearch-tree property holds after change. Insertion is easier than deletion. 56 26 200 18 28 190 213 12 24 27 Tree-Insert(T, z) 1. y NIL 2. x root[t] 3. while x NIL 4. do y x 5. if key[z] < key[x] 6. then x left[x] 7. else x right[x] 8. p[z] y 9. if y = NIL 10. then root[t] z 11. else if key[z] < key[y] 12. then left[y] z 13. else right[y] z

Analysis of Insertion Initialization: O(1) While loop in lines 3-7 searches for place to insert z, maintaining parent y. This takes O(h) time. Lines 8-13 insert the value: O(1) TOTAL: O(h) time to insert a node. Tree-Insert(T, z) 1. y NIL 2. x root[t] 3. while x NIL 4. do y x 5. if key[z] < key[x] 6. then x left[x] 7. else x right[x] 8. p[z] y 9. if y = NIL 10. then root[t] z 11. else if key[z] < key[y] 12. then left[y] z 13. else right[y] z

Exercise: Sorting Using BSTs Sort (A) for i 1 to n do tree-insert(a[i]) inorder-tree-walk(root) What are the worst case and best case running times? In practice, how would this compare to other sorting algorithms?

Tree-Delete (T, x) if x has no children case 0 then remove x if x has one child case 1 then make p[x] point to child if x has two children (subtrees) case 2 then swap x with its successor perform case 0 or case 1 to delete it TOTAL: O(h) time to delete a node

Deletion Pseudocode Tree-Delete(T, z) /* Determine which node to splice out: either z or z s successor. */ if left[z] = NIL or right[z] then y z else y Tree-Successor[z] /* Set x to a non-nil child of x, or to NIL if y has no children. */ 4. if left[y] NIL 5. then x left[y] 6. else x right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ 7. if x NIL 8. then p[x] p[y] /* Continued on next slide */

Deletion Pseudocode Tree-Delete(T, z) (Contd. from previous slide) 9. if p[y] = NIL 10. then root[t] x 11. else if y left[p[i]] 12. then left[p[y]] x 13. else right[p[y]] x /* If z s successor was spliced out, copy its data into z */ 14. if y z 15. then key[z] key[y] 16. copy y s satellite data into z. 17. return y

Correctness of Tree-Delete How do we know case 2 should go to case 0 or case 1 instead of back to case 2? Because when x has 2 children, its successor is the minimum in its right subtree, and that successor has no left child (hence 0 or 1 child). Equivalently, we could swap with predecessor instead of successor. It might be good to alternate to avoid creating lopsided tree.

Binary Search Trees View today as data structures that can support dynamic set operations. Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. Can be used to build Dictionaries. Priority Queues. Basic operations take time proportional to the height of the tree O(h).