Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees."— Presentation transcript:

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

2 AVL Trees The 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.

3 AVL 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.

4 Balancing an AVL Tree 30 20 10 30 20 10

5 Balancing an AVL Tree 40 20 10 3050 60 40 20 10 50 60 30

6 A Double Rotation 40 20 10 30 50 60 2535 22 40 20 10 3050 60 25 35 22

7 A Double Rotation 30 10 2040 50 22 25 60 35 40 20 10 3050 60 25 35 22

8 Inserting 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.

9 AVLNode - data : Object - left : AVLNode - right : AVLNode - parent: AVLNode + createAVLNode() + getData() + setData() + getLeft() + setLeft() + getRight() + setRight() + getParent() + setParent()

10 Red 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.

11 Red Black Tree Rules Every node is either red or black At the end of every branch is a black NULL node If 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.

12 RBBTNode - data : Object - left : RBBTNode - right : RBBTNode - color : char + createRBBTNode() + getData() : Object + setData() + getLeft() : RBBTNode + setLeft() + getRight() : RBBTNode + setRight() + getColor() : char + setColor()

13 2-3 Trees

14 A 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.

15 2-3 Node Pictorially leftValuerightValue firstChildthirdChildsecondChild

16 When Used as a 2-Node leftValuerightValue firstChildthirdChildsecondChild < LV> LV

17 When Used as a 3-Node leftValuerightValue firstChildthirdChildsecondChild < LV> RV> LV && < RV

18 A 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)

19 Inserting into a 2-3 Tree Insert 39 Insert 38

20 Inserting into a 2-3 Tree (con'd) Insert 37Insert 36 Insert 33, 35, 34

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

22 Searching 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.

23 Searching 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 right else (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

24 Inserting 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 rightValue else ( 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.

25 2-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)

26 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)

27 leftValuemiddleValuerightValue firstChildthirdChildfourthChildsecondChild

28 Used as a 2-Node leftValuemiddleValuerightValue firstChildthirdChildfourthChildsecondChild < MV> MV

29 Used as a 3-Node leftValuemiddleValuerightValue firstChildthirdChildfourthChildsecondChild < LV> LV && < RV> RV

30 Used as a 4-Node leftValuemiddleValuerightValue firstChildthirdChildfourthChildsecondChild < LV> LV && < MV> RV> MV && < RV

31 Insertion 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.

32 6 Cases for Splitting a 4-Node The 4-Node is the root The 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

33 Case 1: 4-Node is the root

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

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

36

37 2-3-4 Tree Search Algorithm IF 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…

38 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

39 Example

40 Example Continued

41

42 Deletion 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.

43 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.


Download ppt "Balanced Trees AVL Trees Red-Black Trees 2-3 Trees 2-3-4 Trees."

Similar presentations


Ads by Google