BINARY SEARCH TREE PERFORMANCE

Similar documents
Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

From Last Time: Remove (Delete) Operation

How To Create A Tree From A Tree In Runtime (For A Tree)

Analysis of Algorithms I: Binary Search Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

CSE 326: Data Structures B-Trees and B+ Trees

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

A Comparison of Dictionary Implementations

Persistent Binary Search Trees

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Fundamental Algorithms

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Chapter 14 The Binary Search Tree

Data Structures and Algorithms

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Big Data and Scripting. Part 4: Memory Hierarchies

Symbol Tables. Introduction

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Binary Heaps. CSE 373 Data Structures

Cpt S 223. School of EECS, WSU

Physical Data Organization

An Evaluation of Self-adjusting Binary Search Tree Techniques

Converting a Number from Decimal to Binary

TREE BASIC TERMINOLOGIES

Introduction to Data Structures and Algorithms

Binary Heap Algorithms

A Randomized Self-Adjusting Binary Search Tree

Binary Search Trees. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

Lecture 4: Balanced Binary Search Trees

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Ordered Lists and Binary Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees

Analysis of Algorithms I: Optimal Binary Search Trees

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Binary Search Trees 3/20/14

Fast Sequential Summation Algorithms Using Augmented Data Structures

Why Use Binary Trees?

Data Warehousing und Data Mining

Algorithms Chapter 12 Binary Search Trees

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1)

Data Structures Fibonacci Heaps, Amortized Analysis

Laboratory Module 6 Red-Black Trees

IMPLEMENTING CLASSIFICATION FOR INDIAN STOCK MARKET USING CART ALGORITHM WITH B+ TREE

Classification/Decision Trees (II)

Databases and Information Systems 1 Part 3: Storage Structures and Indices

Rotation Operation for Binary Search Trees Idea:

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Data storage Tree indexes

An Immediate Approach to Balancing Nodes of Binary Search Trees

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

Binary Search Trees. Ric Glassey

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees CMPSC 122

Structure for String Keys

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Simple Balanced Binary Search Trees

M-way Trees and B-Trees

When to use splay trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Full and Complete Binary Trees

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Algorithms and Data Structures

A Non-Linear Schema Theorem for Genetic Algorithms

State History Storage in Disk-based Interval Trees

Lecture 2 February 12, 2003

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

6 March Array Implementation of Binary Trees

Chapter 8: Binary Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Vector storage and access; algorithms in GIS. This is lecture 6

Binary Search Trees (BST)

International Journal of Software and Web Sciences (IJSWS)

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Data Structures For IP Lookup With Bursty Access Patterns

Exam study sheet for CS2711. List of topics

Randomized Binary Search Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

DYNAMIC DOMAIN CLASSIFICATION FOR FRACTAL IMAGE COMPRESSION

Efficient representation of integer sets

1 Introduction to Internet Content Distribution

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

GRAPH THEORY LECTURE 4: TREES

Algorithms and Data Structures Written Exam Proposed SOLUTION

Structural Design Patterns Used in Data Structures Implementation

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Chapter 13: Query Processing. Basic Steps in Query Processing

International journal of Engineering Research-Online A Peer Reviewed International Journal Articles available online

Database Systems. Session 8 Main Theme. Physical Database Design, Query Execution Concepts and Database Programming Techniques

International Journal of Advanced Research in Computer Science and Software Engineering

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

APP INVENTOR. Test Review

MAC Sublayer. Abusayeed Saifullah. CS 5600 Computer Networks. These slides are adapted from Kurose and Ross

Transcription:

BINARY SEARCH TREE PERFORMANCE Operation Best Time Average Time Worst Time (on a tree of n nodes) Find Insert Delete O(lg n)?? O(lg n)?? O(n) Fastest Running Time The find, insert and delete algorithms start at the tree root and a follow path down to, at worst case, the leaf at the very lowest level. The total number of steps of these algorithms is, therefore, the largest level of the tree, which is called the depth of the tree. The best (fastest) running time occurs when the binary search tree is full in which case the search processes used by the algorithms perform like a binary search. Let's verify this. A full binary tree is one in which nodes completely fill every level. For example, here are the unique full binary trees of which have tree depths of 0, 1 and 2: As you can verify by looking at the examples, a full tree has 1 node at level 0, 2 nodes at level 1, 4 nodes at level 2 and so on. Thus, it will have 2 d nodes at level d. Adding these quantities, the total number of nodes n for a full binary tree with depth d is: n = 2 0 + 2 1 + 2 2 + + 2 d = 2 d+1 1 For example, the full binary tree of depth 2 above has 2 3 1 = 7 nodes. The binary tree below is a full tree of depth 3 and has 2 4 1 = 15 nodes. Binary Search Tree Performance Page 1

Now, considering the formula above for the number of nodes in a full binary search tree: Solving for d, we get: n 1 2 n = 2 d+1 1 d 1 lg(2 ( d 1) lg(2) d 1 d 1 d lg( n) For example, the depth of a full binary search tree with 15 nodes is 3. In other words, the depth of a binary search tree with n nodes can be no less than lg(n) and so the running time of the find, insert and delete algorithms can be no less than lg(n). A full binary search tree is said to be balanced because every node's proper descendants are divided evenly between its left and right subtrees. Thus, the search algorithm employed by the find, insert and delete operations perform like a binary search. Slowest Running Time As a binary search tree becomes more and more unbalanced, the performance of the find, insert and delete algorithms degrades until reaching the worst case of O(n), where n is the number of nodes in the tree. For example, he number of comparisons needed to find the node marked A in the binary search trees below is 3, which is equal to the number of nodes. d 1 ) Binary Search Tree Performance Page 2

A A Binary search trees, such as those above, in which the nodes are in order so that all links are to right children (or all are to left children), are called skewed trees. Average Running Time The average running time of the binary search tree operations is difficult to establish because it is not clear that all binary search trees are equally likely. 1 It has been proven that when a binary search tree is constructed through a random sequence of insertions then the average depth of any node is O(lg n). For example, Figure 4.26 2 shows a 500-node randomly generated tree whose average node depth (calculated over all nodes) is 9.98. The problem arises when considering the effect of deletions since the deletion algorithm replaces a deleted node with one from its right subtree, thus favoring left imbalance. For example, Figure 4.27 3 shows the result of applying 250,000 random insert/remove operations to the tree of Figure 4.26. The tree still has 500 nodes but now has an average node depth of 12.51. 1 Mark Allen Weiss, Data Structures and Algorithm Analysis in Java, Addison-Wesley, 1999, page 117. 2 Ibid, page 117. 3 Ibid, page 117. Binary Search Tree Performance Page 3

Extreme left imbalance after deletion, however, does not always happen nor does it happen with all trees, so its effect on the average execution times is not yet understood. In short, the average execution time of the binary search tree operations appears (but cannot be proven) to be O(lg n). 4 4 For a list of references on this topic, see Ibid, page 152. Binary Search Tree Performance Page 4

Balanced versus Unbalanced Trees The time complexities of operations find, insert and delete on a binary search tree is: At best O(lg n), which occur when the tree is full At worst O(n) which occur when the tree is skewed Thought to be on average O(lg n) A balanced binary search tree is close to being full, although not necessarily completely full. It has, for each node, about the same number of nodes in its left subtree as in its right subtree. Thus, the find, insert and delete operations on a balanced tree give close to O(lg n) performance. The more unbalanced the tree becomes, the longer the search time grows until, at worst, it is O(n), occurring when the tree is skewed. If you can live with running times that average O(lg n) but may degenerate to O(n) in the worst case, then the binary search tree is sufficient for your purposes. If, however, you must have O(lg n) as your worst-case running time, then you must use a balanced binary search tree. Balancing Schemes On pages 458 and 459, your author mentions other schemes for keeping a binary search tree balanced. Here is a summary: The first to be invented was the AVL tree, named for Adelson-Velskii and Landis who invented it in 1962. The workings of the AVL scheme are visualized in David Galles' tool. For an explanation of this scheme, see Weiss. 5 The scheme your book covers in Chapter 9 is known as red-black trees. Its workings are visualized in your author's RB Tree Workshop applet. Your author covers another scheme in Chapter 10 called 2-3-4 trees A scheme called B-trees is used for searching large external files (i.e. files stored on disk). These are also covered in Chapter 10. Other schemes include AA trees and 2-3 trees. An alternative to balancing is called splay trees, which guarantees that any m consecutive tree operations has a worst-case time complexity of O(m lg n) without guaranteeing than each single operation has O(lg n) time complexity. Weiss covers this scheme. 6 5 Mark Allen Weiss, Data Structures and Algorithm Analysis in Java, Addison-Wesley, 1999, pages 118-138. 6 Ibid, pages pages 118-138. Binary Search Tree Performance Page 5