Lecture 10COMPSCI.220.FS.T - 20041 Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS202 - Fundamental Structures of Computer Science II
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
© 2004 Goodrich, Tamassia (2,4) Trees
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Starting at Binary Trees
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture 8COMPSCI.220.FS.T Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap to obtain an O(n log n) worst-case sorting.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Data Structures AVL Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 367 Introduction to Data Structures Lecture 8.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AA Trees.
BCA-II Data Structure Using C
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
2-3-4 Trees Red-Black Trees
Binary Trees, Binary Search Trees
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Binary Trees, Binary Search Trees
Presentation transcript:

Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and delete data items Left-to-right ordering in a tree: for every node x, the values of all the keys k left in the left subtree are smaller than the key k parent in x and the values of all the keys k right in the right subtree are larger than the key in x: k parent x k right k left k left  k parent  k right

Lecture 10COMPSCI.220.FS.T Binary Search Tree Compare the left  right ordering in a binary search tree to the bottom  up ordering in a heap where the key of each parent node is greater than or equal to the key of any child node

Lecture 10COMPSCI.220.FS.T Binary Search Tree No duplicates! (attach them all to a single item) Basic operations: –find : find a given search key or detect that it is not present in the tree –insert : insert a node with a given key to the tree if it is not found –findMin : find the minimum key –findMax : find the maximum key –remove : remove a node with a given key and restore the tree if necessary

Lecture 10COMPSCI.220.FS.T BST: find / insert operations find is the successful binary search insert creates a new node at the point at which the unsuccessful search stops

Lecture 10COMPSCI.220.FS.T Binary Search Trees: findMin / findMax / sort Extremely simple: starting at the root, branch repeatedly left ( findMin ) or right ( findMax ) as long as a corresponding child exists The root of the tree plays a role of the pivot in quickSort As in QuickSort, the recursive traversal of the tree can sort the items: –First visit the left subtree –Then visit the root –Then visit the right subtree

Lecture 10COMPSCI.220.FS.T Binary Search Tree: running time Running time for find, insert, findMin, findMax, sort : O(log n) average-case and O(n) worst-case complexity (just as in Q uickSort ) BST of the depth about log n

Lecture 10COMPSCI.220.FS.T BST of the depth about n

Lecture 10COMPSCI.220.FS.T Binary Search Tree: node removal remove is the most complex operation: –The removal may disconnect parts of the tree –The reattachment of the tree must maintain the binary search tree property –The reattachment should not make the tree unnecessarily deeper as the depth specifies the running time of the tree operations

Lecture 10COMPSCI.220.FS.T BST: how to remove a node If the node k to be removed is a leaf, delete it If the node k has only one child, remove it after linking its child to its parent node Thus, removeMin and removeMax are not complex because the affected nodes are either leaves or have only one child

Lecture 10COMPSCI.220.FS.T BST: how to remove a node If the node k to be removed has two children, then replace the item in this node with the item with the smallest key in the right subtree and remove this latter node from the right subtree ( Exercise: if possible, how can the nodes in the left subtree be used instead? ) The second removal is very simple as the node with the smallest key does not have a left child The smallest node is easily found as in findMin

Lecture 10COMPSCI.220.FS.T BST: an Example of Node Removal

Lecture 10COMPSCI.220.FS.T Average-Case Performance of Binary Search Tree Operations Internal path length of a binary tree is the sum of the depths of its nodes: IPL   15 Average internal path length T(n) of the binary search trees with n nodes is O(n log n) depth

Lecture 10COMPSCI.220.FS.T Average-Case Performance of Binary Search Tree Operations If the n -node tree contains the root, the i -node left subtree, and the (n  i  1) -node right subtree, then: T(n) = n  1 + T(i) + T(n  i  1) because the root contributes 1 to the path length of each of the other n  1 nodes Averaging over all i; 0 ≤ i < n : the same recurrence as for Q uickSort : so that T(n) is O(n log n)

Lecture 10COMPSCI.220.FS.T Average-Case Performance of Binary Search Tree Operations Therefore, the average complexity of find or insert operations is T(n) ⁄ n = O(log n) For n 2 pairs of random insert / remove operations, an expected depth is O(n 0.5 ) In practice, for random input, all operations are about O(log n) but the worst-case performance can be O(n) 

Lecture 10COMPSCI.220.FS.T Balanced Trees Balancing ensures that the internal path lengths are close to the optimal n log n The average-case and the worst-case complexity is about O(log n) due to their balanced structure But, insert and remove operations take more time on average than for the standard binary search trees – AVL tree (1962: Adelson-Velskii, Landis) – Red-black and AA-tree – B-tree (1972: Bayer, McCreight)

Lecture 10COMPSCI.220.FS.T AVL Tree An AVL tree is a binary search tree with the following additional balance property: –for any node in the tree, the height of the left and right subtrees can differ by at most 1 –the height of an empty subtree is  1 The AVL-balance guarantees that the AVL tree of height h has at least c h nodes, c  1, and the maximum depth of an n -item tree is about log c n

Lecture 10COMPSCI.220.FS.T AVL Tree Let S h be the size of the smallest AVL tree of the height h ( it is obvious that S 0  1, S 1  2 ) This tree has two subtrees of the height h  1 and h  2, respectively, by the balance condition It follows that S h  S h  1  S h  2  1, or S h = F h  3  1 where F i is the i -th Fibonacci number

Lecture 10COMPSCI.220.FS.T AVL Tree Therefore, for each n - node AVL tree: Thus, the worst-case height is at most 44% more than the minimum height of the binary trees