balanced trees avl trees red-black trees 2-3 trees 2-3-4 trees

43
Balanced Trees Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Upload: candice-ellis

Post on 01-Jan-2016

275 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Balanced TreesBalanced Trees

AVL TreesRed-Black Trees2-3 Trees2-3-4 Trees

Page 2: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

AVL TreesAVL TreesThe earliest form of balanced tree,

originally proposed in 1962.Conceptually easy to understand, but

difficult to code.Based on the concept, that for every sub-

tree of a BST, if the difference in height of the two branches is 0 or 1, that sub-tree is balanced. If more than 1, it is unbalanced, and a rotation has to be performed to balance the tree.

Page 3: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

AVL TreesAVL Trees

Every time a node is inserted, the sub-tree it's in must be checked for balance.

The problem is adding a node might not unbalance the sub-tree it's in, it might unbalance a higher level sub-tree.

Every time a node is added, every sub-tree up to the root has to be checked and rebalanced, if necessary.

Page 4: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Balancing an AVL TreeBalancing an AVL Tree

30

20

10

30

20

10

Page 5: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Balancing an AVL TreeBalancing an AVL Tree

40

20

10

30 50

60

40

20

10

50

60

30

Page 6: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

A Double RotationA Double Rotation

40

20

10 30

50

60

25 35

22

40

20

10

30 50

60

25

35

22

Page 7: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

A Double RotationA Double Rotation

30

10

20 40

50

22

25

60

35

40

20

10

30 50

60

25

35

22

Page 8: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Inserting into an AVL TreeInserting into an AVL Tree

Insert the new node as you would into a normal BST.

Starting from the parent of the new node:◦Get the depth of the left branch.◦Get the depth of the right branch.◦If abs(right – left) is > 1, rotate.◦Recurse up to the next parent.

Page 9: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

AVLNode

- data : Object

- left : AVLNode

- right : AVLNode

- parent: AVLNode

+ createAVLNode()

+ getData()

+ setData()

+ getLeft()

+ setLeft()

+ getRight()

+ setRight()

+ getParent()

+ setParent()

Page 10: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Red Black TreesRed Black Trees

Originally invented in 1972 and called "Symmetric Binary B-trees".

Acquired its modern name in 1978 to make it easier to understand and to code.

Page 11: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Red Black Tree RulesRed Black Tree Rules

Every node is either red or blackAt the end of every branch is a black NULL

nodeIf a node is red, both of its children are

black (Can't have two red nodes in a row.)Every path from a node to a descendant

leaf must have the same number of black nodes.

Page 12: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

RBBTNode

- data : Object

- left : RBBTNode

- right : RBBTNode

- color : char

+ createRBBTNode()

+ getData() : Object

+ setData()

+ getLeft() : RBBTNode

+ setLeft()

+ getRight() : RBBTNode

+ setRight()

+ getColor() : char

+ setColor()

Page 13: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3 Trees2-3 Trees

Page 14: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3 Trees2-3 TreesA 2-3 Tree is always height balanced.A 2-3 Tree has 2-Nodes and 3-Nodes

◦A 2-Node has 1 data item and 2 children.◦A 3-Node has 2 data items and 3 children.

New values are always added at the leaf level.

The only time the height of the tree changes is when the root node has too many values in it, and has to be split.

Page 15: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3 Node Pictorially2-3 Node Pictorially

leftValue rightValue

firstChild thirdChildsecondChild

Page 16: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

When Used as a 2-NodeWhen Used as a 2-Node

leftValue rightValue

firstChild thirdChildsecondChild

< LV > LV

Page 17: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

When Used as a 3-NodeWhen Used as a 3-Node

leftValue rightValue

firstChild thirdChildsecondChild

< LV > RV> LV && < RV

Page 18: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

A Node for a 2-3 TreeA Node for a 2-3 Tree

Node23

- leftValue : Object

- rightValue : Object

- firstChild : Node23

- secondChild : Node23

- thirdChild : Node23

- parent : Node23

- type : int

+ getType : int

+ setType : void

(Accessors and Mutators

for all fields of instance data)

Page 19: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Inserting into a 2-3 TreeInserting into a 2-3 Tree

Insert 39

Insert 38

Page 20: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Inserting into a 2-3 Tree (con'd)Inserting into a 2-3 Tree (con'd)

Insert 37 Insert 36

Insert 33, 35, 34

Page 21: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Inserting into a 2-3 Tree (con'd)Inserting into a 2-3 Tree (con'd)

Inserting 32

Page 22: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Searching a 2-3 TreeSearching a 2-3 Tree

Searching is similar to searching a BST, except you may have 2 values to compare, and 3 possible paths to take.

If you're sitting on a 2-Node, it's exactly the same as a BST.

Page 23: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Searching a 2-3 TreeSearching a 2-3 Tree

if node is a 2-Node if key == node value return current node if key < node value traverse to the left else traverse to the rightelse (node is a 3-node) if key == left value return current node if key < left value traverse to the left else if key == right value return current node if key > right value traverse to the right else traverse to the middle

Page 24: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Inserting into a 2-3 TreeInserting into a 2-3 Tree

if the node to insert into is a 2-Node if the new value is less than the value in the node push the old value into the rightValue assign the new value to the leftValue else (new value is greater than old value) assign new value to rightValueelse ( it's a 3-Node ) The middle value of current left, current right and new value is going to get pushed up to the parent. The lowest of the 3 values is going to get pushed into its own node to the left. The highest of the 3 values is going to get pushed into a new node on the right. Now, recursively, does pushing the middle value up to the parent cause a split up there? If I'm splitting the root node (parent == null), the middle value will be pushed into a new root node.

Page 25: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3-4 Trees2-3-4 Trees

Always perfectly balanced, every leaf has the same depth.

Not a binary tree, nodes may contain◦1 data value and 2 children (2-Node)◦2 data values and 3 children (3-Node)◦3 data values and 4 children (4-Node)

Page 26: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Node234

- leftValue : Object

- middleValue : Object

- rightValue : Object

- firstChild : Node234

- secondChild : Node234

- thirdChild : Node234

- fourthChild : Node234

- parent : Node234

- type : int

+ getType : int

+ setType : void

(Accessors and Mutators

for all fields of instance data)

Page 27: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

leftValue middleValue rightValue

firstChild thirdChild fourthChildsecondChild

Page 28: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Used as a 2-NodeUsed as a 2-Node

leftValue middleValue rightValue

firstChild thirdChild fourthChildsecondChild

< MV > MV

Page 29: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Used as a 3-NodeUsed as a 3-Node

leftValue middleValue rightValue

firstChild thirdChild fourthChildsecondChild

< LV > LV && < RV > RV

Page 30: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Used as a 4-NodeUsed as a 4-Node

leftValue middleValue rightValue

firstChild thirdChild fourthChildsecondChild

< LV > LV && < MV > RV> MV && < RV

Page 31: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Insertion into a 2-3-4 TreeInsertion into a 2-3-4 Tree

New values get inserted to leaves only.As you're looking for the node to insert

into, split any 4-Nodes you run into.By splitting any 4-Node we run into, we

always have room to insert the new value when we find out where it belongs.

Inserting and splitting never changes the height of the tree, unless the root node is being split.

Page 32: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

6 Cases for Splitting a 4-Node6 Cases for Splitting a 4-Node

The 4-Node is the rootThe 4-Node is the child of a 2-Node

◦The 4-Node is the left (first) child◦The 4-Node is the right (fourth) child

The 4-Node is the child of a 3-Node◦The 4-Node is the first child◦The 4-Node is the second child◦The 4-Node is the fourth child

Page 33: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Case 1: 4-Node is the rootCase 1: 4-Node is the root

Page 34: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Cases 2,3: Parent is a 2-NodeCases 2,3: Parent is a 2-Node

Page 35: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Cases 4,5,6: Parent is a 3-NodeCases 4,5,6: Parent is a 3-Node

Page 36: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Cases 4,5,6: Parent is a 3-NodeCases 4,5,6: Parent is a 3-Node

Page 37: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3-4 Tree Search Algorithm2-3-4 Tree Search AlgorithmIF this node is null, return null

IF this is a 2-Node

IF key == middle value

return this;

ELSE IF key < middle value

recurse down the left subtree

ELSE

recurse down the right subtree

ELSE IF this is a 3-Node

IF key == left value

return this;

ELSE IF key < left value

recurse down left subtree

ELSE IF key == right value

return this;

ELSE IF key < right value

recurse down the middle subtree

ELSE recurse down the right subtree continued…

Page 38: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

2-3-4 Tree Search Algorithm (con'd)2-3-4 Tree Search Algorithm (con'd)

ELSE (this is a 4-Node)

IF key == left value

return this;

ELSE IF key < left value

recurse down first subtree

ELSE IF key == middle value

return this;

ELSE IF key < middle value

recurse down second subtree

ELSE IF key == right value

return this;

ELSE IF key < right value

recurse down third subtree

ELSE

recurse down fourth subtree

Page 39: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

ExampleExample

Page 40: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Example ContinuedExample Continued

Page 41: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Example ContinuedExample Continued

Page 42: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Deletion from a 2-3-4 TreeDeletion from a 2-3-4 Tree

Similar to deletion from a Binary Search Tree:◦Find the highest value in the left subtree (will

always be in a leaf node)◦Swap the values in the two nodes◦Remove the value from the leaf node◦Problems arise when the value to be deleted is

in a 2-Node.

Page 43: Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees

Deletion (con'd)Deletion (con'd)

Leaf nodes cannot simply be removed.2-Nodes with value to be deleted must be

MERGED into other nodes (the opposite of splitting), and then, the value removed.

Just like there are 6 split cases, they are MANY merge cases, depending on what types of node the brothers are, and what type of node the parent is.

We're not going to cover it here.