查找链接结构堆中的最后一个元素

发布于 2024-08-14 05:33:37 字数 189 浏览 4 评论 0原文

我想知道如何在堆和根元素的链接结构实现中找到最远的元素。我希望能够对元素进行 Enque 和 Deque。

一些澄清: 我的意思是,假设您有一个构成最大堆的链接结构(根元素具有最大值)。您的树在最底部的某个位置会有一个元素,您可以在该元素之后插入或删除该元素,具体取决于您是入队还是出队。你如何确定该元素?以及如何确定树的根节点? (最上面那一张)

I was wondering how you would go about finding the furthest element in a linked structure implementation of a heap and the root element. I want to be able to Enque and Deque elements.

Some clarification:
what I meant was lets say you have a linked structure making up a max heap (root element has the largest value). Your tree would have an element somewhere at the very bottom which you would insert after or remove depending on if you are enqueing or dequeing. How do you determine that element? and how do you determine the root node of the tree? (the top one)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘸点软妹酱 2024-08-21 05:33:37

我并不完全肯定这就是您所要求的,但这是指向单链表的最后一个元素的一种方法:

T *p = NULL, *q = NULL;  // q follows p by one node
p = root;
while (p)
{
    q = p;
    p = p -> next;
}

// q now points to last node

I'm not completely positive this is what you are asking for, but this is one way of pointing to the last element of a singly-linked list:

T *p = NULL, *q = NULL;  // q follows p by one node
p = root;
while (p)
{
    q = p;
    p = p -> next;
}

// q now points to last node
愿得七秒忆 2024-08-21 05:33:37

将某些内容添加到堆结构的常规方法是从根开始并“遍历”树以找到新元素所在的位置。当你找到它的去向时,你就把它放在那里,如果这意味着替换该位置已有的内容,那么你就采用以前的值并继续向下查找它的去向。重复直到碰到一片叶子,然后添加您“携带”的任何值作为该叶子的适当子元素。

假设你的新值为 5,堆的根节点保存着 10。5 显然低于 10,所以你查看 10 的子节点。假设它们是 8 和 7。两者都大于 5,所以选择一个 (你如何选择一个取决于你是否试图保持堆平衡)。假设您选择 7,它有子节点 3 和 4。5 低于 7 但高于 3 或 4,因此您将 4 替换为 5,然后查看该节点的子节点以了解将 4 放在哪里。假设没有孩子,你可以添加一个包含 4 的新叶子。

至于你如何找到根的问题——通常根是你保留的指向树的唯一指针。这是您所有操作的起点。如果您从其他地方开始,我想您可以导航到根目录,但我会质疑为什么。

The normal method for adding something to a heap structure is to start at the root and "walk" the tree to find the place where the new element goes. When you find where it goes, you put it there, and if that means replacing what's already in that spot, then you take the previous value and keep walking down to find where it goes. Repeat until you hit a leaf, then add whatever value you're "carrying" as the appropriate child of that leaf.

Suppose your new value was 5 and the root node of the heap held a 10. 5 clearly goes below 10, so you look at the children of 10. Suppose they're 8 and 7. Both are larger than 5, so pick one (how you pick one depends on whether you're trying to keep the heap balanced). Suppose you pick 7, and it has children 3 and 4. 5 goes below 7 but above 3 or 4, so you replace, say, the 4 with 5, then look at that node's children to see where to put the 4. Suppose it has no children, you can just add a new leaf containing 4.

As for your question about how you find the root -- generally the root is the only pointer to the tree that you keep. It's your starting point for all operations. If you started somewhere else, I suppose you could navigate up to the root, but I'd question why.

时光倒影 2024-08-21 05:33:37

也许是这样的?

struct node {
  node *parent;
  node *children[2];
  int data; //or whatever else you want
};

struct heap {
  node *root;
  node *last;
};

不过,这比仅使用数组来实现要困难得多。这是一个假设的添加

void add(struct heap* h, int d)
{
  node* add = malloc(sizeof(node));
  add->data = d;
  node* current = h->root;
  while(current->children[1]) current = current->children[1];
  current->children[1] = add;
  add.parent = current;
  add.children[0] = NULL;
  add.children[1] = NULL; 
  h.last = add;
  percolate_up(h);
}

类似的东西。

Maybe something like this ?

struct node {
  node *parent;
  node *children[2];
  int data; //or whatever else you want
};

struct heap {
  node *root;
  node *last;
};

This is a lot trickier to implement then just using an array though. Here is a hypothetical add

void add(struct heap* h, int d)
{
  node* add = malloc(sizeof(node));
  add->data = d;
  node* current = h->root;
  while(current->children[1]) current = current->children[1];
  current->children[1] = add;
  add.parent = current;
  add.children[0] = NULL;
  add.children[1] = NULL; 
  h.last = add;
  percolate_up(h);
}

Something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文