ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand...

22
ITEC200 Week 11 Self-Balancing Search Trees
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

Transcript of ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand...

Page 1: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

ITEC200 Week 11

Self-Balancing Search Trees

Page 2: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 2

Learning Objectives Week 11 (ch 11)

• To understand the impact that balance has on the performance of binary search trees

• To learn about the AVL tree for storing and maintaining a binary search tree in balance

• To learn about the Red-Black tree for storing and maintaining a binary search tree in balance

• To learn about 2-3 trees, 2-3-4 trees, and B-trees and how they achieve balance

• To understand the process of search and insertion in each of these trees and to be introduced to removal

Page 3: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 3

Why Balance is Important

• Searches into an unbalanced search tree could be O(n) at worst case

Page 4: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 4

Rotation (continued)

Page 5: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 5

AVL Tree

• As items are added to or removed from the tree, the balance or each subtree from the insertion or removal point up to the root is updated

• Rotation is used to bring a tree back into balance• The height of a tree is the number of nodes in the

longest path from the root to a leaf node

Page 6: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 6

Balancing a Left-Left Tree

• The heights of the left and right subtrees are unimportant; only the relative difference matters when balancing

• A left-left tree is a tree in which the root and the left subtree of the root are both left-heavy

• Right rotations are required

Page 7: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 7

Balancing a Left-Right Tree

• Root is left-heavy but the left subtree of the root is right-heavy

• A simple right rotation cannot fix this• Need both left and right rotations

Page 8: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 8

Four Kinds of Critically Unbalanced Trees

• Left-Left (parent balance is -2, left child balance is -1)– Rotate right around parent

• Left-Right (parent balance -2, left child balance +1)– Rotate left around child

– Rotate right around parent

• Right-Right (parent balance +2, right child balance +1)– Rotate left around parent

• Right-Left (parent balance +2, right child balance -1)– Rotate right around child

– Rotate left around parent

Page 9: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 9

Red-Black Trees

• Rudolf Bayer developed the red-black tree as a special case of his B-tree

• A node is either red or black• The root is always black• A red node always has black children• The number of black nodes in any path from the

root to a leaf is the same

Page 10: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 10

Insertion into a Red-Black Tree

• Follows same recursive search process used for all binary search trees to reach the insertion point

• When a leaf is found, the new item is inserted and initially given the color red

• It the parent is black we are done otherwise there is some rearranging to do

Page 11: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 11

2-3 Trees

• 2-3 tree named for the number of possible children from each node

• Made up of nodes designated as either 2-nodes or 3-nodes

• A 2-node is the same as a binary search tree node

• A 3-node contains two data fields, ordered so that first is less than the second, and references to three children

• One child contains values less than the first data field

• One child contains values between the two data fields

• Once child contains values greater than the second data field

• 2-3 tree has property that all of the leaves are at the lowest level

Page 12: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 12

Searching a 2-3 Tree

Page 13: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 13

Inserting into a 2-3 Tree

Page 14: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 14

Removal from a 2-3 Tree

• Removing an item from a 2-3 tree is the reverse of the insertion process

• If the item to be removes is in a leaf, simply delete it

• If not in a leaf, remove it by swapping it with its inorder predecessor in a leaf node and deleting it from the leaf node

Page 15: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 15

Removal from a 2-3 Tree (continued)

Page 16: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 16

2-3-4 and B-Trees

• 2-3 tree was the inspiration for the more general B-tree which allows up to n children per node

• B-tree designed for building indexes to very large databases stored on a hard disk

• 2-3-4 tree is a specialization of the B-tree because it is basically a B-tree with n equal to 4

• A Red-Black tree can be considered a 2-3-4 tree in a binary-tree format

Page 17: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 17

2-3-4 Trees

• Expand on the idea of 2-3 trees by adding the 4-node

• Addition of this third item simplifies the insertion logic

Page 18: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 18

Relating 2-3-4 Trees to Red-Black Trees

• A Red-Black tree is a binary-tree equivalent of a 2-3-4 tree

• A 2-node is a black node• A 4-node is a black node with two red children• A 3-node can be represented as either a black node

with a left red child or a black node with a right red child

Page 19: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 19

Relating 2-3-4 Trees to Red-Black Trees (continued)

Page 20: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 20

B-Trees

• A B-tree extends the idea behind the 2-3 and 2-3-4 trees by allowing a maximum of CAP data items in each node

• The order of a B-tree is defined as the maximum number of children for a node

• B-trees were developed to store indexes to databases on disk storage

Page 21: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 21

Where to from here…

• Work through Chapter 11 of the Koffman & Wolfgang Text

• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class

Page 22: ITEC200 Week 11 Self-Balancing Search Trees.  2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.

www.ics.mq.edu.au/ppdp 22

Acknowledgements

These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 11 PowerPoint presentation

by Elliot B. Koffman and Paul A. T. Wolfgang