expression tree

Infix print

Lesson 9 Heap - 图1

  • print infix expression that coresponds to Tree
  1. infixPrint(T){
  2. // stopping
  3. if (isLeaf(T))
  4. print(T.data) //operand
  5. else{
  6. // recursion
  7. print("(")
  8. InfixPrint(T.left)
  9. print(")")
  10. print(T.data)//operator
  11. print("(")
  12. InfisPrint(T.right)
  13. print(")")
  14. }
  15. }

Lesson 9 Heap - 图2 (3)((5)+(7))
Infix print: Left subtree Lesson 9 Heap - 图3 root Lesson 9 Heap - 图4 Right subtree
*Inorder traversal

Postfix Print

PostfixPrint(T){
    //stopping
    if(isLeaf(T))
        print(T.data)  //operand
    else{
        //recursion
        PostfixPrint(T.left)
        PostfixPrint(T.right)
        print(T.data) //operands
    }
}

Postfix print: Left subtree Lesson 9 Heap - 图5 Right subtree Lesson 9 Heap - 图6 root

Post order traversal

Heap: priority queue

Lesson 9 Heap - 图7
node:
data Lesson 9 Heap - 图8 key + data
key mimic: ID of book

  • Task: find the largest priority node
    key to mean priority
    data to mean todo

While taking node, often take the largest priory one

  • How to realize this function fastly

Binary max-tree

  • root key Lesson 9 Heap - 图9 other node’s key
  • every sub-tree is bin-maxtree
    Lesson 9 Heap - 图10
    GetLargest Lesson 9 Heap - 图11 root
    Remove Largest ? Lesson 9 Heap - 图12 replace with larger of sub-root recursively

Complexity analysis

Removal

  • least time complexity: Lesson 9 Heap - 图13#card=math&code=%5Cmathcal%7BO%7D%28h%29)
    Lesson 9 Heap - 图14
  • worst time complexity: Lesson 9 Heap - 图15#card=math&code=%5Cmathcal%7BO%7D%28n%29)
    Lesson 9 Heap - 图16
    To avoid larger complexity, we need to optimize the tree’s structure;
    Binary tree with complexity Lesson 9 Heap - 图17#card=math&code=h%20%3D%20%5Cmathcal%7BO%7D%20%28log%20n%29) has two similar structure:
    Full binary tree and Complete binary tree
    Lesson 9 Heap - 图18
  • Complete binary max-tree

While removing, the property of complete/ max may be lost
Lesson 9 Heap - 图19 Remove Largest:

    1. last -> root (maintain complete)
    1. sink down (maintain max)
  • complexity:Lesson 9 Heap - 图20%20to%20%5Cmathcal%7BO%7D(logn)#card=math&code=%5Cmathcal%7BO%7D%28h%29%20to%20%5Cmathcal%7BO%7D%28logn%29)

Insertion

Insertion:

    1. insert at last
    1. flat up
  • complexity:(worst: Lesson 9 Heap - 图21#card=math&code=%5Cmathcal%7BO%7D%28h%29))
    Lesson 9 Heap - 图22

complete binary tree and array

complete binary tree Lesson 9 Heap - 图23 array
heap Lesson 9 Heap - 图24 partially ordered array

selsection sort on (unordered array): Lesson 9 Heap - 图25)%20%3D%20%5Cmathcal%7BO%7D%20(n%5E2)#card=math&code=%5Cmathcal%7BO%7D%28n%20%5Ccdot%20%5Cmathcal%7BO%7D%28n%29%29%20%3D%20%5Cmathcal%7BO%7D%20%28n%5E2%29)

selection sort on heap: Lesson 9 Heap - 图26%20%5Ccdot%20%5Cmathcal%7BO%7D(log%20%20n))%20%3D%20%5Cmathcal%7BO%7D(nlogn)#card=math&code=%5Cmathcal%7BO%7D%28n%20%5Cmathcal%7BO%7D%281%29%20%5Ccdot%20%5Cmathcal%7BO%7D%28log%20%20n%29%29%20%3D%20%5Cmathcal%7BO%7D%28nlogn%29)

unordered array to a heap: n times insertion: Lesson 9 Heap - 图27)%20%3D%20%5Cmathcal%7BO%7D(logn)#card=math&code=%5Cmathcal%7BO%7D%28n%20%5Ccdot%20%5Cmathcal%7BO%7D%28h%3Dlogn%29%29%20%3D%20%5Cmathcal%7BO%7D%28logn%29)
Lesson 9 Heap - 图28

summarize

structure insert remove largest
heap Lesson 9 Heap - 图29#card=math&code=O%28logn%29) Lesson 9 Heap - 图30#card=math&code=O%28logn%29)
max-tree Lesson 9 Heap - 图31#card=math&code=O%28h%29) Lesson 9 Heap - 图32#card=math&code=O%28h%29)
unordered linked-list Lesson 9 Heap - 图33#card=math&code=O%281%29) Lesson 9 Heap - 图34#card=math&code=O%28n%29)
ordered linked-list Lesson 9 Heap - 图35#card=math&code=O%28n%29) Lesson 9 Heap - 图36#card=math&code=O%281%29)