Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cartesian Trees Amihood Amir Bar-Ilan University.

Similar presentations


Presentation on theme: "Cartesian Trees Amihood Amir Bar-Ilan University."— Presentation transcript:

1 Cartesian Trees Amihood Amir Bar-Ilan University

2 Definition Let A[1],…,A[n] be an array of numbers. The Cartesian Tree T A of A is a binary tree defined recursively as follows: Let A[i] be the smallest number in A. Root of T A : A[i] Left son of A[i]: The root of the Cartesian Tree of A[1],…,A[i-1]. Right son of A[i]: The root of the Cartesian Tree of A[i+1],…,A[n].

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 A[1],…,A[n] be an array of numbers. The Cartesian Tree T A of A can be constructed as follows: Greedily from left to right: Assume the Cartesian Tree of A[1],…,A[i] Has been constructed. The last element, A[i], is the rightmost.

5 Construction (cont.) Consider A[i+1]. If it is larger than A[i], then make it the right son of A[i]. Otherwise, go up toward the root until either: 1.a smaller element is encountered. Or 2.The root is reached.

6 Construction (cont.) Case 1: A smaller element x is encountered. Make A[i+1] the right child of x. Make x’s right child the left son of A[i+1]. Case 2: No smaller element. Make A[i+1] the root of the tree. Make the former root the left son of A[i+1].

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>2>2

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

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

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

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

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

14 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 >1>110

15 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8>1>1 10

16 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 >1>1 10

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

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

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

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

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

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

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

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

25 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 1 10 5 6 7 >2>2

26 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 1 10 5 6 7 >2>2

27 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 1 10 5 6 7 <2<2

28 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 1 10 5 6 7 2 <10

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

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

31 Example 9 2 8 10 1 5 6 7 2 10 3 9 2 8 1 10 5 6 7 2 <3<3

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

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).


Download ppt "Cartesian Trees Amihood Amir Bar-Ilan University."

Similar presentations


Ads by Google