簡體   English   中英

無法將莫爾斯電碼排序為二叉樹

[英]Trouble sorting morse code into Binary Tree

我試圖理解為什么我的Insert(string key)沒有正確排序莫爾斯電碼 (az 0-9)。 從輸出看來,它正在對完全相同的莫爾斯電碼進行排序。 這讓我相信問題出在我的 Insert 函數上,因為提供給 Insert 函數的莫爾斯電碼不包含任何重復項。

void BST::Insert(node *&start, string key){
    if (start == NULL) {
        start = new node;
        start->code = key;
        start->left = start->right = NULL;
        printf("Inserting Morse Code -> %s\n",key.c_str());
    }
}


void BST::Insert(string key) {
    node **start = &root;
    if (*start != NULL) {
        for(int i = 0; i < key.length(); i++) {
            assert(*start);
            if (key[i] == '.') {
                start = &((*start)->left);
            } else if (key[i] == '-') {
                 start = &((*start)->right);
            }else {
                break;
            }
            Insert(*start, key);
       }
    } else {
        Insert(root, key);
    }
}

我生產的輸出是:

Inserting Morse Code -> .-
Inserting Morse Code -> -...
Inserting Morse Code -> -...
Inserting Morse Code -> -...
Inserting Morse Code -> -...
Inserting Morse Code -> -.-.
Inserting Morse Code -> -.-.
Inserting Morse Code -> .
Inserting Morse Code -> ..-.
Inserting Morse Code -> ..-.
Inserting Morse Code -> ..-.
Inserting Morse Code -> --.
Inserting Morse Code -> --.
Inserting Morse Code -> ....
Inserting Morse Code -> ....
Inserting Morse Code -> .---
Inserting Morse Code -> .---
Inserting Morse Code -> .---
Inserting Morse Code -> .-..
Inserting Morse Code -> .-..
Inserting Morse Code -> ---
Inserting Morse Code -> .--.
Inserting Morse Code -> --.-
Inserting Morse Code -> ...-
Inserting Morse Code -> -..-
Inserting Morse Code -> -.--
Inserting Morse Code -> --..
Inserting Morse Code -> ----
Inserting Morse Code -> .----
Inserting Morse Code -> ..---
Inserting Morse Code -> ..---
Inserting Morse Code -> ...--
Inserting Morse Code -> ....-
Inserting Morse Code -> .....
Inserting Morse Code -> -....
Inserting Morse Code -> --...
Inserting Morse Code -> ---..
Inserting Morse Code -> ---..
Inserting Morse Code -> ----.
.....
....
....-
....
...-
...--
..-.
..-.
..-.
..---
..---
.
.-..
.-..
.---
.--.
.---
.---
.----
.-
-....
-...
-...
-..-
-...
-.-.
-.-.
-.--
-...
--...
--..
--.
--.-
--.
---..
---..
---
----.
----

有什么問題 ?

你的Insert(*start, key); for循環的主體中for循環重復直到達到字符串的長度。 因此,當插入 4 個莫爾斯數字的代碼時,您將插入 4 次。 唯一的例外是第一次插入,因為這是一個沒有for循環的分支。

如何解決?

您需要將密鑰與當前代碼進行比較,以決定是向左插入還是向右插入。 目前您正試圖通過混合不合適的迭代和遞歸方法來解決這個問題。

更好的方法是使用公共前端功能:

void BST::Insert(string key) {
    Insert(root, key); 
    }

並使輔助函數私有和遞歸:

void BST::Insert(node *&start, string key){
    if (start == nullptr) {    // new node must be created
        start = new node;
        start->code = key;    // better move this to the node constructor
        start->left = start->right = nullptr;  // same here
        printf("Inserting Morse Code -> %s\n",key.c_str());
    }
    else if (start->code<key) 
        Insert (start->left, key); 
    else if (start->code>key) 
        Insert (start->right, key); 
    else cout<<"Duplicate"<<endl;
}

在線演示

您可能希望修改評估訂單的方式。

現在,由於它始終是相同的鍵,您可以將鍵作為 const string& 傳遞以避免遞歸中不必要的副本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM