Professional Documents
Culture Documents
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To
know what a red-black tree is (47.1). To convert a red-black tree to a 2-4 tree and vice versa (47.2). To design the RBTree class that extends the BinaryTree class (47.3). To insert an element in a red-black tree and resolve the double red problem if necessary (47.4). To insert an element from a red-black tree and resolve the double black problem if necessary (47.5). To implement and test the RBTree class (47.6-47.7). To compare the performance of AVL trees, 2-4 trees, and RBTree (47.8).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15 20 3
34 25 27
15
34
16
50
16
25
50
27
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Properties
A red-black tree has the following properties:
The root is black. Two adjacent nodes cannot be both red. All external nodes have the same black depth.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
e0 c1
e1 c2 c0
e0 or
e1 e1 e0
c2 c1
c1
c2
c0
e0 c1
e1 e2 c2 c3 c0 e0
e1
e2
c1
c2
c3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15 3 25 27
34
3
15 25
34
3
15 27
34
16
50
16
50
16
50
27
25
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
BianryTree<E>
m 0
RBTreeNode<E>
-red: boolean +RBTreeNode() +RBTreeNode(e: E) +isRed(): boolean +isBlack(): boolean +setRed(): void +setBlack(): void
1 Link
RBTree<E>
+RBTree() +RBTree(objects: E[]) #createNewNode(): RBTreeNode<E> +insert(o: E): boolean +delete(o: E): boolean Creates a default red-black tree. Creates an RBTree from an array of objects. Override this method to create an RBTreeNode. Returns true if the element is added successfully. Returns true if the element is removed from the tree successfully.