Lecture11

Binary Search Tree

Full vs. Complete Binary Trees

  • Full binary tree
    • a tree in which every node other than the leaves has two children
image-20210409124544762.png
  • Complete binary tree:
    • a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible
image-20210409125055015.png

Complete vs. Incomplete Tree

  • Complete Tree:
    • All levels are populated left to right
    • Last level IS populated left to right(even thought it is not fully populated)
image-20210409125438993.png
  • Incomplete Tree
    • All levels are populated, BUT
    • Last tree is not populated left to right
image-20210409125638196.png

Terminology: Tree Height

  • Tree height: maximum node depth in the tree OR height of the root
  • Here: tree height H=2
image-20210409125919886.png

Tree Size: Full Tree

  • Tree size: number of all nodes in a tree
  • Here: N = 20+ 21+ 22= 2(2+1)-1=7(but this is a very "neat" FULL tree)
  • N=20+ 21+ 22+...+2H=2(H+1)-1
image-20210409130539079.png

Tree Height = f(Tree size)

  • N- Number of Tree Elements | H-Full Tree Height
  • N=7--> H=log2(N+1)-1 = log2(7+1)-1=3-1=2
image-20210409130759184.png

BST Operations: Best / Worst Case

image-20210409185533622.png

Binary Trees: Underlying Structure

  • Trees can be stored in
    • arrays
    • linked structures
  • Trees based on arrays:
    • fast access
    • fixed size
    • deletion
    • possible waste of space("null" nodes) if not full
  • Trees based on linked structures
    • dynamic size
    • overhead

Tree as Arrays: Structure

image-20210409190040254.png

image-20210409190151498.png

Tree as Arrays: Indexing

image-20210409190241066.png

Abstract Data Type: Heap

  • Heap ADT
    • add (element)
    • top ( )
    • peek ( )
    • size ( )
    • isEmpty ( )

Heap

  • A heap is a binary tree with the following characteristics:
    • It is complete: each tree level is completely filled from left to right, with possible exception of the last level (which is still filled from left to right)
    • All nodes/keys satisfy the heap property:
      • in heaps for every node its key is greater [MaxHeap] / less than [MinHeap] (or equal to ) the keys of its children nodes.

Binary Search Tree vs. Heap

  • Binary Search Tree Properties:
    • The left subtree of a given node contains only nodes with keys lesser than the node's key.
    • The right subtree of a give node contains only nodes with keys greater than the node's key.
image-20210409221006261.png
  • Heap Properties:
    • For every node its key is greater than(or equal to)the keys of its children nodes.

MaxHeap vs. MinHeap

  • MaxHeap Properties:
    • For every node its key is greater than (or equal to) the keys of its children ndoes.
image-20210409221414323.png
  • MinHeap Properties:
    • For every node its key is less than(or equal to) the keys of its children nodes.
image-20210409221754560.png

Invalid Heaps

  • This tree is not complete. It is not a heap.
image-20210409222239012.png
  • MinHeap property is violated: child node key is less than parent ndoe key(5>2)
image-20210409222530554.png

Heap: Subtrees are Heaps

Heap property: both left and right subtrees must also be a heap.

image-20210409223318753.png

Heap: Single-node Trees are Heaps

Heap property: single-node trees are valid heaps.

image-20210409223508844.png

Heap: add(Element/Key)

Step1: A valid MaxHeap prior to adding a new node. New node location.

image-20210409231211399.png

Step2: Heap property temporarily violated!

Step3: Swap Parent and Child elements to restore MaxHeap property

Step4: Swap Parent and Child elements AGAIN to restore MaxHeap property

Step5: Now MaxHeap property is violated one level up

Step6: MaxHeap property is restored. Heap is valid

image-20210409231304026.png

Heap: top()

Step1: top(max in this case) element(10) is to be removed

Step2: top is removed, there will be a gap. Let's "fill/swap it" with the "last element"

image-20210409232256244.png

Step3: Now MaxHeap property is violated

Step4: Parent and LARGEST KEY(9) child elements to restore MaxHeap property

image-20210409232413275.png

Step5: MaxHeap property is restored. Heap is valid

Heap: peek()

Step 1: retrieve the top element without removing it.

image-20210410102901149.png

Application: Heap Sort

  • Heap Sort Pseudocode
heapSort(Collection c){
    if(c !=null){
    
        Heap h = new Heap();
        List out = new List();
        
        while(!h.isEmpty()){
            out.add(h.top());
        }
    }
    return out;
}
image-20210410103602136.png

Application: TOP K Elements

  • TOP K Elements Pseudocode:
topK(Collection c, int k){
    if(c != null & k>0){
        int count = 0;
        Heap h = new Heap();
        List out = new List();
        
        while( !h.isEmpty()){
            out.add(h.top());
            count++;
            if(count > k) break;
        }
    }
    return out;
}
image-20210410104205194.png

Abstract Data Type: Priority Queue

  • Priority Queue ADT:

    • enqueue (element)
    • dequeue ( )
    • peek ( )
    • size ( )
    • isEmpty ( )
  • Comments:

    • Underlying Structure is "invisible" to the user:
      • different approaches can be used
      • consider performance measures for the problem at hand
    • Needs to accept various elements
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 13,586评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 9,757评论 1 3
  • 没事就多看看书,因为腹有诗书气自华,读书万卷始通神。没事就多出去旅游,别因为没钱而找借口,因为只要你省吃俭用,来...
    向阳之心阅读 10,245评论 3 11
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,984评论 2 7