(2015S) Practical Software Technology

326.041 (2015S) – Practical Software Technology (Praktische Softwaretechnologie) Binary Search Trees, Red-Black Trees Alexander Baumgartner Alexander...
Author: Aubrey Morgan
3 downloads 0 Views 2MB Size
326.041 (2015S) – Practical Software Technology (Praktische Softwaretechnologie) Binary Search Trees, Red-Black Trees

Alexander Baumgartner [email protected] Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree: Nodes and Edges

Binary Search Trees, Red-Black Trees – Practical Software Technology

Trees

[email protected]

Definition

Trees

A tree T is a set of nodes storing elements such that the nodes have a parent-child relationship that satisfies the following properties: If T is nonempty, it has a special node, called the root of T , that has no parent. Each node v of T different from the root has a unique parent node w; every node with parent w is a child of w.

A tree can be empty. A node without children is a leaf.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Basic Notions

Binary Search Trees, Red-Black Trees – Practical Software Technology

Trees

[email protected]

Binary Trees

Trees

A tree where every node has at most two children is a binary tree. Children are called the left child and the right child.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Binary Search Trees

Binary Search Trees

A node’s left child must have a key less than its parent. A node’s right child must have a key greater or equal to its parent.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Node.java – Generic Implementation Binary Search Trees

1 2 3 4 5 6 7 8 9 10 11 12

p u b l i c c l a s s Node implements Comparable { E data ; Node l e f t C h i l d ; Node r i g h t C h i l d ; p u b l i c i n t compareTo ( Node o ) { i f ( d a t a == n u l l ) r e t u r n o . d a t a == n u l l ? 0 : −1; r e t u r n o . d a t a==n u l l ? 1 : d a t a . compareTo ( o . d a t a ) ; } }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Finding a Node

Binary Search Trees, Red-Black Trees – Practical Software Technology

Binary Search Trees

[email protected]

Tree.java – Finding a Node

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Binary Search Trees

p u b l i c c l a s s Tree { Node r o o t ; ... p u b l i c Node f i n d ( E d a t a ) { Node c u r = r o o t ; w h i l e ( c u r != n u l l && d a t a . e q u a l s ( c u r . g e t D a t a ( ) ) ) { i f ( c u r . g e t D a t a ( ) . compareTo ( d a t a ) > 0 ) cur = cur . l e f t C h i l d ; else cur = cur . r i g h t C h i l d ; } return cur ; } }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Inserting a Node

Binary Search Trees

Find an appropriate position to insert a node as leaf:

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree.java – Inserting a Node 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Binary Search Trees

p u b l i c void i n s e r t (E data ) { Node newNode = new Node(d a t a ) ; i f ( r o o t == n u l l ) { r o o t = newNode ; return ; } f o r ( Node c u r r e n t = r o o t ; c u r r e n t != n u l l ; ) { Node p a r e n t = c u r r e n t ; i f ( c u r r e n t . compareTo ( newNode ) > 0 ) { current = current . leftChild ; i f ( c u r r e n t == n u l l ) p a r e n t . l e f t C h i l d = newNode ; } else { current = current . rightChild ; i f ( c u r r e n t == n u l l ) p a r e n t . r i g h t C h i l d = newNode ; } } }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Find Minimum / Maximum

Binary Search Trees

Follow the left / right child as long as possible.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree.java – Finding the Minimum

1 2 3 4 5 6

Binary Search Trees

p u b l i c Node minimum ( ) { Node l a s t = n u l l ; f o r ( Node n = r o o t ; n != n u l l ; n = n . l e f t C h i l d ) last = n; return l a s t ; }

Maximum is similar.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Traversing the Tree: Depth-First

Binary Search Trees

Depth-first-traversal: Preorder: Visit the node, Traverse the nodes left subtree, Traverse the nodes right subtree.

Inorder: Traverse the nodes left subtree, Visit the node, Traverse the nodes right subtree.

Postorder: Traverse the nodes left subtree, Traverse the nodes right subtree, Visit the node.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree.java – Inorder Traversal I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Binary Search Trees

// Inner class h a s a c c e s s t o t h e t y p e v a r i a b l e public abstract class V i s i t o r { p u b l i c a b s t r a c t v o i d v i s i t ( Node node ) ; } public void inOrder ( V i s i t o r v i s i t o r ) { inOrder ( root , v i s i t o r ) ; } p r i v a t e v o i d i n O r d e r ( Node c u r r e n t , V i s i t o r i f ( c u r r e n t == n u l l ) return ; inOrder ( current . l e f t C h i l d , v i s i t o r ) ; v i s i t o r . v i s i t ( current ) ; inOrder ( current . rightChild , v i s i t o r ) ; }

Binary Search Trees, Red-Black Trees – Practical Software Technology

visitor ) {

[email protected]

Tree.java – Inorder Traversal II 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 11 12

Binary Search Trees

p u b l i c c l a s s Tree { ... // I n n e r c l a s s h a s a c c e s s t o t h e t y p e v a r i a b l e public abstract class V i s i t o r { p u b l i c a b s t r a c t v o i d v i s i t ( Node node ) ; } Tree t = new Tree () ; t . i n s e r t (5) ; t . i n s e r t (11) ; ... f i n a l S t r i n g B u i l d e r s b = new S t r i n g B u i l d e r ( ) ; t . i n O r d e r ( t.new V i s i t o r ( ) { p u b l i c v o i d v i s i t ( Node node ) { s b . append ( node . g e t D a t a ( ) ) . append ( ’ ’ ) ; } }) ; System . o u t . p r i n t l n ( s b . t o S t r i n g ( ) ) ;

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Traversing the Tree: Breadth-First

Binary Search Trees

Breadth-first-traversal: Levelorder: Use a Queue to go through the tree level-by-level. 2

Start with the root node and visit it (Level 0). Visit the left child, unless it is null.

3

Visit the right child, unless it is null.

1

Put it into the Queue. Put it into the Queue. 4

Poll the next node from the Queue and go to 2.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree.java – Levelorder Traversal

1 2 3 4 5 6 7 8 9 10 11 12

Binary Search Trees

public void l e v e l O r d e r ( V i s i t o r v i s i t o r ) { Queue queue = new ArrayDeque () ; queue . add ( r o o t ) ; w h i l e ( ! queue . i s E m p t y ( ) ) { Node c u r r e n t = queue . p o l l ( ) ; v i s i t o r . v i s i t ( current ) ; i f ( c u r r e n t . l e f t C h i l d != n u l l ) queue . add ( c u r r e n t . l e f t C h i l d ) ; i f ( c u r r e n t . r i g h t C h i l d != n u l l ) queue . add ( c u r r e n t . r i g h t C h i l d ) ; } }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Deleting a Node I

Binary Search Trees

Three cases: The node to be deleted is a leaf. The node to be deleted has one child. The node to be deleted has two children. The first case is trivial.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Deleting a Node II

Binary Search Trees, Red-Black Trees – Practical Software Technology

Binary Search Trees

[email protected]

Deleting a Node III

Binary Search Trees, Red-Black Trees – Practical Software Technology

Binary Search Trees

[email protected]

Deleting a Node IV

Binary Search Trees, Red-Black Trees – Practical Software Technology

Binary Search Trees

[email protected]

Tree.java – Finding the Successor

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Binary Search Trees

p r i v a t e Node g e t S u c c e s s o r ( Node d e l N o d e ) { Node s u c c e s s o r P a r e n t = d e l N o d e ; Node s u c c e s s o r = d e l N o d e ; // go t o r i g h t c h i l d f o r ( Node n = d e l N o d e . r i g h t C h i l d ; n != n u l l ; ) { successorParent = successor ; successor = n; n = n. leftChild ; } i f ( s u c c e s s o r != d e l N o d e . r i g h t C h i l d ) { successorParent . leftChild = successor . rightChild ; s u c c e s s o r . r i g h t C h i l d = delNode . r i g h t C h i l d ; } return successor ; }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Tree.java – Deleting a Node

1 2 3 4 5 6 7 8 9 10 11 12

Binary Search Trees

p u b l i c c l a s s Tree { ... /∗ ∗ ∗ R e t u r n s t r u e i n c a s e o f s u c c e s s and f a l s e ∗ i f t h e g i v e n node was n o t f o u n d . ∗/ p u b l i c b o o l e a n d e l e t e ( E node ) { see Exercise 8 } p r i v a t e Node g e t S u c c e s s o r ( Node d e l N o d e ) { ... }

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Out of Balance?

Binary Search Trees

If the binary tree is balanced, find, insert, delete needs O(log n) time. What happens if the values to be inserted are already ordered? Binary trees might become unbalanced over time. The ability to quickly find, insert, delete a given element is lost.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Solution: Red-Black Trees

Red-Black Trees

A red-black tree is a binary search tree with colored nodes. It uses O(1) structural changes after an update to stay balanced. The height of a red-black tree storing n entries is O(log n). 1 2 3 4

Every node is either red or black. The root is always black. If a node is red, its children must be black. Every path from the root to a “null child”, must contain the same number of black nodes.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Keeping the Balance

Red-Black Trees

The rules ensure that the height is bound by O(log n). 1

Every node is either red or black.

2

The root is always black.

3

If a node is red, its children must be black.

4

Every path from the root to a “null child”, must contain the same number of black nodes.

To keep this rules intact: You can change the colors of nodes. You can perform rotations. Rotations must do two things at once: Raise some nodes and lower others to help balance the tree. Ensure that the characteristics of a binary search tree are not violated.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Example: Change the Color

Red-Black Trees

To insert a node 12, the colors need to be changed:

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Example: Rotations are Required

Red-Black Trees

The node 6, cannot be inserted without rotations:

Solution: Rotate right such that 25 is the new root. Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Rotations

Red-Black Trees

One node is chosen as the “top” of the rotation. If we’re doing a right rotation, this “top” node will move down and to the right, into the position of its right child.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Rotations

Red-Black Trees

One node is chosen as the “top” of the rotation. If we’re doing a right rotation, this “top” node will move down and to the right, into the position of its right child.

Inorder traversal yields the same result! Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Inserting a Node 1 2 3 4 5

Red-Black Trees

p u b l i c c l a s s Node implements Comparable { bo o l e a n b l a c k ; ... }

On the way down to the insertion point: If the current node is black and its two children are both red then: 1 2

3

Color the children black. Color the current red, unless it is the root. Check that there are no violations of Rule 3 (Children of red must be black). If so, perform the appropriate rotations. (At most 2 are needed.)

When you reach a leaf node, insert the new node with color red. Check again for red-red conflicts, and perform any necessary rotations.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Advantages

Red-Black Trees

The space complexity is O(n), where n is the size of the input. The tree-height is bound by O(log n). Searching is done in O(log n) for the worst case time. Insertion is done in O(log n) for the worst case time. Deletion is done in O(log n) for the worst case time.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Deleting a Node

Red-Black Trees

It can be done in O(log n) time, but: If deletion is not performed frequently, then a “deleted flag” can be used to increase the performance. Store a boolean value for each node and mark it as deleted instead of recoloring nodes and rotating the tree.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Other Examples for Trees

Trees

Of course, not every tree is a search tree. Trees can be used for data compression (Huffman-Tree). Trees can be used to represent algebraic expression.

Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Exercise Implement the delete method for the binary tree. Use the method getSuccessor to find the successor like discussed during the lecture. 1 2 3 4 5 6 7 8 9 10 11 12

p u b l i c c l a s s Tree { ... /∗ ∗ ∗ R e t u r n s t r u e i n c a s e o f s u c c e s s and f a l s e ∗ i f t h e g i v e n node was n o t f o u n d . ∗/ p u b l i c b o o l ean d e l e t e ( E node ) { // TODO: I m p l e m e n t t h i s a l g o r i t h m . } p r i v a t e Node g e t S u c c e s s o r ( Node d e l N o d e ) { ... }

See the guidance for this exercise on the Moodle page. Binary Search Trees, Red-Black Trees – Practical Software Technology

[email protected]

Suggest Documents