Cartesian Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Cartesian Trees

Description:

Cartesian Trees Amihood Amir Bar-Ilan University – PowerPoint PPT presentation

Number of Views:257
Avg rating:3.0/5.0
Slides: 35
Provided by: amir84
Category:
Tags: binary | cartesian | tree | trees

less

Transcript and Presenter's Notes

Title: Cartesian Trees


1
Cartesian Trees
  • Amihood Amir
  • Bar-Ilan University

2
Definition
  • Let A1,,An be an array of numbers.
  • The Cartesian Tree TA of A is a binary tree
    defined recursively as follows
  • Let Ai be the smallest number in A.
  • Root of TA Ai
  • Left son of Ai The root of the Cartesian Tree
    of A1,,Ai-1.
  • Right son of Ai The root of the Cartesian Tree
    of Ai1,,An.

3
Example
  • 9 2 8 10 1 5 6 7 2 10
    3
  • 1
  • 2
    2
  • 9 8 5
    3
  • 10 6
    10
  • 7

4
Construction
  • Let A1,,An be an array of numbers.
  • The Cartesian Tree TA of A can be constructed as
    follows
  • Greedily from left to right
  • Assume the Cartesian Tree of A1,,Ai
  • Has been constructed.
  • The last element, Ai, is the rightmost.

5
Construction (cont.)
  • Consider Ai1.
  • If it is larger than Ai, then make it the right
    son of Ai.
  • Otherwise, go up toward the root until either
  • a smaller element is encountered. Or
  • The root is reached.

6
Construction (cont.)
  • Case 1 A smaller element x is encountered.
  • Make Ai1 the right child of x.
  • Make xs right child the left son of
    Ai1.
  • Case 2 No smaller element.
  • Make Ai1 the root of the tree.
  • Make the former root the left son

  • of Ai1.

7
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

9
8
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

9
gt2
9
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
9
10
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
lt8
9
11
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
8
9
12
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
9
8
lt10
13
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
9
8
10
14
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
9
8
gt1
10
15
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
9
8
gt1
10
16
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

2
gt1
9
8
10
17
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
9
8
10
18
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
lt5
2
9
8
10
19
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
9
8
10
20
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
lt6
9
8
10
21
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
6
9
8
10
22
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
6
lt7
9
8
10
23
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
6
9
8
7
10
24
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
6
9
8
7
gt2
10
25
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
6
gt2
9
8
7
10
26
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
5
gt2
6
9
8
7
10
27
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
lt2
2
5
6
9
8
7
10
28
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
lt10
2
2
5
9
8
6
10
7
29
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
2
10
5
9
8
6
10
7
30
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
2
10
gt3
5
9
8
6
10
7
31
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
2
lt3
10
5
9
8
6
10
7
32
Example
  • 9 2 8 10 1 5 6 7 2 10
    3

1
2
2
3
5
9
8
6
10
10
7
33
Algorithm Correctness
  • Clear by definition.

34
Algorithm Correctness
  • Clear by definition.

Algorithm Time
For every element O(1) changes. Only problem
climb up involves many comparisons. Note Every
element is being compared to at most once,
because then it moves to the left and is never
compared to again. Therefore Time amortizes to
O(n).
Write a Comment
User Comments (0)
About PowerShow.com