Docsity

Balanced Binary Search Trees - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Balanced Binary Search Trees - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Balanced Binary Search Trees

• height is O(log n), where n is the

number of elements in the tree

• AVL (Adelson-Velsky and Landis)

trees

• red-black trees

• get, put, and remove take O(log n)

time

Balanced Binary Search Trees

• Indexed AVL trees

• Indexed red-black trees

• Indexed operations also take

O(log n) time

AVL Tree

• binary tree

• for every node x, define its balance factor

balance factor of x = height of left subtree of x

- height of right subtree of x

• balance factor of every node x is -1, 0, or 1

Balance Factors

This is an AVL tree.

AVL Search Tree

put(9)

0

0

put(29)

RR rotation.

AVL Rotations

• RR

• LL

• RL

• LR

Example Red Black Tree

Red Black Trees

Colored Edges Definition

• Binary search tree.

• Child pointers are colored red or black.

• Pointer to an external node is black.

• No root to external node path has two

consecutive red pointers.

• Every root to external node path has the

same number of black pointers.

Red Black Tree

• The height of a red black tree that has n

(internal) nodes is between log

(n+1) and

2log

(n+1).

• java.util.TreeMap => red black tree

Graphs

• G = (V,E)

• V is the vertex set.

• Vertices are also called nodes and points.

• E is the edge set.

• Each edge connects two different vertices.

• Edges are also called arcs and lines.

• Directed edge has an orientation (u,v).

u v

Undirected Graph

Directed Graph (Digraph)