4B-Trees2

# 4B-Trees2 - 4 Trees Part 2 Data Structures and 1/33...

• 33

This preview shows pages 1–11. Sign up to view the full content.

Data Structures and Algorithms in Java 1/33 4. Trees Part 2

This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 2/33 Balanced Binary Tree Definitions Balancing a Tree - Simple Balanced Algorithm Rotations on Binary Search Tree AVL Tree Insertion in an AVL Tree Deletion in an AVL Tree Heaps Heaps as Priority Queue Polish Notation and Expression Trees 2 Objectives
Data Structures and Algorithms in Java 3/33 A binary tree is height-balanced if for every internal node p of T , the heights of the children of p differ by at most 1. A tree is considered perfectly balanced if it is height- balanced and all leaves are to be found on one level or two levels. What is a balanced tree in general? There are several ways to define "Balanced". The main goal is to keep the depths of all nodes to be O(log(n)). Balanced Binary Tree Definitions

This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 4/33 4 Different binary search trees with the same information Why need to balance a Binary Search Tree?
Data Structures and Algorithms in Java 5/33 5 Creating a binary search tree from an ordered array Balancing a Tree - Simple Balance Algorithm - 1 Balancing a tree: 1.Copy all tree nodes to an array 2.Sort the array 3.Clear the tree 4.Call balance() method to rebuild the tree

This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 6/33 public void balance(T data[], int first, int last) { if (first <= last) { int middle = (first + last)/2; insert(data[middle]); balance(data,first,middle-1); balance(data,middle+1,last); } } public void balance(T data[]) { balance(data,0,data.length-1); } 6 Balancing a Tree - Simple Balance Algorithm - 2
Data Structures and Algorithms in Java 7/33 Right Rotation 7 Right rotation of Par about its' left child Ch rotateRight: rotate the node Par to right about its left child Ch Ch become new root of the subtree Right subtree of Ch becomes left subtree of Par Par subtree becomes right subtree of Ch Right rotation Left rotation Rotations on Binary Search Tree

This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 8/33 Rotations on Binary Search Tree demo - 1 Single rotation
Data Structures and Algorithms in Java 9/33 Rotations on Binary Search Tree demo - 2 Double rotation

This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 10/33 AVL Trees An AVL tree (by A delson V
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern