Sei sulla pagina 1di 6

Which

are Binary Search Trees (BST)?

Binary Search Trees

So, a binary tree T is a binary search tree if T is a single node. No! All the values on the left subtree must be less than the value at the root; and 44 is greater than the root. More complete: A binary tree T is a binary search tree if T is a single node. T has two subtrees TL and TR, where o all the values in TL are less than the root of T, o all the values in TR are greater than the root of T Is null a binary search tree? Yes! Correction: A binary tree T is a binary search tree if T is an empty tree T is a single node. T has two subtrees TL and TR, where o all the values in TL are less than the root of T, o all the values in TR are greater than the root of T

Yes!

No! The right subtree is not a binary search tree

Correct definition: A binary tree T is a binary search tree if T is empty, or T has two subtrees TL and TR, where o all the values in TL are less than the root of T, o all the values in TR are greater than the root of T, and o TL and TR are binary search trees. For tree of n nodes: What is the max height? O(n) What is the min height? O(log n) What is the runtime of contains? (see the code below) Best case: O(1) - when the element is at the root Worst case: O(n) when the tree is essentially a linked list Expected case: O(log n) when the tree is relatively balanced Warning: Do not search both sides of the tree (two recursive calls) !!! That would have an O(n) runtime. Knowing you only have to search one side is what makes BST fast when the tree is balanced!! If the tree is balanced, the runtime is O(log n) Recall log (n) is really, really fast! Exponentially faster than O(n)!

Adding an element to a BST : Where should we add the element? If the tree is empty, make it the root Otherwise attach it as a leaf node. Algorithm for Add: (see code below) Search for the element. If you find it return If you reach an empty tree, attach new node to the parent (from whence you came) Exercise: Draw the binary search tree that results from adding the following one- character strings to an empty tree in the following order: C A R N E G I E M E L L O N Do not add any duplicates values to your tree. If you add elements to a binary search tree, what are some examples of when you get a tree of maximum height? Add in ascending order Add in descending order

Add max, min, 2nd max, 2nd min, 3rd max, 3rd min, .

public class BinarySearchTree <E extends Comparable<E>> { private static class BTNode<E> { private E data; private BTNode<E> left; private BTNode<E> right; public BTNode(E element) { data = element; } } private BTNode<E> root; public BinarySearchTree() { root = null; } public booleana contains(E target) { // Is the tree empty? if (root == null) return false; BTNode<E> current = root; while (current != null) { int targetVSdata = target.compareTo(current.data); if (targetVSdata == 0) return true; else if (targetVSdata < 0) current = current.left; else current = current.right; } return false; }

public void add(E element) { root = add(root, element); } private BTNode<E> add(BTNode<E> t, E element) { if (t == null) { return new BTNode<E>(element); } // Don't add element if it is already in the tree if (element.compareTo(t.data) < 0) { t.left = add(t.left, element); } else if (element.compareTo(t.data) > 0) { t.right = add(t.right, element); } return t; } }

What properties (invariants) do we want for remove? Resulting tree must be a BST Runtime of remove is O(log n) for balanced trees Resulting tree remains somewhat balanced Which nodes are easiest to remove? 1 or 0 children Simply link the parent of node to remove to its child (which maybe null) Which nodes are the hardest to remove? 2 children Remove root (possibly of a subtree): Key Idea: Replace the root with another element in the tree What element can replace the root? The in-order predecessor or the in-order successor

Remove

Let's consider using the predecessor. Which subtree contains the predecessor? The left subtree What can we say about its value relative to the other nodes? Its the maximum of the left subtree Where is the predecessor? It is the right-most node of the left subtree At what possible places from the root might the predecessor be? Case 1: There is no predecessor; the roots left-subtree is empty Case 2: The predecessor is the left child of the root; The predecessor has no right child Case 3: The predecessor is the rightmost child of the roots left subtree

Potrebbero piacerti anche