一、简介
笛卡尔树是一种特定的二叉树数据结构,由数组存储。在范围最值、范围top k查询方面广泛应用。
笛卡尔树的性质:
- 树中的元素满足二叉搜索树性质,要求按照中序遍历得到的序列为原数组序列
- 树中节点满足堆性质,节点的key值要大于其左右子节点的key值
笛卡尔树中每个节点都有两个权值 (ki,wi) ,其中 ki 满足二叉搜索树, wi 满足二叉堆。
当按照index从1到n(或者从0到n-1)的顺序将数组中的每个元素插入到笛卡尔树中时,当前要被插入的元素的index值最大,因此根据二叉搜索的性质需要沿着当前已经完成的笛卡尔树的根的右子树链搜索。
由于笛卡尔树要满足堆的性质(以最大堆为例),父节点的key值要大于子节点的key值,所以沿着树根的右子树链往下走,直到搜索到的节点的key值小于等于当前要插入节点的key值。
此时,便找到了当前结点需要插入的位置,记为P。此时P下方的节点的key值肯定小于当前被插入节点的key,但是index也小于当前插入节点的index(即需要在二叉搜索树中当前结点之前的位置),所以将当前节点插入到P的位置,同时将以P为根的子树挂载到新插入的节点的左子树
图1讲解了笛卡尔树的存储方式。

二、代码实现
struct DTreeNode{
int index;//在原来数组中的索引
int key;//节点的key,即原数组中 Array[index]值
DTreeNode* left;//左子节点
DTreeNode* right;//右子节点
DTreeNode* parent;//父节点
DTreeNode(int i,int k):
index(i),key(k),left(NULL),right(NULL),parent(NULL){};
};
DTreeNode* BuildTree(int* arr,int n)
{
stack<DTreeNode*>node_stack;
DTreeNode* node=NULL,*new_node;
for(int i=0; i<n; i++)
{
new_node=new DTreeNode(i,arr[i]);
while(!node_stack.empty())
{
node=node_stack.top();
if(node->key>new_node->key) //直到栈顶的元素的key大于当前结点的key
{
if(node->right)//将原来的右子链挂载到new_node的左子树
{
node->right->parent=new_node;
new_node->left=node->right;
}
node->right=new_node;//将新插入的节点插入,作为右链的最后
new_node->parent=node;
break;
}
node_stack.pop();
}
node_stack.push(new_node);
}
//找出栈顶元素,就是笛卡尔树的根
while(!node_stack.empty())
{
node=node_stack.top();
node_stack.pop();
}
return node;
}
void InorderTravel(DTreeNode* root)//非递归进行中序遍历
{
stack<DTreeNode*>node_stack;
DTreeNode* node=root;
while(!node_stack.empty()||node)
{
if(node)
{
node_stack.push(node);
node=node->left;
}
else
{
node=node_stack.top();
node_stack.pop();
cout<<"travel node, index = "<<node->index<<", value = "<<node->key<<endl;
node=node->right;
}
}
}
参考文献:笛卡尔树 - 走看看笛卡尔树是一种同时满足二叉搜索树和堆的性质的数据结构。 可在一个数组上构造出来(时间复杂度可以达到O(n))。树中节点有几个属性, key(节点元素的大小)、index(节点在原数组中的索引)、lefhttp://t.zoukankan.com/gtarcoder-p-4702853.html 笛卡尔树 —— 二叉搜索树和堆的结合 - 知乎笛卡尔树是一种利用 单调栈创建的数据结构,用于维护区间最值与区间的关系。我们来回想下二叉搜索树的性质: 对于每个节点,满足左小右大。 对二叉搜索树进行中序遍历,可以获得排序后的序列。 二叉堆的性质: 上…
https://zhuanlan.zhihu.com/p/493897833
以上就是本文的全部内容啦!感谢阅读!