Welcome to CIS 068 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Welcome to CIS 068

Description:

has a root node, a left binary tree, and a right binary tree as children ... (Adelson-Velskii Landis, 1962) CIS 068. Idea: ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 27
Provided by: rolflak
Learn more at: https://cis.temple.edu
Category:
Tags: cis | landis | welcome

less

Transcript and Presenter's Notes

Title: Welcome to CIS 068


1
Welcome to CIS 068 !
Lesson 12 Data Structures 3 Trees
2
Overview
  • Binary Trees
  • Complete Trees
  • Heaps
  • Binary Search Trees
  • Balanced Trees

3
Definitions
root
Node 0 is ancestor of all other nodes Nodes 1-6
are descendants of node 0
Node 0
Node 1,2,3 are children of root
Node 1
Node 2
Node 3
Node 1 is parent of Nodes 4,5
Node 4
Node 5
Node 6
Node 4 and 5 are siblings
leaves
4
Binary Trees
  • Def. (recursively defined data structure)
  • A binary tree is either
  • empty (no nodes), or
  • has a root node, a left binary tree, and a right
    binary tree as children
  • hence it is a tree with at most two children
    for each node

5
Complete Trees
  • Def.
  • A tree in which all leaf nodes are at some depth
    n or n-1, and all leaves at depth n are toward
    the left

depth 1 depth 2 depth 3
complete
incomplete
incomplete
6
Complete Trees
  • Properties
  • A complete tree with depth n has at most 2n1 1
    elements
  • A complete tree with depth n has at least 2n
    elements
  • The index of the left child of node k is 2k1,
    the index of the right child of node is 2k2

0
depth 1 depth 2 depth 3
1
2
3
4
5
6
7
8
7
Complete Trees
  • Storage of complete trees in arrays

0
1
2
3
4
5
6
7
8

0
1
2
3
4
5
6
7
8
2k1, 2k2
k3
8
Heap
  • Def.
  • A complete binary tree where every node has a
    value greater than or equal to the key of its
    parent

89
76
80
37
32
39
9
Heap
  • What for ?
  • Sorting (HEAPSORT)
  • Sort elements into heap structure
  • How to
  • Insert ?
  • Delete ?
  • Order of magnitude ?

10
Heap
  • Example of Heapsort HEAPSORT-APPLET
  • Insert / Delete (board)
  • Heaps provide a structure for efficient retrieval
    of maximum values !
  • How to look for arbitrary values ? Binary Search
    Trees !

11
Binary Search Trees
  • Def.
  • A binary tree where every node's left subtree has
    values less than the node's value, and every
    right subtree has values greater.

76
39
80
32
47
79
12
Binary Search Trees
  • Remarks
  • A heap is NOT a binary search tree !
  • A binary search tree is not necessarily complete
    (see example)!
  • (Worst case create BST of sorted list)

89
80
76
39
32
37
13
Binary Search Trees
  • How to search in binary search tree ?
  • (Answer is straightforward)
  • Applet animated BST
  • Order of magnitude ?
  • How to achieve O(log n) ?
  • balanced binary search trees !

14
Balanced Trees
  • Def.
  • A tree whose subtrees differ in height by no more
    than one and the subtrees are height-balanced,
    too. An empty tree is height-balanced.

15
Balanced Trees
  • Remark
  • Every complete tree is balanced, but not vice
    versa !

12
18
8
5
11
17
4
16
Binary Search Trees
  • How to create balanced trees ?
  • Rotation !

17
Rotation
18
Rotation
19
Rotation
20
AVL Trees
  • How to use Rotation to create balanced trees
  • AVL Trees
  • (Adelson-Velskii Landis, 1962)

21
AVL Trees
  • Idea
  • Keep track of the difference in the depth of each
    subtree as items are inserted or removed
  • Use rotation to bring tree into balance if
    difference is not in range of -1

22
AVL Trees
  • Example 1 single rotation
  • Is a single rotation always the solution ?

23
AVL Trees
  • Example 2 single rotation
  • Example 2 the left-heavy tree got a right-heavy
    tree !

24
AVL Trees
  • Example 3 rotation of subtrees
  • Balance is achieved by rotating the subtrees in
    a certain way

25
AVL Trees
  • Resume
  • Special binary trees provide an efficient
    structure for sorting and searching
  • Complete binary trees can be stored in an array
    without link-structure overhead
  • Heaps are used to sort arrays for retrieval of
    maximum values (typical application
    shape-abstraction- assignment !)
  • Binary search trees are used for access to
    arbitrary objects in O(log n), achieved by
    balancing trees
  • AVL trees are one example for balanced trees,
    using rotation to keep the balance

26
Trees
  • to be continued
Write a Comment
User Comments (0)
About PowerShow.com