1 / 12

COMP 171 Data Structures and Algorithms

COMP 171 Data Structures and Algorithms. Tutorial 6 Binary Search Trees. Binary Search Tree. Binary Tree Node X Key values in left subtree ≦ key value of X Key values in right subtree ≧ key value of X. BST Structure. Node Key Data Left (ptr to Node) Right (ptr to Node)

merton
Download Presentation

COMP 171 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 171Data Structures and Algorithms Tutorial 6 Binary Search Trees

  2. Binary Search Tree • Binary Tree • Node X • Key values in left subtree ≦ key value of X • Key values in right subtree ≧ key value of X

  3. BST Structure • Node • Key • Data • Left (ptr to Node) • Right (ptr to Node) • Parent (ptr to Node)

  4. Tree Search TreeSearch(x, k) while x≠NIL and k≠key[x] if k < key[x] then x ← left[x] else x ← right[x] end if end while End TreeSearch

  5. Tree Minimum & Maximum TreeMinimum(x) while left(x) ≠NIL x ← left(x) end while return x End TreeMinimum TreeMaximum(x) while right(x) ≠NIL x ← right(x) end while return x End TreeMaximum

  6. Tree Walk: Inorder Inorder(x) if x≠NIL then Inorder(left(x)) print key(x) Inorder(right(X)) end if End Inorder • Θ(n)

  7. Preorder Preorder(x) if x≠NIL then print key(x) Preorder(left(x)) Preorder(right(X)) end if End Preorder • Θ(n)

  8. Postorder Postorder(x) if x≠NIL then Postorder(left(x)) Postorder(right(X)) print key(x) end if End Postorder • Θ(n)

  9. Tree Successor TreeSuccessor(x) // -----case I------ if right(x)≠NIL then return TreeMinimum(right(x)) end if // -----case II----- y ← parent(x) while y≠NIL and x=right(y) x ← y y ← parent(y) end while End TreeSuccessor

  10. Insertion TreeInsert(T, z) // -----Find position----- y ← NIL x ← root(T) while x≠NIL y ← x if key(z) < key(x) then x ← left(x) else y ← right(x) end if end while // -----Insert into position----- Parent(z) ← y if y = NIL then // T is empty root[T] = z else if key[z] < key[y] then left[y] ← z else right[y] ← z end if end if End TreeInsert

  11. Deletion TreeDelete(T, z) if left(z)=NIL or right(z)=NIL then y ← z else y ← TreeSuccessor(z) if left(y)≠NIL then x ← left(y) else x ← right(y) if x≠NIL then parent(x) ← parent(y) if parent(y)=NIL then root(T) ← x else if y = left(parent(y)) then left(parent(y)) ← x else right(parent(y)) ← x if y≠z then key(z) ← key(y) return y End TreeDelete

More Related