Download presentation
Presentation is loading. Please wait.
Published byDustin Edwards Modified over 6 years ago
1
Binary Search Trees Why this is a useful data structure. Terminology Implementation Considerations
2
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
3
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!
4
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)
5
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: 101520 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.
6
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).
7
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)?
8
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.
9
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.
10
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
11
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);
12
Binary Search Trees A binary search tree Not a binary search tree
13
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)
14
Implementation for a BST Class:
addNode() searchNode() displayTree() deleteNode()
15
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)
16
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.
17
deleteNode() Important:
When we delete a node, we need to consider how to re-assign the children of the deleted node.
18
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
19
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)
20
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.