Binary Search Trees (BSTs) 18 February 2003. 2 Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Binary Trees Chapter 6.
Splay Trees and B-Trees
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
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,
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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.
COMP261 Lecture 23 B Trees.
AA Trees.
File Organization and Processing Week 3
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Multiway Search Trees Data may not fit into main memory
BST Trees
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Binary Search Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 202 Trees.
Binary Search Trees.
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Search Trees (BSTs) 18 February 2003

2 Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information including a unique key value, and associated data. A binary tree is a BST iff, for every node n in the tree: – All keys in n’s left subtree are less than the key n – All keys in n’s right subtree are greater than the key n. Note: if duplicate keys are allowed, then nodes with values that are equal to the key in node n can be either in n’s left subtree or in its right subtree (but not both).

3 BSTs

4 Not BSTs

5 BSTs are Not Unique

6 Importance The reason binary-search trees are important is that the following operations can be implemented efficiently using a BST: – insert a key value – determine whether a key value is in the tree – remove a key value from the tree – print all of the key values in sorted order

7 Lookup In general, to determine whether a given value is in the BST, we will start at the root of the tree and determine whether the value we are looking for: – is in the root – might be in the root’s left subtree – might be in the root’s right subtree There are actually two special cases: – The tree is empty; return null. – The value is in the root node; return the value.

8 Lookup If neither special case holds, a recursive lookup is done on the appropriate subtree. Since all values less than the root’s value are in the left subtree, and all values greater than the root’s value are in the right subtree, there is no point in looking in both subtrees

9 Pseudo Code The pseudo code for the lookup method uses a recursive method lookup(BST, searchkey) if (BST = null) return null; if (BST.key = searchkey) return BST.key; if (BST.key > searchkey) return lookup(BST.left, searchkey); else return lookup(BST.right, searchkey); left key right

10 Look for

11 Searching for > 9 so go to the right subtree Found! 12 < 13 so go to the left subtree

12 Search for > 13 so go to the right subtree 15 < 16 so go to the left subtree. It does not exist so the search fails and it returns null

Animation ml

14 Analysis How much time does it take to search for a value in a BST? Note that lookup always follows a path from the root down towards a leaf. In the worst case, it goes all the way to a leaf. Therefore, the worst-case time is proportional to the length of the longest path from the root to a leaf (the height of the tree).

15 Worst Case What is the relationship between the number of nodes in a BST and the height of the tree? This depends on the “shape” of the tree. In the worst case, all nodes have just one child, and the tree is essentially a linked list.

16 Worst Case This tree has 5 nodes, and has height = 5. Searching for values in the range 16-19, and will require following the path from the root down to the leaf (the node containing the value 20) Requires time proportional to the number of nodes in the tree

17 Best Case In best case, all nodes have 2 children All leaves are at the same depth This tree has 7 nodes, and height =

18 Best Case Tree Height In general, a “full” tree will have height approximately log 2 (N), where N is the number of nodes in the tree. The value log 2 (N) is (roughly) the number of times you can divide N by two, before you get to zero. For example: 7/2 = 3 divide by 2 once3/2 = 1 divide by 2 a second time 1/2 = 0 divide by 2 a third time, the result is zero so quit So log 2 (7) is approximately equal to 3.

19 Summary The worst-case time required to do a lookup in a BST is O(height of tree). The worst case (a “linear” tree) is O(N), where N is the number of nodes in the tree. In the best case (a “full” tree) we get O(log N).

20 Inserting (2) 15 < 16 and no left subtree (1) 15 > 13 so go to right subtree (3) So insert 15 as left child

21 Complexity The complexity for insert is the same as for lookup In the worst case, a path is followed all the way to a leaf.

22 Delete If the search for the node containing the value to be deleted succeeds, there are several cases to deal with: 1. The node to delete is a leaf (has no children). 2. The node to delete has one child. 3. The node to delete has two children

23 Deletion If KeyToDelete in not in the tree, the tree is simply unchanged. We have to be careful that we do not “orphan” any nodes when we remove one. When the node to delete is a leaf, we want to remove it from the BST by setting the appropriate child pointer of its parent to null (or by setting root to null if the node to be deleted is the root, and it has no children).

24 Delete a leaf (15)

25 Delete a node with one child (16)

26 Messy Case The hard case is when the node to delete has two children. To delete n, we can't replace node n with one of its children, because what would we do with the other child? We replace node n with another node, x, lower down in the tree, then (recursively) delete node x.

27 Deletion What node can we use to replace node n? The tree must remain a BST (all of the values in n’s left subtree are less than n, and all of the values in n’s right subtree are greater than n) There are two possibilities that work: the node in the left subtree with the largest value, or the node in the right subtree with the smallest value. To find that node, we just follow a path in the right subtree, always going to the left child, since smaller values are in left subtrees. Once the node is found, we copy its fields into node n, then we recursively delete the copied node.

28 Deletion (8)

29 Delete (8) Replace with (7)

30 Delete (8) Replace with (9)

31 Keeping BSTs Efficient In best of all worlds, BST is fully balanced at all times. In practical world, need BSTs to be “almost” balanced. A lot of CS research and energy has gone into the problem of how to keep binary trees balanced.

32 Height Balanced: AVL Trees An AVL tree is a binary tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are AVL.

33 Splay Trees Self-adjusting data structure Splay trees are BSTs that move to the root the most recently accessed node. Nodes that are frequently accessed tend to cluster at the top of the tree driving those rarely accessed toward the leaves. Splay trees can become highly unbalanced, but over the long term do perform well.