CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

Print. Inorder traversal. Preorder traversal. Postorder traversal. Summary ... Possible Quiz Questions. What's a priority queue? Demo heap sort. ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 26
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 19
  • Binary Trees

2
Objectives
  • In this lecture, we will learn
  • Heap Application
  • Priority Queue Implementation
  • Heap Sort
  • Binary Trees

3
Binary Trees
4
Binary Tree
  • Each node may have up to two child node
  • Root - beginning of the tree
  • There is a unique path from the root to each
    every other node

5
Example
Level 0
Level 1
Level 2
Level 3
6
Properties
  • Height - max level
  • Max number of nodes at level n?
  • Minimum number of levels?

7
Binary Search Tree
  • If node y is in the left subtree of node x, then
    keyy ? keyx
  • Smaller values are in the left subtree
  • If y is in the right subtree of x, then keyy ?
    keyx
  • Larger values are in the right subtree

8
Binary Tree Node
  • template lttypename Tgt
  • class tnode
  • public
  • // tnode is a class
    implementation structure. making the
  • // data public simplifies
    building class functions
  • T nodeValue
  • tnodeltTgt left, right
  • // default constructor. data not
    initialized
  • tnode()
  • // initialize the data members
  • tnode (const T item, tnodeltTgt
    lptr NULL,
  • tnodeltTgt rptr
    NULL)

  • nodeValue(item), left(lptr), right(rptr)

9
Binary Tree Operations
  • createTree
  • destroyTree
  • emtpyTree
  • numberOfNodes
  • retrievalTreeElement
  • insertTreeElement
  • deleteTreeElement
  • replaceTreeElement
  • processAllNodes

10
Number of Node
  • numberOfNode(Tree t)
  • if (t is Null)
  • return 0
  • else
  • return numberOfNodes(t.left)
    numberOfNodes(t.right)1
  • end

11
Insertion
  • insertTreeElement(Tree t, T newElement)
  • if t is Null then
  • t.left Null
  • t.right Null
  • t.key newElement
  • else
  • if t.key lt newElement
  • insertTreeElement(t.right, newElement)
  • else
  • insertTreeElement(t.left, newElement)
  • endif
  • end

12
Deletion
  • deleteTreeElement(Tree t, T key)
  • if t.key gt key
  • deleteTreeElement(t.left, key)
  • else if t.key lt key
  • deleteTreeElement(t.right, key)
  • else
  • deleteNode(t)
  • endif
  • end

13
Deletion
  • deleteNode(t)
  • tmpPtr t
  • if t.left is Null then
  • t t.right
  • else if t.right is Null then
  • t t.left
  • else
  • findAndDeleteMax(t.left, tmpPtr)
  • t.key tmpPtr.key
  • endif
  • delete(tmpPtr)
  • end

14
Deletion
  • findAndDeleteMax(t, maxPtr)
  • if t.right is Null then
  • maxPtr t
  • t t.left
  • else
  • findAndDeleteMax(t.right, maxPtr)
  • endif
  • end

15
Search Key in the Tree
  • searchTree(Tree t, T key)
  • if t is Null return false
  • else if t.key gt key
  • return searchTree(t.left, key)
  • else if t.key lt key
  • return searchTree(t.right, key)
  • else return true
  • end

16
In-order Tree Traversal
  • Key of the root is printed between values in the
    left and right subtree
  • inorderTreeWalk(t)
  • if t is not Null then
  • inorderTreeWalk(t.left)
  • print t.key
  • inorderTreeWalk(t.right)
  • endif
  • end

17
Preorder Tree Traversal
  • Key of the root is printed before values in the
    left and right subtree
  • preorderTreeWalk(t)
  • if t is not Null then
  • print t.key
  • inorderTreeWalk(t.left)
  • inorderTreeWalk(t.right)
  • endif
  • end

18
Postorder Tree Traversal
  • Key of the root is printed after values in the
    left and right subtree
  • postorderTreeWalk(t)
  • if t is not Null then
  • inorderTreeWalk(t.left)
  • inorderTreeWalk(t.right)
  • print t.key
  • endif
  • end

19
Example
  • In-order?
  • Pre-order?
  • Post-order?

20
Binary Expression Tree
  • Use binary tree to indicate precedence
  • Root of subtrees are operators, leaves are
    operands
  • Example (12-3) (41)

21
Evaluation
  • Eval(t)
  • if t.key is an operand then
  • return t.key
  • else
  • case t.key is
  • return Eval(t.left) Eval(t.right)
  • - return Eval(t.left) - Eval(t.right)
  • return Eval(t.left) Eval(t.right)
  • / return Eval(t.left) / Eval(t.right)
  • end case
  • end if
  • end

22
Print
  • Inorder traversal
  • Preorder traversal
  • Postorder traversal

23
Summary
  • Priority queues only allow access to the item
    with the highest priority
  • Priority queue can be implemented as a heap in
    which the root has the element with the highest
    priority Deletion includes swapping root with
    the last leave and reheaping down Insertion is
    implemented by reheaping up
  • Reheaping up and down can be implemented
    recursively
  • Heap sort is an in-place O(nlogn) algorithm
  • Binary trees store small values in the left
    subtree, larger values in the right subtree
  • Binary search has a time complexity of O(logn)

24
Possible Quiz Questions
  • Whats a priority queue?
  • Demo heap sort. What is the running time of heap
    sort?
  • What is the running time of binary search?
  • Write a pseudocode for binary search tree
    operations.

25
Test Review
  • Asymptotic analysis
  • Recurrences
  • Stack
  • Queue
  • Priority Queue
  • Heaps
  • Binary Tree
Write a Comment
User Comments (0)
About PowerShow.com