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

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

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

Binary search trees :

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

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

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

Data Structures. Dynamic Sets

SFU CMPT Lecture: Week 9

12 Binary Search Tree

Lecture 6: Analysis of Algorithms (CS )

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

Lecture: Analysis of Algorithms (CS )

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

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

Binary Search Trees, etc.

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Partha Sarathi Mandal

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

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

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

CMSC 441: Algorithms. Hillol Kargupta, Professor.

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

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

Lecture 5: Scheduling and Binary Search Trees

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

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

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

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

Animations. 01/29/04 Lecture

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

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

CMSC351 - Fall 2014, Homework #2

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

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

Lecture 7. Binary Search Trees / AVL Trees

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?

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

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

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

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

Dictionaries. Priority Queues

CS102 Binary Search Trees

Binary Trees, Binary Search Trees

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

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

External Sorting Methods

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

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

TREES. Trees - Introduction

Searching: Introduction

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

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

COMP Analysis of Algorithms & Data Structures

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

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

Module 4: Priority Queues

Balanced search trees

COMP Analysis of Algorithms & Data Structures

CSI33 Data Structures

1 A node in a tree. Trees. Datatypes ISC

# from math import floor,log

COMP Analysis of Algorithms & Data Structures

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

The Heap Data Structure

Data structures. Organize your data to support various queries using little time and/or space

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


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

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

Algorithms. AVL Tree

CS350: Data Structures Binary Search Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Binary Search Trees. Analysis of Algorithms

Friday Four Square! 4:15PM, Outside Gates

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

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

Introduction to Algorithms 3 rd edition

Lecture 5: Sorting Part A

CS 350 : Data Structures Binary Search Trees

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

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

Data Structures Week #6. Special Trees

1 Figure 1: Zig operation on x Figure 2: Zig-zig operation on x

Algorithms and Data Structures (COMP 1004 Theory 2)

9. Heap : Priority Queue

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

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

Analysis of Algorithms

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

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

COMP171. AVL-Trees (Part 1)

Heaps and Priority Queues

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

Chapter 6 Heap and Its Application

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Recall: Properties of B-Trees

CE 221 Data Structures and Algorithms

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Transcription:

Binary search trees Support many dynamic-set operations, e.g. Search Minimum Maximum Insert Delete... Can be used as dictionary, priority queue... you name it Running time depends on height of tree: 1

if complete, then Θ(log n) worst-case if just one chain, then Θ(n) worst-case if random, then Θ(log n) expected

First of all, what is a binary search tree? it s a binary tree (small wonder here) represented using linked data structure nodes are objects objects store key data pointers to left child, right child, and parent (NULL if one is missing) root is only node with parent=null Binary-search-tree property x node in binary search tree 2

nodes y in left subtree of x have key[y] key[x] nodes y in right subtree of x have key[y] key[x] Note: heaps are different beasts

Def: inorder walk of binary tree: for each node x 1. visit left subtree (recursively) 2. print key of x 3. visit right subtree (recursively) Inorder-Tree-Walk(x) 1: if x NULL then 2: Inorder-Tree-Walk(left(x)) 3: print key(x) 4: Inorder-Tree-Walk(right(x)) 5: end if Interesting property of BSTs: Inorder walk of BST prints all keys in sorted order 3

Example for inorder walk: 6 5 10 3 8 12 2 4 Result is 2 3 4 5 6 8 10 12 4

Theorem. If x is root of n-node (sub)tree, then Inorder-Tree- Walk(x) takes Θ(n) time. Proof. T (n) time if procedure called on n-node (sub)tree Clearly T (0) = c for some constant c (test x NULL) Otherwise, suppose left subtree has k nodes, right subtree has n k 1 nodes. Then for n > 0 with d constant T (n) = T (k) + T (n k 1) + d Substitution method to show T (n) = (c + d)n + c 5

For n = 0 we have (c + d)n + c = c = T (0), OK T (n) = T (k) + T (n k 1) + d = [(c + d)k + c] + [(c + d)(n k 1) + c] + d = (c + d)k + c + (c + d)n (c + d)k (c + d) + c + d = (c + d)n + c (c + d) + c + d = (c + d)n + c

1. Searching Want to search for a node with given key k Return pointer to node if exists, otherwise NULL begin search at root follow path downward for node x on path, compare key[x] with k if equal, done if k < key[x], continue in left subtree (left subtree contains keys key[x]) if k > key[x], continue in right subtree (right subtree contains keys key[x]) 6

Tree-Search(x, k) 1: if x = NULL or k = key[x] then 2: return x 3: else if k < key[x] then 4: return Tree-Search(left[x], k) 5: else 6: return Tree-Search(right[x], k) 7: end if 7

15 6 18 3 7 17 20 2 4 13 9 Running time is O(h), h height of tree 8

2. Minimum/maximum Minimum can be found by following left pointers as long as possible (not necessarily to a leaf!) Tree-Minimum(x) 1: while left[x] NULL do 2: x left[x] 3: end while 4: return x 9

Maximum can be found by following right pointers as long as possible (not necessarily to a leaf!) Tree-Maximum(x) 1: while right[x] NULL do 2: x right[x] 3: end while 4: return x Both have running time O(h), h height of tree 10

3. Successor Meaning: successor in sorted order given by inorder walk 6 5 10 3 8 12 2 4 If right subtree of x is nonempty, then successor of x is leftmost node in right subtree ( the smallest among the larger ) Found by calling Tree-Minimum on right subtree Otherwise (right subtree is empty and x has a successor), then this is the lowest ancestor of x whose left child is also ancestor of x (A node is ancestor of itself) Proof: Homework :-) 11

Tree-Successor(x) 1: if right[x] NULL then 2: return Tree-Minimum(right[x]) 3: end if 4: y parent[x] 5: while y NULL and x = right[y] do 6: x y 7: y parent[y] 8: end while 9: return y Running time clearly O(h), h height of tree Tree-Predecessor symmetric Theorem. Operations Search, Minimum, Maximum, Successor, Predecessor run in time O(h) in BST of height h 12

4. Insertion We always insert into new leaves! Insertion of new element easy. From root, walk down tree according to value of new key and open new leaf 12 5 18 2 9 15 19 13 17 Running time again O(h) 13

Given node z with key[z] = v, left[z] = right[z] = NULL Tree-Insert(T, z) 1: y NULL, x root[t ] 2: while x NULL do 3: y x 4: if key[z] < key[x] then 5: x left[x] 6: else 7: x right[x] 8: end if 9: end while 10: parent[z] y 11: if y = NULL then 12: root[t ] z /* T was empty */ 13: else if key[z] < key[y] then 14: left[y] z 15: else 16: right[y] z 17: end if 14

5. Deletion Given pointer to some node z. Three cases. 1. z has no children At z s parent parent[z], just replace link to z with NULL 2. z has one child splice out z, make new link between its parent and its child 3. z has two children splice out z s successor y (which has no left child, homework :-), and replace z s key and data with y s key and data 15

15 no children 5 16 3 12 20 z 10 13 18 23 6 7 15 5 16 3 12 20 10 18 23 6 7 16

one child 15 5 16 z 3 12 20 10 13 18 23 6 7 15 5 3 12 20 18 23 10 13 6 7 17

15 two children z 5 16 3 12 20 10 13 18 23 y 6 7 y 6 15 5 z 16 3 12 20 10 13 18 23 7 15 6 16 3 12 20 10 13 18 23 7 18

Tree-Delete(T, z) 1: if left[z] = NULL or right[z] = NULL then 2: y z 3: else 4: y Tree-Successor(z) 5: end if 6: if left[y] NULL then 7: x left[y] 8: else 9: x right[y] 10: end if 11: if x NULL then 12: parent[x] parent[y] 13: end if 14: if parent[y] = NULL then 15: root[t ] x 16: else if y = left[parent[y]] then 17: left[parent[y]] x 18: else 19: right[parent[y]] x 20: end if 21: if y z then 22: key[z] key[y] 23: copy y s data into z 19

24: end if 25: return y