red-black-tree 相关问题

红黑树是一种自平衡二叉搜索树,是计算科学中使用的数据结构,通常用于实现关联数组。

红黑树 C++

这是一个基本的 RED-BLACK Tree 程序,除了插入和删除之外,它的大部分工作都很好。 #包括 #包括 使用命名空间标准; 模板 这是一个基本的 RED-BLACK Tree 程序,除了插入和删除之外,它的大部分工作都很好。 #include <iostream> #include <cstdlib> using namespace std; template <typename keytype, typename valuetype> //one lesson try to fit for of the functions from the private so that the access can be easier class RBTree{ private: struct Node{ bool color; //color is true for red and false for black int size; Node *left, *right, *parent; keytype key; valuetype val; }; Node* root; Node* newNode(keytype k, valuetype v, bool color = true){ //usually starts with the root and then the leaves can be placed to the left and right Node *newRBNode = new Node; newRBNode->color = color; newRBNode->key = k; newRBNode->left = NULL; newRBNode->right = NULL; newRBNode->parent = NULL; newRBNode->size = 1; //size 1 as it is 1 leaf initially newRBNode->val = v; return newRBNode; } void clearMemory(Node *current) { if (current == NULL){ //uses recursion to go through the tree and delete the leaves return; } clearMemory(current->left); if (current->left != NULL){ free(current->left); } clearMemory(current->right); if (current->right != NULL){ free(current->right); } } int size(Node* current){ if (current == NULL){ return 0; } return current->size; } Node* leftRotate(Node* current){ Node* y = current->right; current->right = y->left; y->left = current; y->color = y->left->color; y->left->color = true; current->size = size(current->left) + size(current->right) + 1; y->size = size(y->left) + size(y->right) + 1; return y; } Node* rightRotate(Node* current){ //conventional rotation from textbook and updates the size Node* y = current->left; current->left = y->right; y->right = current; y->color = y->right->color; y->right->color = true; current->size = size(current->left) + size(current->right) + 1; y->size = size(y->left) + size(y->right) + 1; return y; } Node* copy(Node* current){ //uses recursion to copy Node* node = newNode(current->key, current->val, current->color); if (current->left != NULL){ node->left = copy(current->left); } if(current->right != NULL){ node->right = copy(current->right); } node->size = current->size; return node; } void colorFlip(Node* current){ current->color = !(current->color); //flips the color of the parent and the children (usually one red and two blacks) current->left->color = !(current->left->color); current->right->color = !(current->right->color); } bool isRed(Node* current){ //needs fixing if (current == NULL){ return false; } return (current->color); } Node* Insertion(Node* current, keytype key, valuetype val){ //Messed up if (current == NULL){ Node* test = newNode(key, val); return test; } if (isRed(current->left) && isRed(current->right)){ colorFlip(current); } if (key < current->key){ current->left = Insertion(current->left, key, val); } else if (key > current->key){ current->right = Insertion(current->right, key, val); } else if (current->key == key){ current->val = val; } if (isRed(current->right)){ //if right node is red before the introduced rode then rotation happens current = leftRotate(current); } if (isRed(current->left) && isRed(current->left->left)){ //conflict happens and the rotation starts current = rightRotate(current); } if(isRed(current->right)){ //fixes up the whole tree after deletion current = leftRotate(current); } if (isRed(current->left) && isRed(current->left->left)){ current = rightRotate(current); } if (isRed(current->left) && isRed(current->right)){ colorFlip(current); } current->size = size(current->left) + size(current->right) + 1; return current; } Node* Deletion(Node* current, keytype key){ //Gotta fix it if (key < current->key){ //uses the textboob delete fixation function if (!isRed(current->left) && !isRed(current->left->left)){ colorFlip(current); if (isRed(current->right->left)){ current->right = rightRotate(current->right); current = leftRotate(current); colorFlip(current); } } current->left = Deletion(current->left, key); } else { if (isRed(current->left)){ current = rightRotate(current); } if ((key == current->key) && (current->right == NULL)){ return NULL; } if(!isRed(current->right) && !isRed(current->right->left)){ colorFlip(current); if (isRed(current->left->left)){ current = rightRotate(current); colorFlip(current); } } if (current->key == key){ Node* y = current->right; while(y->left != NULL){ y = y->left; } current->key = y->key; current->val = y->val; current->right = Deletion(current->right, current->key); } else{ current->right = Deletion(current->right, key); } } if(isRed(current->right)){ //fixes up the whole tree after deletion current = leftRotate(current); } if (isRed(current->left) && isRed(current->left->left)){ current = rightRotate(current); } if (isRed(current->left) && isRed(current->right)){ colorFlip(current); } current->size = size(current->left) + size(current->right) + 1; return current; } valuetype* findVal(Node* current, keytype key){ if (current == NULL){ return NULL; } else if (current->key == key){ return &(current->val); } else if(current->key > key){ return findVal(current->left, key); } else if(current->key < key){ return findVal(current->right, key); } return NULL; } keytype Position(Node* current, int sample){ int pos = size(current->left) + 1; if (pos == sample){ return current->key; } else if(sample < pos){ return Position(current->left, sample); } else{ return Position(current->right, sample-pos); } } int Rank(Node* current, keytype key){ if(current == NULL){ return 0; } else if(current->key > key){ return Rank(current->left, key); } else if(current->key < key){ return (size(current->left) + 1 + Rank(current->right, key)); } else if(current->key == key){ return (current->size - size(current->right)); } else{ return 0; } } keytype* find_sucessor(Node* current, keytype K){ Node* successor = NULL; while(current){ if (current->key > K){ successor = current; current = current->left; } else{ current = current->right; } } return &(successor->key); } keytype* find_predecessor(Node* current, keytype K){ Node* predecessor = NULL; while(current){ if (current->key >= K){ current = current->left; } else{ predecessor = current; current = current->right; } } return &(predecessor->key); } void print_preorder(Node* current){ if (current == NULL){ return; } cout << current->key << " "; print_preorder(current->left); print_preorder(current->right); } void print_postorder(Node* current){ if (current == NULL){ return; } print_postorder(current->left); print_postorder(current->right); cout << current->key << " "; } void print_inorder(Node* current){ if (current == NULL){ return; } print_inorder(current->left); cout << current->key << " "; print_inorder(current->right); } public: RBTree(){ root = NULL; } RBTree(keytype k[], valuetype v[], int S){ //black children all of them root = NULL; //wrong gsfsdfsdf int middle = (S-1)/2; // for (int i = middle; i > 0; i--){ //insert from half till zero then half to full // root = Insertion(root, k[i], v[i]); // root->color = false; // } // for (int i = 0; i < S; i++){ // root = Insertion(root, k[i], v[i]); // root->color = false; //false is black and true is red // } root = Insertion(root, k[middle], v[middle]); root->color = false; for (int i = 1; i <= middle; i++){ root = Insertion(root, k[middle-i], v[middle-i]); root->color = false; root = Insertion(root, k[middle+i], v[middle+i]); root->color = false; } if (((2*middle)) != (S-1)){ root = Insertion(root, k[S-1], v[S-1]); root->color = false; } } RBTree(const RBTree<keytype, valuetype> &A){ root = copy(A.root); } RBTree<keytype, valuetype>& operator=(const RBTree<keytype, valuetype> &A){ root = copy(A.root); return *this; } ~RBTree(){ clearMemory(root); free(root); } valuetype* search(keytype k){ return findVal(root,k); } void insert(keytype k, valuetype v){ root = Insertion(root, k, v); root->color = false; } int remove(keytype k){ if (search(k) == NULL){ return 0; } int node = size(root); root = Deletion(root, k); return (node - size(root)); } int rank(keytype k){ return Rank(root,k); } keytype select(int pos){ return Position(root, pos); } keytype* successor(keytype k){ return find_sucessor(root, k); } keytype* predecessor(keytype k){ return find_predecessor(root, k); } int size(){ return size(root); } void preorder(){ print_preorder(root); cout << endl; } void inorder(){ print_inorder(root); cout << endl; } void postorder(){ print_postorder(root); cout << endl; } }; 当我尝试插入:A"、"B"、"C"、"D"、"E"、"F"、"G"、"H"、"I"、"K"时,我得到了顺序正确。但后序和前序不正确。我该怎么做才能正确输出其中的 3 个视图?非常感谢您的帮助。

回答 0 投票 0

调试平衡二叉搜索树的过程

我已经实现了一个红黑搜索树,目前正在研究以下功能: 目前正在使用以下功能: 向树中添加新节点 打印树: -- 对于...

回答 0 投票 0

算法导论中提到的红黑删除算法的问题,CLRS第三版。

RB-DELETE(T, z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-TRANSPLANT(T, z, z.right) 6 elif z.right == T.nil 7 x = z.left 8 RB-TRANSPLANT(T, z, ...

回答 1 投票 0

确定红黑树中的节点颜色

我正在阅读有关红黑树的文章,并且我知道节点可以是红色或黑色。规则说:1)每个节点都有一种颜色-红色或黑色。 2)树的根总是黑色的。 3)...

回答 1 投票 1

C中的分配节点内存(RED-BLACK树)

我正在尝试使用带有(void *)数据的节点结构(节点保存的数据是generalint,字符串,struct Vector等)来构建带有节点结构的RED-BLACK树。我能够删除节点完全向上...

回答 1 投票 0

每个节点多少位

[这个问题来自算法4的练习。我在这里粘贴如下:3.3.19每个节点1位的颜色,我们可以表示2、3和4节点。每个节点需要多少位...

回答 1 投票 -1

红黑树插入操作不断崩溃

else if(par = par->parent->left) par->parent->left = child;

回答 2 投票 0

为什么只有当插入的节点的叔叔是黑色时才旋转红黑树?有人可以解释其属性背后的逻辑吗?

所以最近我一直在分析Red black tree及其属性,并试图解释为什么它们如此,我理解这些约束用于使树保持平衡,并且...

回答 1 投票 0

Red Black Tree Height Proof

我正在阅读此算法书,并且在“红黑树”部分中指出了以下引理:具有n个内部节点的红黑树的高度最多为2 lg(n + 1)。 ...

回答 1 投票 0

RB树不适用于*某些*未排序的数组

if(node.parent.parent.parent.right == node.parent.parent):tnode = self.LR(node.parent.parent)node.parent.parent.parent.right = tnode这是...

回答 1 投票 0

为什么RBT总是将零个节点作为其叶节点?

我不知道为什么我们需要NIL节点作为RBT中的叶节点。谁能解释一下?预先谢谢。

回答 1 投票 0

什么是执行以下查询的最佳数据结构,为什么我的查询结果会出错

我需要构建一个数据结构,以:学习x(插入x)忘记x(删除x)。如果x不存在,则不执行任何操作将x n减少-将x的计数减少n,如果n> = count,则该节点就是...

回答 1 投票 1

对于相同的输入,红黑树的每个实现都具有相同数量的红色节点吗?

详细说明,对于相同的输入,是否可以有实现不同的红黑树的实现?

回答 1 投票 0

在OCaml中具有类的红黑树

有人知道如何在OCaml中使用类来实现红黑树吗?至少是类属性和初始化程序?我是OCaml的新手。我试过的:键入data = {key:int;值:字符串} ...

回答 1 投票 0

Red Black Tree vs Trie Tree拼写检查器

我正在学习特里树,我相信特里树的常见用法是将其用于拼写检查器。拼写检查器是否总是更好?在什么情况下(如果有),您会选择...

回答 1 投票 0

点号:确定左右两个孩子

我想在点(GraphViz)中制作一棵红黑树。但是我总是在节点放置方面遇到问题。到目前为止,这是我的代码(由于放置问题而未完成):digraph ...

回答 2 投票 0

红黑树〜1个孩子被删除

红色的父节点是否可能只有一个黑色的子节点?我一直在网上玩Red / Black Tree模拟器,但无法设法解决这种情况。 ...

回答 2 投票 4

红黑树中C的简单结构定义

我多年来没有使用C语言,现在我再次需要它。我正在尝试构建一棵红黑树,但由于一开始缺少关于“结构”的信息,所以我一开始陷入困境。看看我的“ ...

回答 1 投票 0

红黑树旋转:当我有y = x.right时; x.right = y.left。将y.left.p = x写成x.right.p = x

在CLRS中,作者通过以下伪代码在红黑树中介绍了旋转操作:LEFT-ROTATE(T,x)y = x.right#第1行x.right = y.left#如果y.left是第2行≠T ....

回答 1 投票 6

在函数定义/实现的平均值(c ++)的返回值范围内,私有是什么?

所以,我正在查看与我在学校上班的一个项目有关的代码,我发现一个函数实现在返回值之前具有私有功能,我希望有人可以...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.