even more balanced trees computing 2 comp1927 15s1

35
Even MORE Balanced Trees Computing 2 COMP1927 15s1

Upload: jonathan-perkins

Post on 04-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Even MORE Balanced Trees Computing 2 COMP1927 15s1

Even MORE Balanced Trees Computing 2 COMP1927 15s1

Page 2: Even MORE Balanced Trees Computing 2 COMP1927 15s1

OPTIMISATION BASED BALANCED TREES

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) 2-3-4 trees…use varying-sized nodes to assist

balance red-black trees…isomorphic to 2-3-4, but with

binary nodes

Page 3: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 TREES 2-3-4 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

Page 4: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 TREES 2-3-4 trees are ordered similarly to BSTs

In a balanced 2-3-4 tree: all leaves are at same distance from the root 2-3-4 trees grow "upwards" from the leaves.

Page 5: Even MORE Balanced Trees Computing 2 COMP1927 15s1

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 2TreeLink 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;}

Page 6: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 SEARCHING:

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);

}

Page 7: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 INSERTION

Insertion into a 2-node or 3-node:

Page 8: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SPLITTING A NODE IN A 2-3-4 TREE:

Insertion into a 4-node (requires a split):

Page 9: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SPLITTING THE ROOT:

Page 10: Even MORE Balanced Trees Computing 2 COMP1927 15s1

BUILDING A 2-3-4 TREE ... 7 INSERTIONS:

Page 11: Even MORE Balanced Trees Computing 2 COMP1927 15s1

EXERCISE: 2-3-4 TREE INSERTION

Insert S then U into this 2-3-4 tree.

Page 12: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 INSERTION ALGORITHM

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

Page 13: Even MORE Balanced Trees Computing 2 COMP1927 15s1

TOP DOWN INSERTION

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.

Page 14: Even MORE Balanced Trees Computing 2 COMP1927 15s1

EXERCISE: 2-3-4 TREE INSERTION

Insert S then U into this 2-3-4 tree using a top down approach

Page 15: Even MORE Balanced Trees Computing 2 COMP1927 15s1

2-3-4 TREE SEARCHING COST ANALYSIS:

Worst case determined by height h2-3-4 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 ≅ log2N

best case for depth: all nodes are 4-nodes balanced tree with branching factor 4, i.e. h ≅ log4N

Page 16: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SOME NOTES ON 2-3-4 TREES

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.

Page 17: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED-BLACK TREES

Red-black trees are a representation of 2-3-4 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 2-3-4 tree self-balancing (although

deeper)

Page 18: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED BLACK TREES

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

Page 19: Even MORE Balanced Trees Computing 2 COMP1927 15s1

REPRESENTING 3- AND 4-NODES IN RED-BLACK TREES:

Page 20: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED BLACK LINKS

Colour allows us to distinguish links black = parent node is a "real"parent red   = parent node is a 2-3-4 neighbour

Page 21: Even MORE Balanced Trees Computing 2 COMP1927 15s1

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

Page 22: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED-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

} ;

Page 23: Even MORE Balanced Trees Computing 2 COMP1927 15s1

EXERCISE

Draw the following 2-3-4 tree as an equivalent red-black tree

Page 24: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SOLUTION

Page 25: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED BLACK TREE OPERATIONS

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

Page 26: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SPLITTING 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;

}

Page 27: Even MORE Balanced Trees Computing 2 COMP1927 15s1

CHECKING SUBTREE AFTER INSERTION

Stage 1: in a red right subtree, and a successive leftward

red link

if(isRightSubtree &&isRed(currTree) &&

isRed(currTree->L)){

currTree = rotateR(currTree);

}

Page 28: Even MORE Balanced Trees Computing 2 COMP1927 15s1

CHECKING SUBTREE AFTER INSERTION

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;

}

Page 29: Even MORE Balanced Trees Computing 2 COMP1927 15s1

INSERTIONvoid 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;

}

Page 30: Even MORE Balanced Trees Computing 2 COMP1927 15s1

INSERTIONif(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

Page 31: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SIMPLE EXAMPLE 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

Page 32: Even MORE Balanced Trees Computing 2 COMP1927 15s1

SIMPLE 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

Page 33: Even MORE Balanced Trees Computing 2 COMP1927 15s1

EXAMPLE OF SPLITTING

Suppose we wish to insert 4 into

2 / \ 1 3

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

Page 34: Even MORE Balanced Trees Computing 2 COMP1927 15s1

EXERCISE: RED/BLACK TREE INSERTION

Show the result of inserting the following values 8,4,6,2,3

Page 35: Even MORE Balanced Trees Computing 2 COMP1927 15s1

RED-BLACK TREE COST ANALYSIS

Guaranteed O(log n) insertion, search and deletion Maximal height of a red-black tree

2 * log n

Yay!