SlideServe
Menu
Préférences
© SlideServe · v2026

Binary Search Trees: Conceptual Implementation and Performance Analysis

sunee avatar
18 diapositives · 314 Views · Sep 05, 2014

Learn about Binary Search Trees (BST), conceptual implementation of Bag interface using BST, performance of BST Bag operations, structure of BST, insertion, removal, and search operations with examples and complexity analysis.

Binary Search Trees: Conceptual Implementation and Performance Analysis - slide 1
01 / 18Binary Search Trees: Conceptual Implementation and Performance Analysis
1 / 17

Télécharger la présentation

Binary Search Trees: Conceptual Implementation and Performance Analysis
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CS261 Data Structures Binary Search Trees Concepts

  2. Goals • Introduce the Binary Search Tree (BST) • Conceptual implementation of Bag interface with the BST • Performance of BST Bag operations

  3. Binary Search Tree • Binary search trees are binary trees where every node’s value is: • Greater than all its descendents in the left subtree • Less than or equal to all its descendents in the right subtree • If tree is reasonably full (well balanced), searching for an element is O(log n). Why?

  4. Intuition 50 25 75 20 35 60 85 30 45 65 80

  5. Binary Search Tree: Example Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

  6. BST Bag: Contains • Start at root • At each node, compare value to node value: • Return true if match • If value is less than node value, go to left child (and repeat) • If value is greater than node value, go to right child (and repeat) • If node is null, return false • Dividing in half each step as you traverse path from root to leaf (assuming reasonably full!!!)

  7. BST Bag: Contains/Find Example Alex Abner Agnes Angela Abigail Alice Audrey Adela Adam Agnes Allen Arthur Object to find

  8. BST Bag: Add Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur • Do the same type of traversal from root to leaf • When you find a null value, create a new node

  9. BST Bag: Add Example Alex Abner Aaron Angela Abigail Alice Audrey Adela Aaron Adam Agnes Allen Arthur Object to add “Aaron” should be added here Before first call to add

  10. BST Bag: Add Example Alex Abner Ariel Angela Abigail Alice Audrey Adela Aaron Adam Ariel Agnes Allen Arthur Next object to add “Ariel” should be added here After first call to add

  11. BST Bag: Remove Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur How would you remove Abigail? Audrey? Angela?

  12. Who fills the hole? • Answer: the leftmost child of the right subtree(smallest element in right subtree) • Try this on a few values • Alternatively: The rightmost child of the left subtree

  13. BST Bag: Remove Example Alex Abner Angela Abigail Alice Audrey Adela Adam Agnes Allen Arthur Element to remove Replace with: leftmost(rght) Before call to remove

  14. BST Bag: Remove Example Alex Abner Arthur Audrey Abigail Alice Adela Adam Agnes Allen After call to remove

  15. Special Case Angela Alice Audrey Allen Arthur • What if you don’t have a right child? • Try removing “Audrey” • Simply return left child

  16. Complexity Analysis (contains) • If reasonably full, you’re dividing in half at each step: O(log n) • Alternatively, we are running down a path from root to leaf • We can prove by induction that in a complete tree (which is reasonably full), the path from root to leaf is bounded by floor(log n), so O(log n)

  17. Binary Search Tree: Useful Collection? • We’ve shown all Bag operations to be proportional to the length of a path, rather than the number of elements in the tree • We’ve also said that in a reasonably full tree, this path is bounded by : floor( (log2n)) • This Bag is faster than our previous implementations!

  18. Comparison • Average Case Execution Times