简体   繁体   English

如何使用Stringstream C ++递归打印二进制搜索树中的所有节点

[英]How to recursively print out all the nodes in a binary search tree using stringstream C++

I would like to print the contents of all the nodes in my binary search tree by using a stringstream and recursion. 我想通过使用stringstream和递归来打印二进制搜索树中所有节点的内容。 The issue is that when I use this code, only the contents of the root are displayed. 问题是当我使用此代码时,仅显示根目录的内容。 I know the reason is that every time I recursively call the function InOrder(BSTNode* bst_node), my stringstream variable is created again. 我知道原因是每次我递归调用InOrder(BSTNode * bst_node)函数时,都会再次创建我的stringstream变量。 What can I do to my code to fix this issue while still utilizing a stringstream for output? 在仍然利用字符串流进行输出的同时,我该如何对我的代码进行处理以解决此问题?

This is my code: 这是我的代码:

string BSTree::InOrder(BSTNode* bst_node) {
  stringstream ss;
  if (root_ ==  NULL) {
    return "";
  } else {
    if (bst_node->GetLeftChild() != NULL) {
      InOrder(bst_node->GetLeftChild());
    }
    ss << bst_node->GetContents() << " ";
    if (bst_node->GetRightChild() != NULL) {
      InOrder(bst_node->GetRightChild());
    }
  }
  return ss.str();
}

Something like that maybe? 这样的事吧?

string BSTree::InOrder(BSTNode* bst_node)
{
  if (!bst_node)
    return "";
  ostringstream ss;
  ss << InOrder(bst_node->GetLeftChild());
  ss << bst_node->GetContents() << " ";
  ss << InOrder(bst_node->GetRightChild());
  return ss.str();
}

or, you can pass around the same instance of stringstream: 或者,您可以传递相同的stringstream实例:

void BSTree::InOrderImpl(BSTNode* bst_node, ostringstream &ss)
{
  if (bst_node)
  {
    InOrderImpl(bst_node->GetLeftChild(), ss);
    ss << bst_node->GetContents() << " ";
    InOrderImpl(bst_node->GetRightChild(), ss);
  }
}

string BSTree::InOrder(BSTNode* bst_node)
{
  ostringstream ss;
  InOrder(bst_node, ss);
  return ss.str();
}

Easy: just define an utility function where stringstream is passed as argument: 简单:只需定义一个实用函数,在其中将stringstream作为参数传递:

void BSTree::InOrder(BSTNode *root, std::stringstream &ss)
{
  if (root == nullptr) { return; }
  InOrder(root->GetLeftChild(), ss)
  ss << root->GetContents() << " ";
  InOrder(root->GetRightChild(), ss)
}

string BSTree::InOrder(BSTNode* bst_node) {
  stringstream ss;
  InOrder(bst_node, ss);
  return ss.str();
}

Notice that instantiating only one stringstream where the whole tree is dumped is probably much more efficient than instantiating a stringstream for each node of your tree (as it happens if you define the utility function as something taking a node and returning a string). 请注意,仅实例化一个stringstream在整个树倾倒可能比实例化一个更有效stringstream为你的树的每个节点(因为它如果你定义的效用函数的东西取一个节点,并返回一个字符串发生)。

My no brainer for this - pass the stringstream as argument by reference to this method: 我毫不费力地-通过引用此方法将stringstream作为参数传递:

static stringstream existingSSreference;
string BSTree::InOrder(BSTNode* bst_node, stringstream & ss = existingSSreference) {

  if (root_ ==  NULL) {
    return "";
  } else {
    if (bst_node->GetLeftChild() != NULL) {
      InOrder(bst_node->GetLeftChild(), ss);
    }
    ss << bst_node->GetContents();
    if (bst_node->GetRightChild() != NULL) {
      InOrder(bst_node->GetRightChild(), ss);
    }
  }
  return ss.str();
}

and either you want to declare the stringstream before using the method: 并且您想要在使用方法之前声明stringstream:

stringstream ss;
string myResult = bstreeObj->InOrder(bstNode, ss);

or use it without passing it 或不通过就使用它

string myResult = bstreeObj->InOrder(bstNode);

this should work. 这应该工作。 Better define the stringstream outside the function and pass it when you call the function. 更好地在函数外部定义stringstream,并在调用函数时将其传递。

EDIT 编辑

As pointed out by Ajay, a reference argument can't have default value, unless we pass an actual existing instance as default (so that we can keep the possibility to omit the second argument). 正如Ajay指出的那样,引用参数不能具有默认值,除非我们将实际的现有实例作为默认值传递(这样我们就可以省略第二个参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM