More Trees. Outline Tree B-Tree 2-3 Tree 2-3-4 Tree Red-Black Tree.

Slides:



Advertisements
Similar presentations
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Advertisements

COMP 451/651 Indexes Chapter 1.
A balanced life is a prefect life.
Chapter 15 B External Methods – B-Trees. © 2004 Pearson Addison-Wesley. All rights reserved 15 B-2 B-Trees To organize the index file as an external search.
1 B trees Nodes have more than 2 children Each internal node has between k and 2k children and between k-1 and 2k-1 keys A leaf has between k-1 and 2k-1.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
Red Black Trees Colored Nodes Definition Binary search tree.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
B + -Trees (Part 2) Lecture 21 COMP171 Fall 2006.
B-Trees Chapter 9. Limitations of binary search Though faster than sequential search, binary search still requires an unacceptable number of accesses.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Preliminaries Multiway trees have nodes with greater than two children. Multiway trees of order k have nodes with most k children Trees –For all.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
B-Trees (continued) Analysis of worst-case and average number of disk accesses for an insert. Delete and analysis. Structure for B-tree node.
Tirgul 6 B-Trees – Another kind of balanced trees.
AVL Trees / Slide 1 Deletion  To delete a key target, we find it at a leaf x, and remove it. * Two situations to worry about: (1) target is a key in some.
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
Introduction to Database Systems1 B+-Trees Storage Technology: Topic 5.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
 B+ Tree Definition  B+ Tree Properties  B+ Tree Searching  B+ Tree Insertion  B+ Tree Deletion.
Storage CMSC 461 Michael Wilson. Database storage  At some point, database information must be stored in some format  It’d be impossible to store hundreds.
B + TREE. INTRODUCTION A B+ tree is a balanced tree in which every path from the root of the tree to a leaf is of the same length, and each non leaf node.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
CSE AU B-Trees1 B-Trees CSE 373 Data Structures.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Red-Black Trees Acknowledgment Many thanks to “erm” from Purdue University for this very interesting way of presenting this course material. 1.
B + -Trees Same structure as B-trees. Dictionary pairs are in leaves only. Leaves form a doubly-linked list. Remaining nodes have following structure:
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
COSC 2007 Data Structures II Chapter 15 External Methods.
2-3 Trees, Trees Red-Black Trees
Adapted from Mike Franklin
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Chapter 12 B+ Trees CS 157B Spring 2003 By: Miriam Sy.
B-Tree – Delete Delete 3. Delete 8. Delete
 B-tree is a specialized multiway tree designed especially for use on disk  B-Tree consists of a root node, branch nodes and leaf nodes containing the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
B-Trees Katherine Gurdziel 252a-ba. Outline What are b-trees? How does the algorithm work? –Insertion –Deletion Complexity What are b-trees used for?
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
B+-Tree Deletion Underflow conditions B+ tree Deletion Algorithm
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: TEL 3049.
Lecture 23 Red Black Tree Chapter 10 of textbook
COMP261 Lecture 23 B Trees.
AA Trees.
G64ADS Advanced Data Structures
Binary search tree. Removing a node
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
Red Black Trees.
B-Trees.
2-3-4 Trees Red-Black Trees
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
Presentation transcript:

More Trees

Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree

Tree 定義 : 從 root 開始存取的一種 data structure. 每個 node 為 leaf 或 internal node. internal node 有一個或多個 children nodes, 他為 child node 的 parent 相同 internal node 的 children nodes 互稱 siblings.

Tree Node Root Node leaf Internal node Parent Child

B-Tree in memory of R. Bayer Degree 為 d 的 B tree 每個 node 包含至多 d 個 pointers 或 d-1 elements 每個 node 至少 1/2 滿 ( 即至少 [ (d-1)/2] elements)

B-Tree of Degree

B* Tree B-tree 的 node 至少 2/3 滿

B* Tree of Degree

B+ Tree 包含 index pages 和 data pages root node 和 internal nodes 為 index pages (keys only). leaf nodes 為 data pages ( 排序 data ) data 即 element ( 含 key) 每個 node 至少 1/2 full.

B+ Tree index page data page Number of Keys4 Number of Pointers5 Fill Factor50% Minimum Keys in each page2 This B+ tree:

B+-Tree insert element 時, 考慮下面 3 個 cases: Leaf Page FullIndex Page Full No YesNo Yes

B+-Tree Case 1: leaf 未滿 – 把 element 放到適當且 sorted 好的 leaf 位置

B+-Tree Case 2: leaf 滿, 但 index 沒滿 1. 把 leaf 分裂 (split) 2. 把 middle key 放在排序好的 index 3.left leaf 放比 middle key 小的 elements 4. right leaf 放比 middle key 大的 elements.

B+-Tree 插入 element 其 key 為 70 到 B+ tree. 此 element 將加到含有 50, 55, 60, and 65 的 leaf. 不幸地, 此 leaf 已滿. 這意味我們必須分裂 ( split ) leaf 如下 : Left Leaf PageRight Leaf Page

B+-Tree middle key 60 放在 50 和 75 之間的 index 下面表示加入 70 後的 B+ tree

B+-Tree Case 3: leaf 和 index 都滿了 1. 分裂 leaf. 2.< middle key 的 element 放 left leaf. 3.>= middle key 的 record 放 right leaf. 4. 分裂 index. 5.keys < middle key 放 left index. 6.keys > middle key 放 right index. 7.middle key 放上一層 (higher level) index. 如果上一層 index 也滿了, 持續分裂 ( split) index (steps 4 - 7).

B+ Tree 加入 element 其 key 為 95. 此 element 將加入含 75, 80, 85, and 90 的 leaf. 此 leaf 已滿, 所以分裂 leaf: middle key 85, 提升到 index. 不幸地, index 也滿, 所以 分裂 index: Left Leaf PageRight Leaf Page Left Index PageRight Index PageHigher level Index

B+ Tree 下圖表示加入 key 為 95 的 element 的結果

B+ Tree Deleting element 考慮 3 個 cases: Leaf Page Below Fill Factor Index Page Below Fill Factor No YesNo Yes Case 1 Case 2 Case 3

B+ Tree Case 1: delete 後, leaf 和 index 符合 B+tree – 從 leaf delete 此 element – 把 keys 排成升冪排序. – 如被刪除的 key 在 index, 用下個 key 取代它

B+ Tree Delete element with key 70 –This element is in a leaf containing 60, 65 and 70. This leaf will contain 2 elements after the deletion. Since our fill factor is 50% or (2 elements), we simply delete 70 from the leaf.

B+ Tree Case 2: delete 後, leaf 不夠滿 – 結合 leaf 和它的 sibling leaf. – 改 index 來反應改變

B+ Tree Delete the element with key 25 –This element is found in the leaf containing 25, 28, and 30. The fill factor will be 50% after the deletion; however, 25 appears in the index. Thus, when we delete 25, we must replace it with 28 in the index.

B+ Tree Case 3: delete 後, index 和 leaf 都不夠滿 1. 合併 (merge) leaf 和它的 sibling leaf. 2. 調整 index page 來反應其改變 3. 合併 index page 和它的 sibling index. 4. 持續合併 index 直到符合 B+ tree, 或到達 root.

B+ Tree Delete the element with key 60 1.The leaf containing 60 (60 65) will be below the fill factor after the deletion. Thus, we must merge leaves. 2.With merged leaves, the index will be reduced by one key. Hence, it will fall below the fill factor. Thus, we must merge indexes is now the only key in root index page. Obviously, it will be removed.

2-3 Tree

為 search tree 可為空或符合 : – 每個 internal node 為 2-node 或 3-node 2-node 有一 element 而 3-node 有二 elements – 所有 external nodes 都在相同 level external nodes internal node 2-node B 3-node C A

2-3 Tree insert Case 1: 插入 70 先尋找 70. 發現不在其中. 須知尋找 70 時 遇到哪 node. 是 含 80 的 node C node C 只有一個 element, 所以 70 可放 C C A B

2-3 Tree insert Case 2: 插入 30 會遇到 30 的是 node B B 為 3-node, 須產生新 node D. B 含 elements 10, 20, 30 B 中最大 element 30 放 D 最小 element 10 放 B. 中間 element 20 放 B 的 parent A 80 C A B Figure 3 30 D

2-3 Tree Insert case 3: 插入 60 尋找 60 會遇 node C C 為 3-node, 需產生新 node E C 含 elements 60,70,80 中間值 70 放在 C 的 parent A 最小值 60 放 C 最大值 80 放 E A 為 3-node, 產生新 node F A 含 elements 20, 40, 70 中間值 40 放在 A 的 parent G ( 需產生 G) 最小值 20 放 A 最大值 70 放 F

2-3 Tree 40 G 70 F 80 E 60 C 20 A 10 B 30 D Figure4 Insertion of 60 into the 2-3 tree of Figure 3

2-3 Tree 5080 A 1020 B 6070 C 9095 D 5080 A 1020 B 60 C 9095 D (a) Initial 2-3 tree (b) 70 deleted

2-3 Tree 5080 A 1020 B 60 C 95 D (c) 90 deleted 2080 A 10 B 50 C 95 D (d) 60 deleted

2-3 Tree 20 A 10 B 5080 C (e) 95 deleted B 80 C (f) 50 deleted A 2080 (g) 10 deleted A

2-3-4 Tree

為 search tree 為空或是滿足 : – 每個 internal node 為 2, 3, 或 4 node. 2-node 有一 element, 3-node 有二 elements, 4-node 有三 elements – 所有 external nodes 都在相同 level tree 類似 2-3 tree, 但它有 4-nodes ( 即 四個 pointers 和三個 elements)

2-3-4 Tree Insertion There are 3 cases for a 4-node: Case 1: It is the root Case 2: Its parent is a 2-node Case 3: Its parent is a 3-node

2-3-4 Tree Case 1: It is the root. xyz abcd t (root) y xz ab cd t Figure1 when the root is a 4-node

2-3-4 Tree Case 2: Its parent is a 2-node wxy abcd z e w abcd xz e y Figure 2 when the child of a 2-node is a 4-node

2-3-4 Tree Deletion Deleting p –The following cases are to be considered: p is a leaf. q is not a 2-node. (q is a child of p) q is a 2-node and its nearest sibling r is also a 2- node. q is a 2-node and its nearest sibling r is a 3-node. q is a 2-node and its nearest sibling r is a 4-node.

2-3-4 Tree vw b de xz f y v b c d wz e xy p q a f r Figure1 when the nearest sibling is 3-node q p r a q is the left child of a 3-node p c

2-3-4 Tree u b d vyz e wx p q a f r Figure1 when the nearest sibling is 3-node q is the left child of a 4-node p c g uv b e wyz x q f r d g a c

2-3-4 Tree tree 可轉成 binary search tree 稱為 red-black tree 可節省儲存空間

Red-Black Tree

red-black tree 為 binary search tree: 每個 node 不是 red 就是 black 每個 leaf (NULL) 都為 black red node 的兩個 children 都為 black. 每個 path 含相同數目的 black nodes. red node 不可接著 red node A basic red-black tree

Red-Black Tree A red-black tree with n internal nodes has height at most 2 log(n+1). Red-Black tree can always be searched in O (log n) time.

Red-Black Tree c S L  L S S L ab c a b OR a b c Figure 1 Transforming a 3-node into two red_black nodes S for Small; L for Large.

Red-Black Tree Figure 2 Transforming a 4-node into two red_black nodes S M L M S L a b c d bc d  S for Small;M, Middle; L, Large. a

Red-black Tree 將下圖的 Red-Black Tree 轉成 Tree 依序 (1) 刪除 60 (2) 加入 8 再轉回 Red-Black Tree

2-3-4 Tree tree

刪除 60 70

加入 8 8 7

轉回 Red-Black Tree