【问题标题】:Why is my Binary Search Tree void remove function not working properly?为什么我的二叉搜索树无效删除功能无法正常工作?
【发布时间】:2020-11-20 16:08:53
【问题描述】:

我无法让我的二叉搜索树删除功能正常工作。无论我做什么,它实际上都不想正常工作。当我试图让它正常运行时,它似乎总是做一些最奇怪的事情。

node* 结构和 root_ 一样位于另一个头文件中(它们在正常的右、左和数据存储配置中设置)

void remove(int value){
   node* n = root_;
   node* nClone = nullptr; 

    while (n != nullptr) {//constant checker to ensure that a 
        if (value > n->value_) { // if value is larger than value stored within node n it will descend further down right
        
            nClone = n; //stores n before continueing
            n = n->rhs_;
        
        } else if (value < n->value_) { // if value is less than value stored within node n it will descend further down left
        
            nClone = n; //stores n before continueing
            n = n->lhs_;
        
        } else { //if it is equal to the value (there are no other possible outcomes so an else would work) check if there are any subsiquent leaves attached

            if (n->lhs_ == nullptr && n->rhs_ == nullptr) { //if both left and right are empty (I.E. no leaves attached to node) set n to nullptr and then delete using free();
            
                nClone->lhs_ = nullptr; //stores both left
                nClone->rhs_ = nullptr; // and right leaves as nullptr
                
                free(n); //frees n

                n = nullptr;

                count_--;//decreases count_/size counter by 1
                return; //exits from function as there is nothing more to do
            
            } else if (n->lhs_ == nullptr || n->rhs_ == nullptr) { //if n has one connection whether it be on the left or right it stores itself in nClone and then deletes n

                if (n->lhs_ != nullptr) { //if statement to check if left leaf of n exists
                
                    nClone->lhs_ = n->lhs_; //if it does it stores n's left leaf in nClone 
                                    
                } else { //if it doesnt have anything stored in the left then there garuntteed is one on the right

                    nClone->rhs_ = n->rhs_; //stores n's right leaf in nClone

                }
                
                free(n);
                count_--; //decreases count_/size counter by 1
                return; //exits from function as there is nothing more to do

            } else {
                //for preorder succession
                node* nSuc = n->rhs_; //stores right leaf of n in nSuc

                while (nSuc->lhs_ != nullptr) { //look for successor
                    nSuc = nSuc->lhs_;
                
                }

                n->value_ = nSuc->value_;
                free(n);
                count_--;
                return;

            
            }
                    
        }    

    }

}

【问题讨论】:

  • 此问题显示的代码不符合 stackoverflow.com 对 minimal reproducible example 的要求。这意味着这里的任何人都不太可能最终回答这个问题。但最多只能猜测。你应该edit你的问题来展示一个最小的例子,不超过一两页代码(“最小”部分),其他人都可以剪切/粘贴、编译、运行和重现所描述的问题(“ reproducible”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
  • C++ 代码中的 free() 总是看起来很可疑。你是用new还是malloc分配新节点?
  • 我用new分配新节点
  • 我并不是说这会解决你的问题,但这肯定是个问题。使用new分配的内存必须使用delete释放,使用new[]获得的内存必须使用delete[]释放,使用malloc()获得的内存必须使用free()释放。所有其他组合都是未定义的行为。
  • 找到要删除的节点后,nClone-&gt;lhs_ 和 nClone-&gt;rhs_ 的大部分分配都是错误的。被删除的n 可以来自父节点的任一节点。在纸上画出来可能会帮助您发现问题。

标签: c++ binary-search-tree


【解决方案1】:

您的代码就像 C 一样编写。C++ 是一种不同的语言,您可以而且应该利用它来发挥自己的优势。

下面的文字中穿插着complete, compileable example,您可以在线尝试:) 这当然不是实现树的唯一方法,我不得不掩盖至少要实现它所需的各种细节一个功能齐全的解决方案。 “现实生活”的实现可能要复杂得多,因为“教科书”方法通常不能很好地与现代 CPU 上的缓存层次结构混合,并且与最先进的实现相比,这样的树会相当慢.但我认为,它确实有助于弥合普遍的“类 C”思维方式与现代 C++ 带来的东西之间的差距。

再说一遍:这个例子是最小的,它没有做很多通常需要做的事情,但它至少指向了远离 C 和 C++ 的方向,这就是我打算。

首先,让我们有一个Node 类型,它使用子节点的拥有指针。这些指针自动管理内存并从根本上防止您犯下会泄漏内存或允许使用悬空指针的错误。术语“拥有指针”意味着总是有一个明确定义的所有者:它是指针本身。例如,这些指针不能被复制——从那时起,同一个对象将有两个所有者,为此您需要共享所有权。但是共享所有权很难做到正确,因为必须有一些协议来确保你不会得到循环引用。在构建树时,“父”节点是“子”节点的自然所有者,因此唯一所有权正是我们所需要的。

// complete example begins
#include <cassert>
#include <memory>

using Value = int;

struct Node {
  std::unique_ptr<Node> lhs_;
  std::unique_ptr<Node> rhs_;
  Value value_;
  explicit Node(int value) : value_(value) {}
};
// cont'd.

我们还应该有一个拥有根节点的Tree,并保持节点数:

// cont'd.
struct Tree {
  std::unique_ptr<Node> root_;
  int count_ = 0;
};
// cont'd.

在对此类数据结构进行操作时,您经常希望不仅可以访问节点指针的 值,还希望访问 指针本身,以便您可以修改它。因此,我们需要某种“节点引用”,它的行为主要类似于 Node*,但在内部,它还携带指向节点的 指针 的地址,例如节点可以是replaced:

// cont'd.
class NodeRef {
  std::unique_ptr<Node> *owner;
public:
  NodeRef() = delete;
  NodeRef(std::unique_ptr<Node> &o) : owner(&o) {}
  Node *get() const { return owner->get(); }
  // Use -> or * to access the underlying node
  Node *operator->() const { return get(); }
  Node &operator*() const { return *get(); }
  // In boolean contexts, it's true if the Node exists
  explicit operator bool() const { return bool(*owner); }
  // Replace the Node (if any) with some other one
  void replace(std::unique_ptr<Node> &&oldNode) {
    *owner = std::move(oldNode);
  }
  NodeRef &operator=(std::unique_ptr<Node> &val) {
    owner = &val;
    return *this;
  }
};
// cont'd.

NodeRef 持有指向节点所有者的指针(所有者是拥有指针类型std::unique_ptr)。

以下是您可以将NodeRef 当作Node* 使用的方式:

NodeRef node = ...;
node->value_       // access to pointed-to Node using ->
(*node).value      // access to pointed-to Node using *
if (node) ...      // null check
node = otherNode;  // assignment from another node (whether owner or NodeRef)

NodeRef 的行为方式类似于std::unique_ptr&lt;Node&gt; &amp;,即类似于对节点所有者的引用,允许您更改所有权:

Tree tree;
NodeRef root = tree.root_; // reference the root of the tree
root.replace(std::make_unique<Node>(2)); // replace the root with a new node

请注意,由于std::unique_ptr 和移动语义的强大功能,这段代码执行了所有必要的内存分配和释放。 没有 new、delete、malloc 也没有 free 声明。而且,性能与手动分配相当——这段代码不使用任何类型的垃圾收集或引用计数。 std::unique_ptr 是一种工具,可让您利用编译器以保证正确的方式为您编写内存分配和释放代码。

但是,NodeRef 不是一个“观察”指针,即如果它指向的节点的 所有者 突然消失,那么 NodeRef 就会悬空。否则会产生更多开销,并且需要使用一些跟踪指针,例如shared_ptr 和 weak_ptr,或者定制的解决方案 - 当然超出了这里的范围。

因此NodeRef 满足了使实际树管理代码更易于编写、理解和维护的典型要求,并减少了出错的可能性。这种方法有助于设计正确的代码,即导致未定义行为的错误大多被编译器捕获或无法编写。

让我们看看使用我们上面介绍的类型进行二分节点搜索的外观:

// cont'd
// Finds the owner of a node that contains a given value,
// or the insertion point where the value would be
NodeRef find(Tree &tree, const Value &value)
{
  NodeRef node = tree.root_;
  while (node) {
    if (value < node->value_)
      node = node->lhs_;
    else if (node->value_ < value)
      node = node->rhs_;
    else
      break; // we found the value we need
  }
  return node;
}
// cont'd

首先,请注意,虽然返回的节点引用可以为空,但这并不意味着它“无用”。 NodeRef 永远不会“完全”为空,并且必须始终引用某个节点所有者 - 这就是删除默认构造函数的原因,因此您不能错误地创建无效的 NodeRef。 node 可以为 null,而不是对指向该节点的拥有指针的底层引用。

请注意代码与使用Node * 的版本有多么相似,但它更强大。因为这个版本的find返回一个NodeRef,我们可以使用这个引用来替换节点(或者如果它为空,则第一次设置它),而签名Node *find(Node *root, const Value &amp;value)只会让我们访问节点自己,而不是它的主人。而且,如果没有找到该节点,它将返回一个空指针 - 也不会让我们更接近知道在哪里插入新节点,并且放弃为找到这样的插入点所做的工作( !)。

NodeRef 为我们提供了对父节点的谨慎访问:它不会暴露整个父节点,而只是拥有给定节点的拥有指针——而且它也比“父”节点更通用,因为拥有指针甚至不需要被 Node 类型持有。事实上,当节点的所有者在 Tree 类中时,NodeRef 工作得很好,或者它也可以引用一个独立的指针:

std::unique_ptr<Node> myNode;
NodeRef node = myNode;

// The two lines below are equivalent - both change the `myNode` owning pointer
node.replace(std::make_unique<Node>(42));
myNode = std::make_unique<Node>(42);

原则上,可能存在NodeRef &amp;NodeRef::operator=(std::unique_ptr&lt;Node&gt; &amp;&amp;),即移动分配节点本身的一种方式,但这会隐藏一个重要的事实,即NodeRef 并不真正拥有该节点,而只是指某个所有者,而replace 方法使这一点更加明确:我们正在替换所有者持有的节点。

现在我们可以实现您想要的功能:节点移除。此函数接受NodeRef,并修改该节点根部的子树,从而删除原始节点:

// cont'd
// Removes the given node. Returns true if the node was removed, or false if
// there was nothing to remove
bool remove(NodeRef node)
{
  for (;;) {
    if (!node) return false; // the node is empty, nothing to do
  
    if (!node->lhs_) {
      // replace the node with its sole right child, if any
      node.replace(std::move(node->rhs_));
      return true;
    }
    else if (!node->rhs_) {
      // replace the node with its sole left child, if any
      node.replace(std::move(node->lhs_));
      return true;
    }
    else {
      // node has two children
      // 1. take on the largest value in the left subtree
      // oldValue is a *reference* to the value of the node being replaced
      Value &oldValue = node->value_;
      node = node->lhs_;
      while (node->rhs_) node = node->rhs_;
      // we found the node with a replacement value - substitute it for
      // the old value
      oldValue = std::move(node->value_);

      // 2. remove that child - continue the removal loop
      continue;
      // instead of continue, we could also do
      // remove(node);
      // return;
      // but by continuing we don't have recursion, and we levarage
      // the fact that the `node` references the correct node to remove
    }
  }
}
// cont'd

我们std::move 的值 - 这在处理像整数这样的“简单”值类型时根本不重要,但如果 Value 是一种只能移动但不能移动的类型,这将很重要复制,例如using Value = std::unique_ptr&lt;SomeType&gt;;.

现在是在Tree 中管理节点移除的助手:

// cont'd
void remove(Tree &tree, const Value& value)
{
  auto node = find(tree, value);
  if (remove(node))
    -- tree.count_;
}
// cont'd

我们可以用int value 代替const Value &amp;value,但这样它是一种更通用的方法,可以与其他Value 类型一起使用。

节点插入也相当容易,因为find 已经提供了值所在的插入点,如果它存在的话:

// cont'd
bool insert(Tree &tree, const Value& value)
{
  auto node = find(tree, value);
  if (node) {
    // Such a value already exists
    assert(node->value_ == value);
    return false;
  } else {
    // Insert new value
    node.replace(std::make_unique<Node>(value));
    ++ tree.count_;
    return true;
  }
}
// cont'd

如果Value 是不可复制的类型,那么我们需要一个接受右值引用的insert 签名,即bool insert(Tree &amp;tree, Value &amp;&amp;value)。

现在您可能会问:我们将如何“行走”这棵树?在 C++ 中,处理项目集合的惯用方式是通过迭代器,然后可以使用所谓的 range-for。以下示例打印出向量的元素:

std::vector<int> values{1,2,3,4,5};
for (int val : values)
  std::cout << val << "\n";

在迭代或“遍历”树时,我们需要一些“面包屑”留下来,这样我们才能找到回到树上的路。那些需要引用节点,以及节点是否被访问或遍历:

// cont'd
#include <functional>
#include <stack>
#include <vector>

// An entry in the node stack used to iterate ("walk") the tree
struct BreadCrumb {
  NodeRef node;
  bool visited = false; // was this node visited?
  bool traversedLeft = false; // was the left child descended into?
  bool traversedRight = false; // was the right child descended into?
  BreadCrumb(std::unique_ptr<Node> &owner) : node(owner) {}
  BreadCrumb(NodeRef node) : node(node) {}
  Node *operator->() const { return node.get(); }
  explicit operator bool() const { return bool(node); }
};
// cont'd

我们沿着树走的“路径”保存在专门用于此目的的堆栈中:

// cont'd
// A stack holds the path to the current node
class NodeStack {
  // Top of stack is the current node
  std::stack<BreadCrumb, std::vector<BreadCrumb>> m_stack;
public:
  NodeStack() = default;
  NodeStack(NodeRef n) { if (n) m_stack.push(n); }
  
  bool empty() const { return m_stack.empty(); }
  // The breadcrumb that represents the top of stack, and thus the current node
  BreadCrumb &crumb() { return m_stack.top(); }
  const BreadCrumb &crumb() const { return m_stack.top(); }
  NodeRef node() { return crumb().node; }
  Node *node() const { return empty() ? nullptr : crumb().node.get(); }
  
  void push(NodeRef n) { m_stack.push(n); }

  // Visit and mark the node if not visited yet
  bool visit() {
    if (crumb().visited) return false;
    crumb().visited = true;
    return true;
  }
  // Descend one level via the left edge if not traversed left yet
  bool descendLeft() {
    if (crumb().traversedLeft) return false;
    crumb().traversedLeft = true;
    auto &n = crumb()->lhs_;
    if (n) m_stack.push(n);
    return bool(n);
  }
  // Descends one level via right edge if not traversed right yet
  bool descendRight() {
    if (crumb().traversedRight) return false;
    crumb().traversedRight = true;
    auto &n = crumb()->rhs_;
    if (n) m_stack.push(n);
    return bool(n);
  }
  // Ascends one level
  bool ascend() {
    m_stack.pop();
    return !empty();
  }
};
// cont'd

树的遍历操作在栈中被抽象出来,所以剩下的代码层次更高,没有这些细节。

现在我们可以实现一个节点迭代器,它使用堆栈来保持其面包屑的踪迹:

// cont'd
// Node Forward Iterator - iterates the nodes in given order
class NodeIterator {
  using Advancer = void (NodeIterator::*)();
  
  NodeStack m_stack; // Breadcrumb path to the current node
  Advancer m_advancer; // Method that advances to next node in chosen order
  Order m_order = Order::In;

public:
  NodeIterator() = default;
  // Dereferencing operators
  Node& operator*() { return *m_stack.node(); }
  Node* operator->() { return m_stack.node().get(); }
  // Do the iterators both point to the same node (or no node)?
  bool operator==(const NodeIterator &other) const {
    return m_stack.node() == other.m_stack.node();
  }
  bool operator==(decltype(nullptr)) const { return !bool(m_stack.node()); }
  bool operator!=(const NodeIterator &other) const { return m_stack.node(); }
  bool operator!=(decltype(nullptr)) const { return bool(m_stack.node()); }
  
  NodeIterator(NodeRef n, Order order = Order::In) : m_stack(n) {
    setOrder(order);
    if (n) operator++(); // Start the traversal
  }
  
  void setOrder(Order order) {
    if (order == Order::In)
      m_advancer = &NodeIterator::advanceInorder;
    else if (order == Order::Pre)
      m_advancer = &NodeIterator::advancePreorder;
    else if (order == Order::Post)
      m_advancer = &NodeIterator::advancePostorder;
    m_order = order;
  }

  NodeIterator &operator++() { // Preincrement operator
    assert(!m_stack.empty());
    std::invoke(m_advancer, this);
    return *this;
  }
  // No postincrement operator since it'd need to copy the stack and thus
  // be way too expensive to casually expose via postincrement.

  void advanceInorder();
  void advancePreorder();
  void advancePostorder();
  
  bool goLeft() { return m_stack.descendLeft(); }
  bool goRight() { return m_stack.descendRight(); }
};
// cont'd

还记得堆栈吗?它让我们可以相当简洁地描述顺序遍历、前序遍历和后序遍历:

// cont'd
void NodeIterator::advanceInorder() {
  for (;;) {
    if (m_stack.descendLeft())
      continue;
    if (m_stack.visit())
      break;
    if (m_stack.descendRight())
      continue;
    if (m_stack.ascend())
      continue;
    assert(m_stack.empty());
    break;
  }
}

void NodeIterator::advancePreorder() {
  for (;;) {
    if (m_stack.visit())
      break;
    if (m_stack.descendLeft())
      continue;
    if (m_stack.descendRight())
      continue;
    if (m_stack.ascend())
      continue;
    assert(m_stack.empty());
    break;
  }
}

void NodeIterator::advancePostorder() {
  for (;;) {
    if (m_stack.descendLeft())
      continue;
    if (m_stack.descendRight())
      continue;
    if (m_stack.visit())
      break;
    if (m_stack.ascend())
      continue;
    assert(m_stack.empty());
    break;
  }
}
// cont'd

现在,当我们希望迭代以某个节点为根的树时,我们想要一些简单的方法来使用这个迭代器:

// cont'd
class TreeRangeAdapter {
  NodeRef m_root;
  Order m_order;
public:
  TreeRangeAdapter(NodeRef root, Order order) :
    m_root(root), m_order(order) {}
  NodeIterator begin() const { return {m_root, m_order}; }
  constexpr auto end() const { return nullptr; }
};

auto inOrder(NodeRef node) { return TreeRangeAdapter(node, Order::In); }
auto preOrder(NodeRef node) { return TreeRangeAdapter(node, Order::Pre); }
auto postOrder(NodeRef node) { return TreeRangeAdapter(node, Order::Post); }
// cont'd

这一切将如何运作?这只是填充树并按顺序遍历的简单示例:

// cont'd
#include <iostream>
#include <cstdlib>

int main() {
  Tree tree;
  for (int i = 0; i < 10; ++i) insert(tree, rand() / (RAND_MAX/100));
  
  for (auto &node : inOrder(tree.root_)) {
    std::cout << node.value_ << " ";
  }
  std::cout << "\n";
}
// complete example ends

输出:

19 27 33 39 55 76 78 79 84 91 

【讨论】:

  • 天哪,你是神
  • @WalterMedly :) 这绝对不是解决您的问题的直接方法,也不是初学者容易编写的东西 - 但这通常是因为学习 C++ 的初学者并不真正,而是学习一些 C 和 C++ 的混蛋突变产物,因为大多数教程(和教师)都是这样的, 并且可以将事物抽象为多个级别,以便每个级别本身更容易理解。这只是看起来不错的东西:)
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多