Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.

Similar presentations


Presentation on theme: "Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn."— Presentation transcript:

1 Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn

2 Basics What Is It? ‘A simple data structure that extends the binary search algorithm to allow for insertions and deletions’ Average time for operations is O(log N) but a worst case scenario of O(N)

3 Basics continued… How Do We Avoid Worst Case? AVL Trees Red-Black Trees AA-Trees Focus Will Be: AVL Trees

4 Some Definitions Order Property For every node x, values in left subtree are less than x and values in right subtree are greater than x Duplicate nodes or key values can be handled by a counter

5 Basic Operations or Methods find() Start at root and go left or right based on comparison findMin() Start at root and keep going left findMax() Start at root and keep going right insert() remove()

6 A Search Tree

7 Not A Search Tree

8 Problems Insertions and Deletions Can Lead To Unbalanced Tree If Input Is Sorted… If Input Contains Long Sequence of Nonrandomness

9 Insertion Sequence: 7,2,9,5,3,1

10 Insertion Sequence: 3 2 1 9 7 5

11 Deleting A Node Most complicated operation If node is a leaf = delete If node has one child, connect child to node to be deleted’s parent, then delete If node has two children = most complicated!

12 Deleting a Node with 2 Children Idea: Replace node to be deleted with the smallest node in the right subtree then remove the node Finding smallest node is easy -> keep going left in right subtree

13 Deletion Example: Single Child Remove 5

14 Deletion Example: Single Child with a Subtree Remove 5

15 Deletion Example: 2 Children Remove 2

16 Balanced Tree

17 An Unbalanced Tree

18 AVL Trees Balanced Search Tree Adelson-Velskii and Landis for those historically inclined Satisfies O(log N)

19 Basic Idea Require left and right tree are same height Using recursion, this implies that all nodes must adhere to this as each node is a root of a subtree Insertion become difficult due to balancing

20 AVL Definition A Binary Search Tree with balance property that for any node, the left and right subtree heights differ by no more than 1.

21 AVL Tree…Not

22 AVL Tree…Not – The Problem

23 Balancing Approaches ‘Random’ insertions and deletions will eventually lead to an unbalanced tree To simplify task, information regarding the height of a node's subtrees is stored in the node structure. If we know the relative height, we can arrange to simply store a value that indicates The subtree heights are the same Left subtree is higher Right subtree is higher

24 It All Boils Down To... Only nodes on along the insertion to the root may have their balances alterered...because...only their subtrees are being changed A height imbalance occurs if for a node X abs(height(left subtree of X) – height(right subtree of X)) = 2

25 The 4 Cases for Rotations 1. Insertion in left subtree of left child of X 2. Insertion in right subtree of left child of X 3. Insertion in left subtree of right child of X 4. Insertion in right subtree of right child of X

26 Become 2 Cases for Rotations 1 & 4 are symmetrically the same -> left–left & right–right Handled by Single Rotation 2 & 3 are symmetrically the same -> right–left & left-right Handled by more complicated Double Rotation

27 The Single Rotation Switches Role of Parent and Child while maintaining search order Before Inserting 1 During After

28 Double Rotation Start at page 627

29 A ‘Simple’ Project???? public class AVLNode extends BinaryNode { public static final int LEFT_HEAVY = -1; public static final int RIGHT_HEAVY = +1; public static final int EQUAL = 0; private int balance; AVLNode(Comparable theElement) { super(theElement); this.balance = EQUAL; } public int getBalance( { return balance;} public void setBalance(int balance) { this.balance = balance;} }


Download ppt "Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn."

Similar presentations


Ads by Google