C++完整二叉樹插入器


眾所周知,完整二叉樹是指除最後一層外,每一層都被完全填滿,並且所有節點都儘可能靠左的二叉樹。我們必須編寫一個名為CBTInserter的資料結構,它用一個完整的二叉樹初始化,並支援以下操作:

  • CBTInserter(TreeNode root):這將用給定的根節點root初始化資料結構;

  • CBTInserter.insert(int v):用於將一個值為node.val = v的TreeNode插入到樹中,使得樹保持完整,並返回插入的TreeNode的父節點的值;

  • CBTInserter.get_root():這將返回樹的根節點。

例如,如果我們將樹初始化為[1,2,3,4,5,6],然後插入7和8,然後嘗試獲取樹,輸出將是:3, 4,[1,2,3,4,5,6,7,8],3是因為7將插入到3下,4是因為8將插入到4下。

為了解決這個問題,我們將遵循以下步驟:

  • 定義一個佇列q和一個根節點

  • 初始化器將獲取完整的二叉樹,然後按如下方式工作:

  • 將root設定為給定的root,並將root插入到q中。

  • while迴圈:

    • 如果root的左子節點存在,則將root的左子節點插入到q中,否則中斷迴圈。

    • 如果root的右子節點存在,則將root的右子節點插入到q中,並從q中刪除首節點,否則中斷迴圈。

  • 在insert方法中,它將獲取值v。

  • 設定parent := q的首元素,temp := 一個值為v的新節點,並將temp插入到q中。

  • 如果parent的左子節點不存在,則設定parent的左子節點 := temp;否則,從q中刪除首元素,並將temp作為parent的右子節點插入。

  • 返回parent的值。

  • 在getRoot()方法中,返回root。

示例(C++)

讓我們看看下面的實現來更好地理解:

 線上演示

#include <bits/stdc++.h>
using namespace std;
class TreeNode{
   public:
      int val;
      TreeNode *left, *right;
      TreeNode(int data){
         val = data;
         left = NULL;
         right = NULL;
      }
};
void insert(TreeNode **root, int val){
   queue<TreeNode*> q;
   q.push(*root);
   while(q.size()){
      TreeNode *temp = q.front();
      q.pop();
      if(!temp->left){
         if(val != NULL)
            temp->left = new TreeNode(val);
         else
            temp->left = new TreeNode(0);
         return;
      } else {
         q.push(temp->left);
      }
      if(!temp->right){
         if(val != NULL)
            temp->right = new TreeNode(val);
         else
            temp->right = new TreeNode(0);
         return;
      } else {
         q.push(temp->right);
      }
   }
}
TreeNode *make_tree(vector<int> v){
   TreeNode *root = new TreeNode(v[0]);
   for(int i = 1; i<v.size(); i++){
      insert(&root, v[i]);
   }
   return root;
}
void tree_level_trav(TreeNode*root){
   if (root == NULL) return;
   cout << "[";
   queue<TreeNode *> q;
   TreeNode *curr;
   q.push(root);
   q.push(NULL);
   while (q.size() > 1) {
      curr = q.front();
      q.pop();
      if (curr == NULL){
         q.push(NULL);
      } else {
         if(curr->left)
            q.push(curr->left);
         if(curr->right)
            q.push(curr->right);
         if(curr == NULL || curr->val == 0){
            cout << "null" << ", ";
         } else{
            cout << curr->val << ", ";
         }
      }
   }
   cout << "]"<<endl;
}
class CBTInserter {
public:
   queue <TreeNode*> q;
   TreeNode* root;
   CBTInserter(TreeNode* root) {
      this->root = root;
      q.push(root);
      while(1){
         if(root->left){
            q.push(root->left);
         }
         else break;
         if(root->right){
            q.push(root->right);
            q.pop();
            root = q.front();
         }
         else break;
      }
   }
   int insert(int v) {
      TreeNode* parent = q.front();
      TreeNode* temp = new TreeNode(v);
      q.push(temp);
      if(!parent->left){
         parent->left = temp;
      } else {
         q.pop();
         parent->right = temp;
      }
      return parent->val;
   }
   TreeNode* get_root() {
      return root;
   }
};
main(){
   vector<int> v = {1,2,3,4,5,6};
   TreeNode *root = make_tree(v);
   CBTInserter ob(root);
   cout << (ob.insert(7)) << endl;
   cout << (ob.insert(8)) << endl;
   tree_level_trav(ob.get_root());
}

輸入

Initialize the tree as [1,2,3,4,5,6], then insert 7 and 8 into the tree, then find root

輸出

3
4
[1, 2, 3, 4, 5, 6, 7, 8, ]

更新於:2020年4月30日

881 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告