[问题] binary tree sort element

楼主: solinari (soli)   2014-08-18 02:07:49
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux clang++
问题(Question):
可以请大家帮我看看BST.cpp里的函式
int* sortedElements(int* length) const
int sortedElementsHelper(Node* node, int* array, int pos) const
有什么问题吗?
int sortedElementsHelper(Node* node, int* array, int pos) const
这函式我是照教材的建议写的.
要把binary tree 里面的 node 按大小排好在 array 里,
可是出来的结果不对, 有问题.
我想问题可能是出在
int* sortedElements(int* length) const
可以请大家帮我看看问题出在哪吗? 我试了好久
谢谢!
预期的正确结果(Expected Output):
用 doNonInsertTesting 函式应该要出现
Tree according to sortedElements(int*): 1, 3, 5, 7,
Tree according to sortedElements(int*): 0, 1, 2, 3, 4,
用 doFullTesting 函式应该要出现
Tree according to sortedElements(int*): 1, 3, 5, 7,
Tree according to sortedElements(int*): 0, 1, 2, 3, 4,
Tree according to sortedElements(int*): 0, 3, 4, 7, 10, 12, 15,
错误结果(Wrong Output):
在用 doNonInsertTesting 函式时第一个数字不对
感觉好像是变成整个 tree 的 node 的数目
Tree according to sortedElements(int*): 4, 3, 5, 7,
Tree according to sortedElements(int*): 5, 1, 2, 3, 4,
然后用 doFullTesting 时出现
http://codepad.org/EgjyFj9i
程式码(Code):(请善用置底文网页, 记得排版)
main.cpp http://codepad.org/7QXzx7XY
BST.h http://codepad.org/kX655NFK
BST.cpp http://codepad.org/AsXbLLuA
Node.h http://codepad.org/Y30VYEyy
Node.cpp http://codepad.org/i968Q2F3
作者: scwg ( )   2014-08-18 07:24:00
sortedElements 为什么要把 helper 的结果存进 *lengthwait a minute... 两个测试函数都没有开空间给 sortedElements 存资料 "arrLength" 只有“一个”整数, NonInsert 没crash 只是运气好...
楼主: solinari (soli)   2014-08-18 07:52:00
请问我该怎么改它呢@@?我应该在哪里开空间呢?在不改动main.cpp的前提下

Links booklink

Contact Us: admin [ a t ] ucptt.com