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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容

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