2-3-4树节点成员
2-3-4 trees node members
我真的希望能澄清一下2-3-4树…假设你有一个这样定义的树:
class N234{ //node class
public:
int firstData, secondData, thirdData;
N234 *firstChild,*secondChild,*thirdChild,*fourthChild,*parent;
};
class T234{ //tree with root node
public:
T234(){
this->root->parent=NULL;
this->root->firstChild=NULL;
this->root->secondChild=NULL;
this->root->thirdChild=NULL;
this->root->fourthChild=NULL;
}
private:
N234* root;
};
My question actually is how do i know whether a node is full (has all three values in it) when its variables(firstData, secondData, thirdData) have already some value in them as?
for example:
root: |4| left child of root:|1,2|
right child of root |7,9|
Here root has one value (4). My question is how do we know that it actually has one value, since all of his other variables(secondData, thirdData) have some value in them (even if it is trash).. Thanks in advance!
If we had only internal nodes to worry about, the simplest way would be to look at the child pointers:
If thirdChild
is null, then the node is a 2-node, so it has only one meaningful value (firstData
), and the rest (secondData
, thirdData
) contain junk.
If thirdChild
is not null, but fourthChild
is null, then it's a 3-node; the first two data variables contain meaningful values.
If all child pointers are non-null, then it's a 4-node.
The only problem with this approach is that it requires that a pointer be null iff it is actually invalid (e.g. the thirdChild
of a 2-node) and not just unused. But not all valid child pointers will be in use (e.g. the firstChild
of a 2-node that is a leaf).
So one solution is to create a node which all invalid pointers point to, and which is not used in any other way. In a sense, it acts as a second NULL, distinct from the ordinary one.
Note that it doesn't matter which kind of pointer (invalid, or valid-but-unused) points to NULL and which points to the phony node. (Also note that you could use any non-NULL value for the "second NULL", it wouldn't have to be the address of an actual node, but you'd have to be certain that it could never become the address of an actual node, and anyway it would make me nervous to use such an approach.)
Your need to add a "total number of keys" member variable to your node class
class N234{ //node class
public:
N234(int x) : totalItems(1) { keys[0] = x; children[0] = 0; }
N234(int x, int y) : totalItems(2) {keys[0] = x; keys[1] = y; children[0] = 0;}
N234(int x, int y, int z) : totalItems(3) { keys[0] = x; keys[1] = y;
keys[2] = z; children[0] = 0; }
bool isLeaf() { return child[0] == 0 ? true : false }
bool isFourNode() { return totalItems == 4 : true : false}
private:
totalItems;
int keys[3];
N234 *children[4];
};
然后检查是否为4节点
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 没有从"树节点"到"树节点*"的可行转换
- 对树节点使用 CString 时出现内存泄漏
- 预分配的节点向量中的无锁树节点分配
- 接受来自键盘的树节点以确定其高度
- C++ Eclipse:二叉搜索树节点>数据=变量似乎不起作用
- C++错误(从不兼容的类型"void"分配给树节点*)
- UWP:在树节点中将文本设置为粗体
- 树节点之间的最大距离中的运行时错误
- 想要以最新的添加节点完整返回树节点的根
- 从GCC功能树节点中检索函数参数
- 如何映射数组元素以选择二进制树节点
- Boost::p tree - 访问列表中包含的属性树节点
- 二进制搜索树节点插入
- 根据其子节点的余额计算 AVL 树节点余额
- 检查树节点等于值
- 创建一个C++预排序迭代器,为循环提供树节点
- 递归后序遍历解除分配二叉树节点
- 创建模板树节点列表时出错
- 2-3-4树节点成员