SlideServe
Menu
Préférences
© SlideServe · v2026

Algorithms and Data Structures CMPSC 465

vondra avatar
18 diapositives · 373 Views · Oct 02, 2014

Algorithms and Data Structures CMPSC 465. Adam Smith. L ECTURES 22-23 Binary Search Trees. Heaps: Review. Heap-leap-jeep-creep(A): A.heap -size = 0 For i =1 to len (A) t mp = A[ i ] Heap-Insert( A,tmp ) For i = len (A) down to 1 tmp = Heap-Extract-Max(A) A[ i ]= tmp

Algorithms and Data Structures CMPSC 465 - slide 1
01 / 18Algorithms and Data Structures CMPSC 465
1 / 17

Télécharger la présentation

Algorithms and Data Structures CMPSC 465
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. Algorithms and Data StructuresCMPSC 465 Adam Smith LECTURES22-23 Binary Search Trees

  2. Heaps: Review • Heap-leap-jeep-creep(A): • A.heap-size = 0 • For i=1 to len(A) • tmp = A[i] • Heap-Insert(A,tmp) • For i=len(A) down to 1 • tmp = Heap-Extract-Max(A) • A[i]=tmp • What does this do? • What is the running time of this operation?

  3. Trees • Rooted Tree: collection of nodes and edges • Edges go down from root (from parents to children) • No two paths to the same node • Sometimes, children have “names”(e.g. left, right, middle, etc)

  4. 3 8 1 2 6 5 7 Binary Search Trees • Binary tree: every node has 0, 1 or 2 children • BST property: • If y is in left subtree of x, then key[y] <= key[x] • same for right

  5. 3 8 1 2 6 5 7 Binary Search Trees • Binary tree: every node has 0, 1 or 2 children • BST property: • If y is in left subtree of x, then key[y] <= key[x] • same for right

  6. Implementation • Keep pointers to parent and both children • Each NODE has • left: pointer to NODE • right: pointer to NODE • p: pointer to NODE • key: real number (or other type that supports comparisons)

  7. 3 8 1 2 6 5 7 Drawing BST’s • To help you visualize relationships: • Drop vertical lines to keep nodes in right order • For example: picture shows the only legal location to insert a node with key 4 1 2 3 5 6 7 8

  8. n n-1 1 Height of a BST can be... • ... as little as log(n) • full balanced tree • ... as much as n-1 • unbalanced tree ...

  9. Searching a BST • Running time: Θ(height)

  10. Insertion • Find location of insertion, keep track of parent during search • Running time: Θ(height)

  11. Tree Min and Max • Running time: Θ(height)

  12. Tree Successor • Running time: Θ(height)

  13. Traversals (not just for BSTs) • Traversal: an algorithm that visits every node in the tree to perform some operation, e.g., • Printing the keys • Updating some part of the structure • 3 basic traversals for trees • Inorder • Preorder • Postorder

  14. Inorder traversal • Running time: Θ(n)

  15. Postorder traversal • Write pseudocode that computes • height of a BST • number of nodes in a BST • average depth • ... • Example: height • Find-height(T) • if T==NIL return -1 • else • h1 := Find-height(T.left) • h2 := Find-height(T.left) • return max(h1, h2) + 1

  16. Preorder Traversal • Recall that insertion order affects the shape of a BST • insertion in sorted order: height n-1 • random order: height O(log n) with high probability (we will prove that later in the course) • Write pseudocode that prints the elements of a binary search tree in a plausible insertion order • that is, an insertion order that would produce this particular shape • (print nodes in a preorder traversal)

  17. Exercise on insertion order • Exercise: Write pseudocode that takes a sorted list and produces a “good” insertion order (that would produce a balanced tree) • (Hint: divide and conquer: always insert the median of a subarray before inserting the other elements)

  18. Deletion • Cases analyzed in book • Running time: Θ(height)