作者solinari (soli)
看板C_and_CPP
標題[問題] binary tree sort element
時間Mon Aug 18 02:07:49 2014
開發平台(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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 169.231.26.22
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1408298872.A.546.html
→ scwg: sortedElements 為什麼要把 helper 的結果存進 *length 08/18 07:24
→ scwg: wait a minute... 兩個測試函數都沒有開空間給 sortedElemen 08/18 07:26
→ scwg: ts 存資料 "arrLength" 只有「一個」整數, NonInsert 沒 08/18 07:27
→ scwg: crash 只是運氣好... 08/18 07:27
→ solinari: 請問我該怎麼改它呢@@? 08/18 07:52
→ solinari: 我應該在哪裡開空間呢? 08/18 13:42
→ solinari: 在不改動main.cpp的前提下 08/18 13:47