11 Binary Tree Data Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

11 Binary Tree Data Structures

Description:

BST search (3) Animation (successful search): To find which if any node of a BST contains an element equal to ... BST search (4) Animation (unsuccessful search) ... – PowerPoint PPT presentation

Number of Views:3621
Avg rating:3.0/5.0
Slides: 22
Provided by: watt50
Category:
Tags: binary | bst | data | structures | tree

less

Transcript and Presenter's Notes

Title: 11 Binary Tree Data Structures


1
11Binary Tree Data Structures
  • Binary trees and binary search trees.
  • Searching.
  • Traversal.
  • Implementation of sets using BSTs.

2
BST search (1)
  • Problem Search for a given target value in a
    BST.
  • Idea Compare the target value with the element
    in the root node.
  • If the target value is equal, the search is
    successful.
  • If target value is less, search the left subtree.
  • If target value is greater, search the right
    subtree.
  • If the subtree is empty, the search is
    unsuccessful.

3
BST search (2)
  • BST search algorithm
  • To find which if any node of a BST contains an
    element equal to target
  • 1. Set curr to the BSTs root.2. Repeat 2.1. I
    f curr is null 2.1.1. Terminate with answer
    none. 2.2. Otherwise, if target is equal to
    currs element 2.2.1. Terminate with answer
    curr. 2.3. Otherwise, if target is less than
    currs element 2.3.1. Set curr to currs left
    child. 2.4. Otherwise, if target is greater than
    currs element 2.4.1. Set curr to currs right
    child.

4
BST search (3)
  • Animation (successful search)

5
BST search (4)
  • Animation (unsuccessful search)

6
BST search (5)
  • Analysis (counting comparisons)
  • Let the BSTs size be n.
  • If the BST has depth d, the number of
    comparisons is at most d 1.
  • If the BST is well-balanced, its depth is
    floor(log2 n)
  • Max. no. of comparisons floor(log2 n) 1
  • Best-case time complexity is O(log n).
  • If the BST is ill-balanced, its depth is at most
    n1
  • Max. no. of comparisons n
  • Worst-case time complexity is O(n).

7
BST search (6)
  • Implementation as a Java method (in class BST)
  • public BSTNode search (Comparable target)
  • int direction 0 BSTNode curr
    root for ()
  • if (curr null) return
    null direction target.compareTo(curr.element)
    if (direction 0) return curr else if
    (direction lt 0) curr curr.left else curr
    curr.right

8
Binary tree traversal
  • Binary tree traversal Visit all nodes (elements)
    of the tree in some predetermined order.
  • We must visit the root node, traverse the left
    subtree, and traverse the right subtree. But in
    which order?
  • In-order traversal Traverse the left subtree,
    then visit the root node, then traverse the right
    subtree.
  • Pre-order traversal Visit the root node, then
    traverse the left subtree, then traverse the
    right subtree.
  • Post-order traversal Traverse the left subtree,
    then traverse the right subtree, then visit the
    root node.

9
Binary tree in-order traversal (1)
  • Schematic for in-order traversal

10
Binary tree in-order traversal (2)
  • Illustration
  • In-order traversal of a BST visits the elements
    in ascending order.

11
Binary tree in-order traversal (3)
  • Binary tree in-order traversal algorithm
    (generic)
  • To traverse, in in-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Traverse, in
    in-order, tops left subtree. 1.2. Visit
    top. 1.3. Traverse, in in-order, tops right
    subtree.2. Terminate.
  • This algorithm is generic the meaning of Visit
    is left open.

12
Example printing elements in order
  • Java method
  • public static void printInOrder (BSTNode top)
    // Print, in ascending order, all the elements
    in the BST subtree // whose topmost node is
    top. if (top ! null)
  • printInOrder(top.left) System.out.print
    ln(top.element) printInOrder(top.right)

Visit top (printing its element).
13
Binary tree pre-order traversal (1)
  • Schematic for pre-order traversal

14
Binary tree pre-order traversal (2)
  • Illustration

15
Binary tree pre-order traversal (3)
  • Binary tree pre-order traversal algorithm
    (generic)
  • To traverse, in pre-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Visit
    top. 1.2. Traverse, in pre-order, tops left
    subtree. 1.3. Traverse, in pre-order, tops
    right subtree.2. Terminate.

16
Binary tree post-order traversal (1)
  • Schematic for post-order traversal

17
Binary tree post-order traversal (2)
  • Illustration

18
Binary tree post-order traversal (3)
  • Binary tree post-order traversal algorithm
    (generic)
  • To traverse, in post-order, the subtree whose
    topmost node is top
  • 1. If top is not null 1.1. Traverse, in
    post-order, tops left subtree. 1.2. Traverse,
    in post-order, tops right subtree. 1.3. Visit
    top.2. Terminate.

19
Implementation of sets using BSTs (1)
  • Represent an (unbounded) set by a BST whose
    elements are the set members.

20
Implementation of sets using BSTs (2)
  • The BST representation of a set is not unique

21
Implementation of sets using BSTs (3)
  • Summary of algorithms and time complexities
Write a Comment
User Comments (0)
About PowerShow.com