Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Cartesian Tree
Cartesian Tree in C++
A Cartesian tree is a binary tree derived from a sequence of distinct numbers. To construct a Cartesian tree, set its root to be the minimum number in the sequence, and then recursively construct its left and right subtrees from the subsequence before and after this number.
A Cartesian tree is a tree data structure that obeys the following structural invariants:
- The tree follows the min (or max) heap property - each node is less than or greater than its children.
- An inorder traversal of the nodes causes the values in the same order in which they arise in the initial series.
Let's construct a max-heap Cartesian Tree of the given data: {10, 15, 30, 25, 20}
Let's also construct the min-heap Cartesian Tree of the above data:
The Cartesian tree is not height-balanced. The Cartesian tree of a sequence of distinct numbers is always unique.
Example to Construct a Cartesian Tree
In the following example, we implement a program to construct a Cartesian tree from a given array, which takes O(n) time:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct nod {
int d;
struct nod * l;
struct nod * r;
};
class CTree {
public: //declare the functions
nod * newNode(int);
int min(int[], int, int);
nod * buildTree(int[], int, int);
void inorder(nod * node);
void show(nod * , int);
CTree() {}
};
int CTree::min(int arr[], int s, int e) {
int i, min = arr[s], minind = s;
for (i = s + 1; i <= e; i++) {
if (arr[i] < min) {
min = arr[i];
minind = i;
}
}
return minind;
}
//build the cratesian tree
nod * CTree::buildTree(int inorder[], int s, int e) {
if (s > e)
return NULL;
int i = min(inorder, s, e);
nod * r = newNode(inorder[i]);
if (s == e)
return r;
//call the function recursively for left child
r -> l = buildTree(inorder, s, i - 1);
//call the function recursively for right child
r -> r = buildTree(inorder, i + 1, e);
return r;
}
void CTree::inorder(struct nod * node) {
if (node == NULL)
return;
inorder(node -> l);
cout << node -> d << " ";
inorder(node -> r);
}
//show the tree
void CTree::show(nod * ptr, int level) {
int i;
if (ptr == NULL)
return;
if (ptr != NULL) {
show(ptr -> r, level + 1);
cout << endl;
for (i = 0; i < level; i++)
cout << " ";
cout << ptr -> d;
show(ptr -> l, level + 1);
}
}
//creation of new node
nod * CTree::newNode(int d) {
nod * t = new nod;
t -> d = d;
t -> l = NULL;
t -> r = NULL;
return t;
}
int main() {
CTree ct;
int arr[] = {
10,
15,
30,
25,
20
};
int n = sizeof(arr) / sizeof(arr[0]);
nod * r = ct.buildTree(arr, 0, n - 1);
cout << "Cartesian tree Structure: ";
ct.show(r, 1);
cout << endl;
cout << "\nInorder traversal of the tree" << endl;
ct.inorder(r);
cout << endl;
return 0;
}
Following is the Cartesian tree of the above data -
Cartesian tree Structure:
20
25
30
15
10
Inorder traversal of the tree
10 15 30 25 20
Advertisements