1 / 151

Chapter 12

Chapter 12. Tree Maps and Tree Sets. Chap.12 Contents. 12.1 Red-Black Trees 12.1.1 The Height of a Red-Black Tree 12.1.2 Red-Black Tree Insertion 12.2 The Map Interface 12.3 The TreeMap Implementation of the SortedMap Interface

gusty
Download Presentation

Chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12 Tree Maps and Tree Sets

  2. Chap.12 Contents 12.1 Red-Black Trees 12.1.1 The Height of a Red-Black Tree 12.1.2 Red-Black Tree Insertion 12.2 The Map Interface 12.3 The TreeMap Implementation of the SortedMap Interface 12.3.1 The TreeMap Class’s Fields and Nested Entry Class 12.3.2 Method Definitions in the TreeMap Class

  3. Chap.12 Contents (cont.) 12.4 Application of the TreeMap Class: A Simple Thesaurus 12.4.1 Design and Implementation of the Thesaurus Class 12.6 The Tree Set Class 12.6.1 Implementation of the TreeSet Class 12.6.2 Application: A Simple Spell Checker

  4. 12.1 Red-Black Trees

  5. Red-black tree 特性 • 如果黑元素只有一個子元素, 則此子元素必為紅leaf • 如果紅元素有子元素, 則必定是兩個黑色子元素 Why? See next page.

  6. Answers • 有兩 paths 經此黑元素 1) 其一到此為止 2) 另一到此又到其子 其子須為紅元素 此兩 paths 才會有相同數目的黑元素 才符合 path rule • 經此紅元素 有兩 paths 到其二子 此二子須為黑元素 此兩 paths 才會有相同數目的黑元素 才符合 path rule 而且 若子為紅 則紅紅 不符 red rule

  7. 12.1.1 The Height of a Red-Black Tree

  8. 12.1.2 Red-Black Tree Insertion 我們要對左圖 insert 2 1)依 binary search tree   把 2 當 1 的 right child 2)依 red black tree 新加入者為red 故 1,2形成右圖 Double Red違反 Red Rule 3 3 1 4 4 1 2

  9. How to do 3 Cases of Double Red? Case 1: Red uncle new nodeRecurse upwards if Double Red grandparent 3 3 uncle uncle parent 1 4 1 4 node node can be left child too 2 2 • recolor parent and uncle to BLACK • recolor grandparent to RED • node 3 as new node • if Double Red again then recurse upwards

  10. Case 2: Black uncle, LR (3,1,2) 3 3 1. rotateLeft (1) 2. node 1as new node 3. goto Case 3 uncle uncle 4 2 4 1 node new node 2 1

  11. Case 3: Black uncle, LL (3,2,1) 1. rotateRight (3) 2. recolor 2 to BLACK 3. recolor 3 to RED 3 2 uncle 4 2 3 1 node uncle node 4 1

  12. 以上 3 cases 是 parent isleft child 另有左右對調的 3 cases 是 parent isright child 請自行畫圖 依照這些 design sketch 可推敲出 pseudo-code 如下

  13. Copy right: MIT Erik D. Demaine & Leiserson // x= node (Red Rule)

  14. RBT.EntryRBTinsert(aRoot, node) // red black tree (RBT) insert • // aRoot is the current RBT’s root before insertion. • // node is the node to be inserted. • 1. root = (RBT.Entry)BST.BSTinsert (aRoot, node)// binary search tree (BST) insert • 2. node <- RED • 3. while nodeis not root AND parent is RED • if parent is left child then • 1. uncle <- right child of grandparent • 2. if uncle is REDthen 〈Case 1〉 • else • 1. if node is right child then〈Case 2〉end if • 2.〈Case 3〉 • end if • else〈parent is right child 上面紅色部分中 ”left” 與 “right”對調〉 • end if • end while • 4. root <- BLACK • 5. return root • end RBTinsert

  15. 60 80 30 50 70 90 20 40 剛加的必為紅元素Case 1

  16. 12.2 The Map Interface

More Related