Binary search trees :

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

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

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

Data Structures. Dynamic Sets

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

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

12 Binary Search Tree

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

Lecture 6: Analysis of Algorithms (CS )

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

Binary Search Trees, etc.

Lecture: Analysis of Algorithms (CS )

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Animations. 01/29/04 Lecture

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

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

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

External Sorting Methods

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

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

CMSC 441: Algorithms. Hillol Kargupta, Professor.

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


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))

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

Lecture 5: Scheduling and Binary Search Trees

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

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

Lecture 7. Binary Search Trees / AVL Trees

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

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

TREES. Trees - Introduction

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

Balanced search trees

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

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

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

Binary Trees, Binary Search Trees

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

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

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

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

March 20/2003 Jayakanth Srinivasan,

CS102 Binary Search Trees

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

Analysis of Algorithms

SFU CMPT Lecture: Week 9

Binary Trees

Associate Professor Dr. Raed Ibraheem Hamed

1 A node in a tree. Trees. Datatypes ISC

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Searching: Introduction

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

COMP171. AVL-Trees (Part 1)

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

Chapter 20: Binary Trees

CMPS 102 Solutions to Homework 6

Augmenting Data Structures

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Binary Search Trees. What is a Binary Search Tree?

Binary Search Trees. See Section 11.1 of the text.

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

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

Data Structures Week #6. Special Trees

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

CPS 231 Exam 2 SOLUTIONS

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

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

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Range Searching and Windowing

Search Trees: BSTs and B-Trees

Friday Four Square! 4:15PM, Outside Gates

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?

Successor/Predecessor Rules in Binary Trees

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

CS350: Data Structures Binary Search Trees

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

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.

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CMSC351 - Fall 2014, Homework #2

COMP251: Red-black trees

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

CS 350 : Data Structures Binary Search Trees

Priority queue. Advanced Algorithmics (6EAP) MTAT Heaps. Binary heap. Heap/Priority queue. Binomial heaps:

search(i): Returns an element in the data structure associated with key i

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

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

Binary Trees. Examples:

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

Binary Trees. Height 1

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

Transcription:

Binary search trees

Binary search trees : Search trees are data structures that generally offer the following dynamic-set operations : SEARCH MINIMUM MAXIMUM PREDECESSOR SUCCESSOR INSERT DELETE Basic operations on these trees take time proportional to the height of the tree. For complete balanced trees with n nodes, this height is of log n. However, since trees are not always balanced, the worst case is in θ(n). To represent the nodes of these tree, we usually use linked data structures in which each node of the tree is an object. These have the following fields : key: The usual value used to compare the different objects. p : The parent of the current node. left : The left child of the current node. right : The right child of the current node.

Binary-search-tree property : Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] key[x]. If y is a node in the right subtree of x, then key[x] key[y]. This property allows us to easily output all the keys in a binary search tree in sorted order : function INORDER T REE W ALK(x) if x NIL then INORDER T REE W ALK(left[x]) print key[x] INORDER T REE W ALK(right[x]) Note that the name inorder tree walk comes from the fact that this algorithm prints the value of the key of the current node between the values of the left subtree and the values of the right subtree. Similarly, a preorder tree walk prints the root before the values in either subtree, and a postorder tree walk prints the root after the values in its subtrees.

Searching : function T REE SEARCH(x, k) if x = NIL or k = key[x] then return x if k < key[x] then return T REE SEARCH(left[x], k) else return T REE SEARCH(right[x], k) This algorithm starts at node x and traces its way downward until it finds the node with the key equal to k or determines that no such node exist. The maximum number of keys encountered during the recursive search is the length of the path going from x to the node with key k. The running time is therefore in O(h), where h is the height of the tree. function IT ERAT IV E T REE SEARCH(x, k) while x NIL and k key[x] do if k < key[x] then x left[x] else x right[x]

Minimum and Maximum : The minimum in a binary-search-tree can always be found by following the left pointers from the root until a NIL is encountered. function T REE MINIMUM(x) while left[x] NIL do x left[x] return x Similarly, the maximum can be found by following the right pointers until a NIL is found. function T REE MAXIMUM(x) while right[x] NIL do x right[x] return x Both of these algorithms are in O(h) since they trace paths downward in the tree.

Predecessor and Successor : If all keys are distinct, the successor of a node x is the node with the smallest key greater then key[x]. The following algorithm returns this successor or N IL if the key of x is the largest key in the tree. function T REE SUCCESSOR(x) if right[x] NIL then return T REE MINIMUM(right[x]) y p[x] while y NIL and x = right[y] do x y y p[y] return y If the right subtree of x is not NIL, then the sucessor is the minimum of this subtree. Otherwise, the returned value is the closest ancestor who s left subtree includes x. The algorithm for T REE P REDECESSOR is the symmetry of the T REE SUCCESSOR algorithm. The running time for a tree of height h is O(h).

Insertion : function T REE INSERT (T, z) y NIL x root[t ] while x NIL do y x if key[z] < key[x] then x left[x] else x right[x] p[z] y if y = NIL then root[t ] z else if key[z] < key[y] then left[y] z else right[y] z This algorithm starts at the root of the tree and traces a path downward. The pointer x traces this path while the pointer y is maintained as the parent of x. When x is found to be NIL then the appropriate position has been found and the new value can be inserted as a child of y. Like the previous algorithms, T REE IN SERT O(h). is in

Deletion : function T REE DELET E(T, z) if left[z] = NIL or right[z] = NIL then y z else y T REE SUCCESSOR(z) if left[y] NIL then x left[y] else x right[y] if x NIL then p[x] p[y] if p[y] = NIL then root[t ] x else if y = left[p[y]] then left[p[y]] x else right[p[y]] x if y z then key[z] key[y] \\ If y has other fields, copy them, too. return y This algorithm, like all others, is in O(h).