Even MORE Balanced Trees Computing 2 COMP1927 15s1.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
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.
Balanced Binary Search Trees
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
A balanced life is a prefect life.
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.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
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.
Balanced Search Trees (Ch. 13) To implement a symbol table, Binary Search Trees work pretty well, except… The worst case is O(n) and it is embarassingly.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
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.
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.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Ch. 13: Balanced Search Trees Symbol table: insert, delete, find, pred, succ, sort,… Binary Search Tree review: What is a BST? binary tree with a key at.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Splay Trees and B-Trees
Binary Search Trees CSE 331 Section 2 James Daly.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Balanced Search Trees 3.4 – 3.7 Team names Majed Suhaim Ahmed Sulaiman M Alharbi.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
2-3 Trees, Trees Red-Black Trees
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
File Organization and Processing Week Tree Tree.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
1 More Trees Trees, Red-Black Trees, B Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
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.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
COMP261 Lecture 23 B Trees.
AA Trees.
Multiway Search Trees Data may not fit into main memory
CSCI Trees and Red/Black Trees
Red Black Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
Data Structures Balanced Trees CSCI
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
B- Trees D. Frey with apologies to Tom Anastasio
Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
2-3-4 Trees Red-Black Trees
Presentation transcript:

Even MORE Balanced Trees Computing 2 COMP s1

O PTIMISATION B ASED B ALANCED T REES So far we have seen local approaches Randomised trees…make poor performance unlikely Splay trees…reasonable amortized performance BUT both still have O(n) worst case. Ideally we want worst case to be O(log n) trees…use varying-sized nodes to assist balance red-black trees…isomorphic to 2-3-4, but with binary nodes

2-3-4 T REES trees have three kinds of nodes 2-nodes, with two children (same as normal BSTs) 3-nodes, two values and three children 4-nodes, three values and four children

2-3-4 T REES trees are ordered similarly to BSTs In a balanced tree: all leaves are at same distance from the root trees grow "upwards" from the leaves.

2-3-4 TREE IMPLEMENTATION : typedef struct node *TreeLink; struct node { int order; // 2, 3 or 4 Item data[3]; // items in node TreeLink child[4]; //links to subtrees }; //New nodes are always order 2 TreeLink newNode(Item item){ TreeLink n = malloc(sizeof(struct node)); n->order = 2; n->data[0] = it; n->child[0] = n->child[1] = NULL; return n; }

2-3-4 S EARCHING : Item search(Tree t, Key k){ if(t == NULL) return NULL; int i, int diff; int nItems = t->order-1; for(i = 0; i < nItems; i++){ diff = compare(k,keyOf(t->data[i]); if(diff <=0) break; } if(diff == 0) return t->data[i]; else return search(t->child[i],k); }

2-3-4 I NSERTION Insertion into a 2-node or 3-node:

S PLITTING A NODE IN A TREE : Insertion into a 4-node (requires a split):

S PLITTING THE ROOT :

B UILDING A TREE... 7 INSERTIONS :

E XERCISE : T REE INSERTION Insert S then U into this tree.

2-3-4 I NSERTION A LGORITHM Find leaf node where Item belongs (via search) if not full (i.e. order < 4) insert Item in this node, order++ if node is full (i.e. contains 3 Items) split into two 2-nodes as leaves promote middle element to parent insert item into appropriate leaf 2-node if parent is a 4-node continue split/promote upwards if promote to root, and root is a 4-node split root node and add new root

T OP D OWN I NSERTION To avoid propagation of splitting 4-nodes all the way up the tree we can use a top-down approach. Every time we encounter a 4-node on our way down the search path for insertion, we split it.

E XERCISE : T REE INSERTION Insert S then U into this tree using a top down approach

2-3-4 TREE SEARCHING COST ANALYSIS : Worst case determined by height h trees are always balanced ⇒ height is O(logN) worst case for height: all nodes are 2-nodes same case as for balanced BSTs, i.e. h ≅ log 2 N best case for depth: all nodes are 4-nodes balanced tree with branching factor 4, i.e. h ≅ log 4 N

S OME N OTES ON T REES Splitting the root is the only way depth increases. One variation: why stop at 4? Allow nodes to hold up between M/2 and M items. If each node is a disk-page, then we have a B-tree (databases). Implement similar ideas using just BST nodes → red-black trees.

R ED -B LACK T REES Red-black trees are a representation of trees using BST nodes. each node needs one extra value to encode link type but we no longer have to deal with different kinds of nodes Link types: red links... combine nodes to represent 3- and 4- nodes black links... analogous to "ordinary" BST links (child links) Advantages: standard BST search procedure works unmodified get benefits of tree self-balancing (although deeper)

R ED B LACK T REES Definition of a red-black tree: a BST in which each node is marked red or black no two red nodes appear consecutively on any path Balanced red-black tree: all paths from root to leaf have same number of black nodes Insertion algorithm ensures that balanced trees remain balanced Prevents O(n) behaviour

R EPRESENTING 3- AND 4- NODES IN RED - BLACK TREES :

R ED B LACK L INKS Colour allows us to distinguish links black = parent node is a "real"parent red = parent node is a neighbour

E QUIVALENT TREES ( ONE 2-3-4, ONE RED - BLACK ):

R ED - BLACK TREE IMPLEMENTATION : typedef enum {RED,BLACK} Colour; typedef struct Node *TreeLink; struct Node { Item data; // actual data Colour colour; // colour of link to // parent TreeLink left; // left subtree TreeLink right; // right subtree } ;

E XERCISE Draw the following tree as an equivalent red-black tree

S OLUTION

R ED B LACK T REE O PERATIONS Search is same as standard BST Insertion is more complex than for standard BSTs Need to balance only when we see 4-nodes If a node has 2 red children it is part of a 4-node. Low overhead as there are not usually many 4-nodes in a tree We can split 4-nodes in a top down manner, splitting any 4 nodes on the search path down the tree during insertion

S PLITTING A 4- NODE //Done while descending the tree if(isRed(currTree->L) && isRed(currTree->R)){ currTree->colour = RED; currTree->L-Colour = BLACK; currTree->R->Colour = BLACK; }

C HECKING SUBTREE AFTER I NSERTION Stage 1: in a red right subtree, and a successive leftward red link if(isRightSubtree &&isRed(currTree) && isRed(currTree->L)){ currTree = rotateR(currTree); }

C HECKING SUBTREE AFTER I NSERTION Stage 2: two successive red links in same direction = newly- created 4-node if(isRed(currTree->L) && isRed(currTree->L->L)){ currTree = rotateR(currTree); currTree->color = BLACK; currTree->color = RED; }

I NSERTION void Stinsert(Item item, ST st){ st->root = RBInsert(st->root,item,0); st->root->colour = BLACK; } TreeLink RBInsert(TreeLink t, Item item, int inRightSubtree){ Key currKey = Key(item); if(t == emptyTree){ //always part of an existing node return new(item, emptyTree,emtyTree,RED); } if(isRed(currTree->left) && isRed(currTree->right)){ currTree->colour = RED; currTree->left->Colour = BLACK; currTree->right->Colour = BLACK; }

I NSERTION if(less(currKey,Key(t->item))){ //insert in left currTree->left = RBinsert(currTree->left, item, 0); if(isRed(currTree && isRed(currTree->left) && inRightSubtree) currTree = rotateR(currTree); //Can’t be true if last if statement was true if(isRed(currTree->left) && isRed(currTree->left_left)){ currTree = rotateR(currTree); currTree->colour = BLACK; currTree->right->colour = RED; } } else { //insert into right

S IMPLE E XAMPLE Insert keys 1,3,2 1 becomes the root 3 becomes the right child of 1 and has a red link 1 \ 3 2 becomes the left child of 3 and has a red link 1 \ 3 (3 is red and a right child and has a red left child ) / so we rotate right at 3 2

S IMPLE EXAMPLE CONTINUED After rotating at 3 we get this 1 (Now 1 has a red right and a red right right link) \ so we rotate and change colours 2 1 now has a red parent but 2 does not \ 3 2 / \ 1 3

E XAMPLE OF S PLITTING Suppose we wish to insert 4 into 2 / \ is on the search path to insert 4 and has left and right red links so we split then keep inserting 2 / \ 1 3 \ 4

E XERCISE : R ED /B LACK T REE I NSERTION Show the result of inserting the following values 8,4,6,2,3

R ED -B LACK T REE C OST A NALYSIS Guaranteed O(log n) insertion, search and deletion Maximal height of a red-black tree 2 * log n Yay!