Binary Search tree, redblack tree, and AVL tree - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Binary Search tree, redblack tree, and AVL tree

Description:

Binary Search tree, red-black tree, and AVL tree. Binary Search Trees. 2-3-4 Tree ... Converting a 2-3-4 Tree to Red-Black Tree (top-down) Main Index. Contents ... – PowerPoint PPT presentation

Number of Views:439
Avg rating:3.0/5.0
Slides: 29
Provided by: Stew92
Category:

less

Transcript and Presenter's Notes

Title: Binary Search tree, redblack tree, and AVL tree


1
CSE331 Lecture 20 Advanced Associative
Structures
Chapter 11 Binary Search tree, red-black tree,
and AVL tree Binary Search Trees 2-3-4
Tree Insertion of 2-3-4 tree Red-Black
Trees Converting 2-3-4 tree to Red-Black tree
Four Situations in the Splitting of a
4-Node left child of a BLACK parent P prior
to inserting node oriented left-left from G
using a single right rotation oriented
left-right from G after the color flip Building
a Red-Black Tree Red-Black Tree
Representation Summary Slide
2
Binary Search Tree, Red-Black Tree and AVL Tree
Example
3
Two Binary Search Tree Example
  • Insertion sequence 5, 15, 20, 3, 9, 7, 12, 17,
    6, 75, 100, 18, 25, 35, 40

4
2-3-4 Tree (node types)
5
2-3-4 Tree Example
6
2-3-4 Tree InsertionSplits 4-nodes top-down
After split, 7 is added to leaf following normal
search tree order
Inserting 7 into tree requires split of 4-node
before adding 7 as child (leaf or part of
existing leaf)
6
3 6 9
9
3
2
10
4
8
2
10
7 8
4
7
2-3-4 Tree Insertion Details
  • 4-nodes must be split on the way down
  • Split involves promotion of middle value into
    parent node
  • New value is always added to existing 2-node or
    3-node
  • 2-3-4 tree is always perfectly balanced
  • Implementation is wasteful of space
  • Each node must have space for 3 values, 1 parent
    pointer and 4 child pointers, even if not always
    used
  • There is an equivalent red-black binary tree

8
Building 2-3-4 Tree by Series of Insertions
  • Insertion Sequence 2, 15, 12, 4, 8, 10, 25, 35,
    55, 11, 9, 5, 7

9
Insertion Sequence 2, 15, 12, 4, 8, 10, 25, 35,
55, 11, 9, 5, 7
10
Insertion Sequence 2, 15, 12, 4, 8, 10, 25, 35,
55, 11, 9, 5, 7
11
Red Black Tree Properties
  • The root is always BLACK
  • A RED parent never has a RED child
  • Every path from ROOT to an EMPTY subtree has the
    same black height ( of BLACK nodes)
  • Black Height of tree is O(log2n)

12
Red Black Tree Node Types
  • BLACK parent with two RED children is equivalent
    of a 4-node
  • A 4-node split is accomplished by flipping the
    colors to RED parent two BLACK children, with
    possible rotation if colors conflict
  • BLACK parent with single RED child is equivalent
    of a 3-node
  • New nodes are inserted a RED leaves

13
Red-Black Trees binary equivalents of 2-3-4
trees
14
Converting a 2-3-4 Tree to Red-Black Tree
(top-down)
15
Building red black tree by repeated insertions
  • Insert root
  • Each successive insertion point is found in
    normal search tree order
  • Any 4-nodes (black parent with two red children)
    is split going down into tree
  • Requires color changes and possible rotations of
    subtrees
  • New value is added as RED leaf
  • If result is RED child of RED parent, then
    rotations are required to correct

16
Situations in the Splitting of a 4-Node (A X B)
17
Required actions to split 4-node (A X B) with
parent P grandparent G
  • 4-node is either child of black parent
  • Flip subtree colors ONLY (X red, A B black)
  • 4-node is child of red parent (left-left)
  • Flip subtree colors single right rotation about
    root
  • 4-node is child of red parent (right-right)
  • Flip subtree colors single left rotation about
    root
  • 4-node is child of red parent (left-right)
  • Double rotation (first left, then right) about X
    then color X black, G P red
  • 4-node is child of red parent (right-left)
  • Double rotation (first right, then left) about X
    then color X black, G P red

18
Left child of a Black parent PSplit requires
only a Color Flip
19
Example split prior to inserting 55 flip colors
(40,50,60) insert leaf
20
RED parent, 4-node oriented left-left from G
(grandparent)
21
Red Parent Oriented Left-Right From G
22
Red Parent Oriented Left-Right From G
23
Red Parent Oriented Left-Right From G
After second (right) rotation about X and
coloring X black, G P red
24
Building A Red-Black Tree Insertions 2, 15,
12, 4, 8, 10, 25, 35
25
Building A Red-Black Tree Insertions 2, 15, 12,
4, 8, 10, 25, 35
26
rbnode Representation of Red-Black Tree
27
Summary Slide 1
- 2-3-4 tree - a node has either 1 value
and 2 children, 2 values and 3 children, or 3
values and 4 children - construction of
2-3-4 trees is complex, so we build an
equivalent binary tree known as a red-black tree 
28
Summary Slide 2
- red-black trees - Deleting a node from a
red-black tree is rather difficult.   -
the class rbtree, builds a red-black tree
Write a Comment
User Comments (0)
About PowerShow.com