data structures in java
play

Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 - PowerPoint PPT Presentation

Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 Daniel Bauer 1 Contents 1. Binary Search Trees 2. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are unique, values need not be.


  1. Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 Daniel Bauer 1

  2. Contents 1. Binary Search Trees 2. Implementing Maps with BSTs

  3. Map ADT A map is collection of (key, value) pairs. • Keys are unique, values need not be. • Two operations: • get(key) returns the value associated with this key • put(key, value) (overwrites existing keys) • value1 key1 key2 value2 key3 value3 key4 How do we implement map operations efficiently?

  4. Binary Search Tree Property • Goal: Reduce finding an item to O(log N) • For every node n with value x • The value of all nodes • the value of all nodes in 6 in the right subtree of the left subtree of n are n are larger than x. smaller than x. 2 8 1 4 3

  5. Binary Search Tree Property • Goal: Reduce finding an item to O(log N) • For every node n with value x • The value of all nodes • the value of all nodes in 6 in the right subtree of the left subtree of n are n are larger than x. smaller than x. 2 8 1 4 3 7 This is not a search tree

  6. Binary Search Tree (BST) ADT r • A Binary Search Tree T 
 consists of T r T l • A root node r with value r item • A t most two non-empty subtrees T l and T r , connected by a directed edge from r. • T l and T r satisfy the BST property: • For all nodes s in T l , s item < r item . • F or all nodes t in T l , t item > r item . 
 • No value appears more than once in the BST.

  7. BST operations • insert(x) - add value x to T. • contains(x) - check if value x is in T. • findMin() - find smallest value in T. • findMax() -find largest value in T. • remove(x) -remove an item from T.

  8. BST operations: contains private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ 6 ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ // ¡Match ¡ } 2 8 1 4 3

  9. BST operations: contains private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ 6 6 ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ // ¡Match ¡ } 2 8 contains(3) 1 4 3

  10. BST operations: contains private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ 6 ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ // ¡Match ¡ } 2 2 8 contains(3) 1 4 3

  11. BST operations: contains private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ 6 ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ // ¡Match ¡ } 2 8 contains(3) 1 4 4 3

  12. BST operations: contains private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ 6 ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ // ¡Match ¡ } 2 8 contains(3) 1 4 3 3

  13. BST operations: findMin private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ 6 } 2 8 1 4 findMax is equivalent. 3

  14. BST operations: findMin private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ 6 6 } 2 8 findMin() 1 4 findMax is equivalent. 3

  15. BST operations: findMin private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ 6 } 2 2 8 findMin() 1 4 findMax is equivalent. 3

  16. BST operations: findMin private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ 6 } 2 8 findMin() 1 1 4 findMax is equivalent. 3

  17. BST operations: insert • Follow same steps as contains(X) • if X is found, do nothing. • Otherwise, contains stopped at node n. 
 Insert a new node for X as a left or right child of n. private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ 6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ 2 8 ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ 1 4 ¡ ¡return ¡t; ¡ ¡ ¡ } 3 Maintains the BST property.

  18. BST operations: insert • Follow same steps as contains(X) • if X is found, do nothing. • Otherwise, contains stopped at node n. 
 Insert a new node for X as a left or right child of n. private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ insert(5) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ 6 6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ 2 8 ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ 1 4 ¡ ¡return ¡t; ¡ ¡ ¡ } 3 Maintains the BST property.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend