Binary Search Trees Why this is a useful data structure. Terminology

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 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,
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.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
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,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
Trees Chapter 15.
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
UNIT III TREES.
Cinda Heeren / Geoffrey Tien
Binary Search Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Source: Muangsin / Weiss
COMP 103 Binary Search Trees.
Tree data structure.
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees, Binary Trees, and Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSE 332: Data Abstractions Binary Search Trees
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
CSE 326: Data Structures Lecture #7 Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Trees Why this is a useful data structure. Terminology Implementation Considerations

Consider the following example: Personnel Dictionary Data Structure Operations: insert search delete Adrien Dave Hannah … insert John Dictionaries associate some key with a value, just like a real dictionary (where the key is a word and the value is its definition). This is probably the most valuable and widely used ADT we’ll hit. I’ll give you an example in a minute that should firmly entrench this concept. find Michael delete Fred

Personnel Dictionary Data Structure Requirements Fast insertion runtime Fast searching Fast deletion OK, how fast do we want these to be? In general, however, we want finds in particular and inserts and deletes if possible to be fast. O(log n) for all of them would be great. We’ll see later how to get an expected O(1) time for all of them. But can anyone think right now of a data structure that gives constant time search, at least, for dictionaries? ARRAYS!

Personnel Dictionary – Possible Implementations other than a Binary Search Tree unsorted ArrayList sorted LinkedList insert O(n) search+ O(n) O(1) search O(log n) delete search+ O(1)

Consider Searching a Binary Tree Task: Find the node 20. Given the structure of a binary tree, this task produces a search in the following sequence of nodes: 101520 10 5 15 Anyone notice anything interesting about that in-order listing? Everything in the left subtree is listed first. Then the root. Then everything in the right subtree. OK, let’s work out the code to make the in-order listing. Is there an iterative version that doesn’t use its own stack? Not really, no. So, recursion is probably OK here. Anyway, if the tree’s too deep for recursion, you must have a huge amount of data. If (n != null) inorder(n->left) cout << n inorder(n->right) 2 9 13 20 7 17 30 Searching a BST takes O(log n) comparisons to find a specified node.

Compare searches within data structures: BST unsorted ArrayList sorted LinkedList O(log n) O(n) Given the structure of a BST: Search: O(log n). Insertion: O(log n). Deletion: O(log n).

Why are Binary Trees useful data structures? The time it takes to search a linked lists is often prohibitive. The time it takes to delete an item in an array is prohibitive. Sorting a large array is prohibitive. A tree structure is efficient for search, insertion, and deletion. This is the only data structure for which the running time of most operations (search, insert, delete) is O(log N)?

Some Terminologies Path Length number of edges on the path. An edge is the link between two nodes. Path length from 6 to 1 is 2. Depth of a node length of the unique path from the root to that node The depth of a tree is equal to the depth of the deepest leaf. Depth of node 4 is 2. Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree is equal to the height of the root. In the figure, the height of the tree is 3.

Binary Trees A tree in which no node can have more than two children The depth of an “average” binary tree is considerably smaller than N, even though in the worst unbalanced case, the depth can be as large as N – 1.

Binary Search Tree Example: Expression Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary

Inorder Traversal Inorder traversal infix expression L: left node visit V: Visit node R: right node visit infix expression a+b*c+d*e+f*g //----------------------------------------------------------- //IN-ORDER TRAVERSAL public void displayInOrder() { inOrderRecursive(root); }   private void inOrderRecursive(Node myRoot) { if (myRoot != null) { inOrderRecursive(myRoot.left); System.out.println(myRoot.value); inOrderRecursive(myRoot.right);

Binary Search Trees A binary search tree Not a binary search tree

Binary search trees Average depth of a node is O(log N); Two binary search trees representing the same set. The first tree is balanced. Average depth of a node is O(log N); maximum depth of a node is O(N)

Implementation for a BST Class: addNode() searchNode() displayTree() deleteNode()

addNode() Begin at the root node. Search for a location down the tree. If the node is found, do nothing. Duplicate nodes should not be generated in a BST. Otherwise, insert the node at the last spot on the path traversed Example: addNode(13)

searchNode() Begin search at the root. If we find the node we are searching for, then we are done. If we are searching for a node with a value less than the current node, then we should search in the left subtree. If we are searching for a node with a value greater than the current node, then we should search in the right subtree.

deleteNode() Important: When we delete a node, we need to consider how to re-assign the children of the deleted node.

deleteNode() Three scenarios: (1) the node is a leaf Delete it immediately (2) the node has one child Adjust a pointer from the parent to bypass that node

deleteNode() … Scenario 3 (3) the node has 2 children replace the key of that node with the minimum element at the right subtree delete the minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. Time complexity = O(height of the tree)

Try: findMin() and findMax() Return the node containing the smallest element in the tree Start at the root and go left as long as there is a left child. The stopping point is the smallest element Similarly for findMax Time complexity = O(height of the tree)