Given three integers A, B and L, the task is to print the ternary cantor set from range [A, B] upto L levels.
Ternary Cantor Set: A ternary Cantor set is a set built by removing the middle part of a line segment when divided into 3 parts and repeating this process with the remaining shorter segments. Below is an illustration of a cantor set.
Input: A = 0, B = 1, L = 2
Level 0: [0.000000] — [1.000000]
Level 1: [0.000000] — [0.333333] [0.666667] — [1.000000]
Level 2: [0.000000] — [0.111111] [0.222222] — [0.333333] [0.666667] — [0.777778] [0.888889] — [1.000000]
Explanation: For the given range [0, 1], in level 1, it is divided into three parts ([0, 0.33], [0.33, 0.67], [0.67, 1]). From the three parts, the middle part is ignored. This process is continued for every part in the subsequent executions.
Input: A = 0, B = 9, L = 3
Level_0: [0.000000] — [9.000000]
Level_1: [0.000000] — [3.000000] [6.000000] — [9.000000]
Level_2: [0.000000] — [1.000000] [2.000000] — [3.000000] [6.000000] — [7.000000] [8.000000] — [9.000000]
Level_3: [0.000000] — [0.333333] [0.666667] — [1.000000] [2.000000] — [2.333333] [2.666667] — [3.000000] [6.000000] — [6.333333] [6.666667] — [7.000000] [8.000000] — [8.333333] [8.666667] — [9.000000]
- Create a linked list data structure for each node of the Set, having the start value, end value and a pointer to the next node.
- Initialize the list with the start and end value given as the input.
- For the next level:
- Create a new node where the difference between the start and end values is of the initial, i.e. start value is less than the initial end value.
- Further, modify the original node, such that the end value is more of the initial start value.
- Place the pointer to the new node after the original one accordingly
Below is the implementation of the above approach:
Level_0 : [0.000000] — [9.000000]
Level_1 : [0.000000] — [3.000000] [6.000000] — [9.000000]
Level_2 : [0.000000] — [1.000000] [2.000000] — [3.000000] [6.000000] — [7.000000] [8.000000] — [9.000000]
References: Cantor Set Wikipedia
Related Article: N-th term of George Cantor set of rational numbers
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- N-th term of George Cantor set of rational numbers
- C++ | Nested Ternary Operator
- Ternary number system or Base 3 numbers
- Create a Doubly Linked List from a Ternary Tree
- Ternary operator vs Null coalescing operator in PHP
- Representation of a number in powers of other
- Maximum distance between two 1's in Binary representation of N
- Next greater number than N with exactly one bit different in binary representation of N
- Count numbers have all 1s together in binary representation
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Convert a given Decimal number to its BCD representation
- Memory representation of Binomial Heap
- Number of trailing zeroes in base B representation of N!
- Check if binary representation of a number is palindrome
- Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation)
- Decimal representation of given binary string is divisible by 10 or not
- Prime Number of Set Bits in Binary Representation | Set 1
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Prime Number of Set Bits in Binary Representation | Set 2
- Find the number obtained after concatenation of binary representation of M and N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.