简体   繁体   English

测试遍历二叉搜索树(BST)

[英]Testing traversal of binary search tree (BST)

What is a good way to test the in-order traversal of a binary search tree with Google Test? 用Google Test测试二进制搜索树的有序遍历的好方法是什么? I can test it manually by printing out the values of the nodes and verifying that the list is sorted visually. 我可以通过打印出节点的值并验证列表是否在视觉上进行排序来进行手动测试。 Is there a way to validate this automatically using Google Test? 有没有一种方法可以使用Google测试自动验证? I was thinking of adding a static string to the node class and appending to the string for every node visited. 我正在考虑将静态字符串添加到节点类,并为访问的每个节点附加到字符串。 The problem with this is that there could be multiple trees which should each have their own in-order list but there can only be one static string. 问题是可能有多个树,每个树应该有自己的顺序列表,但是只能有一个静态字符串。

Once I get it working with the in-order traversal the solution should be extensible to pre-order and post-order traversals. 一旦我将其与按顺序遍历一起使用,该解决方案应该可以扩展到按顺序遍历和按顺序遍历。

Keep track of the previous entry and when you're no longer on the first entry. 跟踪上一个条目以及何时不再使用第一个条目。

Assert that you're still on the first entry or the current entry is >= the previous entry. 断言您仍在第一个条目上,或者当前条目> =上一个条目。

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

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