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

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 1/33 4. Trees Part 2
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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?
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 8/33 Rotations on Binary Search Tree demo - 1 Single rotation
Image of page 8
Data Structures and Algorithms in Java 9/33 Rotations on Binary Search Tree demo - 2 Double rotation
Image of page 9

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

View Full Document Right Arrow Icon
Data Structures and Algorithms in Java 10/33 AVL Trees An AVL tree (by A delson V
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern