帮助将值列表插入二叉树..?
好吧,我已经研究了一段时间......试图找出一种算法将我的随机数列表插入二叉树中。
这是我到目前为止所得到的:
NodePtr 和 Tree 是指向节点的指针,
NodePtr CreateTree(FILE * fpData)
{
int in;
fscanf(fpData, "%i", &in);
Tree T = (NodePtr)malloc(sizeof(Node));
T->Left = NULL;
T->Right = NULL;
T->value = in;
while((fscanf(fpData, "%i", &in)) != EOF)
{
InsertInTree(in, T);
printf("\n %p", T);
}
return T;
}
void InsertInTree(int value,Tree T)
{
if(T == NULL)
{
T->Left = (NodePtr)malloc(sizeof(Node));
T->Left->Left = NULL;
T->Left->Right = NULL;
T->Left->value = value;
printf("\n %i ", value);
return;
}
if(T->Left == NULL)
{
InsertInNull(value, T->Left);
}
else if(T->Right == NULL)
{
InsertInNull(value, T->Right);
}
else
{
if(T->Left->Left == NULL || T->Left->Right == NULL) InsertInTree(value, T->Left);
else InsertInTree(value, T->Right);
}
}
如果特定节点的两个子节点都不为空,我不知道该怎么做。我在这里所做的工作适用于少量数字(1、2、3、5、6),但如果列表较大,它就会变得不平衡和错误。
Well, I've been at it for a while...trying to figure out an algorithm to insert my list of random numbers into a binary tree.
This is what I have gotten so far:
NodePtr and Tree are pointers to a node
NodePtr CreateTree(FILE * fpData)
{
int in;
fscanf(fpData, "%i", &in);
Tree T = (NodePtr)malloc(sizeof(Node));
T->Left = NULL;
T->Right = NULL;
T->value = in;
while((fscanf(fpData, "%i", &in)) != EOF)
{
InsertInTree(in, T);
printf("\n %p", T);
}
return T;
}
void InsertInTree(int value,Tree T)
{
if(T == NULL)
{
T->Left = (NodePtr)malloc(sizeof(Node));
T->Left->Left = NULL;
T->Left->Right = NULL;
T->Left->value = value;
printf("\n %i ", value);
return;
}
if(T->Left == NULL)
{
InsertInNull(value, T->Left);
}
else if(T->Right == NULL)
{
InsertInNull(value, T->Right);
}
else
{
if(T->Left->Left == NULL || T->Left->Right == NULL) InsertInTree(value, T->Left);
else InsertInTree(value, T->Right);
}
}
I'm lost on what to do if the both children of a particular node are not null. What I did here works for a small amount of numbers (1,2,3,5,6) but if the list is larger it becomes unbalanced and wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是一个搜索树吗?我没有看到任何
if (value < T->Value)
条件。并且您有一个 InsertNull (未显示)。这应该不是必需的,1 个功能就足够了。
要解决您的主要问题,请使用指针到指针参数,或者更优雅的是,始终返回一个新树:
在 CreateTree 中:
Is it meant to be a search-tree? I don't see any
if (value < T->Value)
conditions.And you have an InsertNull (not shown). That shouldn't be necessary, 1 function should be enough.
To address your main problem, use a pointer-to-pointer parameter or, more elegant, always return a new Tree:
And in CreateTree:
由于分配不是针对排序树的,因此您可以以广度优先的方式填充它。这意味着插入的第一个始终是根,下一个是下一级的第一个节点,因此它看起来像这样:
这是一篇进一步解释它的文章:
http://www.cs.bu.edu/teaching/c/tree/breadth-first/
Since the assignment is not for a sorted tree, you can populate it in a breadth-first manner. This means the first thing inserted is always the root, the next is the first node at the next level so it looks like this:
Here is an article that explains it further:
http://www.cs.bu.edu/teaching/c/tree/breadth-first/
二叉树中的简单插入和保持二叉树平衡是不同的问题。我建议您从第一个问题开始,只关注保持树中订单属性的正确性。你离那并不远。
然后你应该看看红黑树的经典实现,这是经过充分研究的保持树平衡的有效方法,但有成本,它更复杂。
Simple insertion in a binary tree and keeping a binary tree balanced are different problems. I suggest you start with the first problem and just focus on keeping order properties correct within the tree. Your are not far from that.
Then you should have a look at classical implementations for red-black trees, well studied and efficient way of keeping trees balanced, but with a cost, it's more complex.