Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees.

Similar presentations


Presentation on theme: "Binary Search Trees."— Presentation transcript:

1 Binary Search Trees

2 Binary Search Trees Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently. A Binary Search Tree (BST) is a binary tree with the following properties: For every node X: All the keys in its left subtree are smaller than or equal to the key value in node X All the keys in its right subtree are larger than the key value in node X

3 Binary Search Trees: Examples
root root 14 10 15 11 8 18 16 C A D NOTE! When nodes of a BST are traversed by Inorder traversal the keys appear in sorted order

4 Searching for a key in a BST
How do you search BST for node with key x ? L P R

5 Searching for a key in a BST
search(root, x) compare x to key of root: - if x = key return true - if x < key => search in L - if x > key => search in R - if no subtree, return false search L or R in the exact same way Example: 14 10 15 11 8 16 x=10 is ??? X=19 is ??? Time complexity O(height of the tree)

6 Binary search – Object nodes
public class BSTNode { Comparable data; BSTNode left; BSTNode right; }

7 Searching for a key in a BST
public boolean search(Comparable obj) { int comp = data.compareTo(obj); if (comp==0) return true; if (comp>0 && left!=null) return left.search(obj); else if (comp<0 && right!=null) return right.search(obj); return false; } 10 6 22 2 7 14 13 19 4 9

8 Inserting a new key in a BST
How to insert a new key? The same procedure used for search also applies: Determine the location by searching. Search will fail. Insert new key where the search failed. Example: 10 8 5 3 4 2 9 Insert 4?

9 Inserting a new key in a BST
Design decision: if the key is found? throw exception do nothing multiple entries (our choice) replace current entry

10 Inserting a new key in a BST
public boolean insert(Comparable obj) { int comp = data.compareTo(obj); if (comp>0) { if (left!=null) return left.insert(obj); else {left = new BSTNode(obj,null,null); return true;} } else // new element greater { if (right!=null) return right.insert(obj); {right = new BSTNode(obj,null,null); // Main describes non-recursive method

11 Deleting from a BST CASE A: x is a leaf
To delete node with key x first you need to search for it. Once found, apply one of the following three cases CASE A: x is a leaf p p q q r x r delete x BST property maintained

12 Deleting from a BST cont.
Case B: x is interior with only one subtree L x q r delete x L q r BST property maintained

13 Deleting from a BST cont.
Case C: x is interior with two subtrees W q r delete x s Z BST property maintained t W x q r Z t s delete x

14 Deleting from a BST cont.
Case C cont: … or you can also do it like this q < x < r q is smaller than the smaller element in Z r is larger than the largest element in W r q W t r Z Can you see other possible ways ? s

15 Deleting from a BST cont.
Case C cont: … or you can also do it like this t s W x q r delete x Z t W s q r Z s W t q r Z OR

16 Deleting from a BST cont.
Remove node with two children (22) find next node in Inorder (23) copy next node data to this node ( ) remove next node instead (23) 10 6 22 2 7 14 13 19 4 9 26 23 28

17 Building a BST 1) Insert C 2) Insert A
Build a BST from a sequence of nodes read one a time Example: Inserting C A B L M (in this order!) 1) Insert C 2) Insert A C A C

18 Building a BST 3) Insert B 5) Insert M 4) Insert L C A B C A B L M C A

19 Building a BST NO! Different input sequences result in different trees
Is there a unique BST for letters A B C L M ? NO! Different input sequences result in different trees Inserting: A B C L M Inserting: C A B L M A B C L M C A B L M

20 BST Applications Sorting with a BST
Given a BST can you output its keys in sorted order? Yes, print the keys using Inorder traversal How can you find the minimum? How can you find the maximum? Find the Leftmost node Find the Rightmost node

21 BST Applications Find kth element in BST
Given a BST can you output the Kth element? Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © Addison Wesley

22 Complexity of Searching BST
It depends on the shape of the tree. For full or complete binary trees: best time analysis ………… O(1) worst time analysis ………… O(log N) Balanced Binary Tree: The heights of the two child subtrees of any node differ by at most one.

23 Complexity of Searching BST
Unfortunately, inserting and removing are uncontrolled in BST This may lead to a BST like this one: best time analysis O(1) worst time analysis O(N) A B C L M

24 Assuring Performance of BSTs
inserting and removing must rebalance tree to maintain performance Balanced trees are called AVL trees – we will not be studying these trees. 22 10 10 10 26 22 22 26 26


Download ppt "Binary Search Trees."

Similar presentations


Ads by Google