最近刷題的時候發現要用到樹的結構,當時腦子就宕機咯,不知道怎麼建立一個自己需要的樹,這篇博文介紹瞭如何建立一個二叉樹。
不同於劍指offer或者書上講的給定先序遍歷的數組和中序遍歷的數組來唯一確定一棵樹,我給出的是已知二叉樹的結構在自己的白紙上,怎麼建立它。
已知一顆二叉樹在白紙上,我的習慣是把它變成一個二維數組(也就相當於層序遍歷,但是在結點爲null的位置插入0),之後依據這個二維的數組建立一個對應的二叉樹。下面是代碼。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Tree{
int val;
Tree* left;
Tree* right;
Tree(int a):val(a),left(nullptr),right(nullptr){}
};
void TreeConstruct(vector<vector<int> >& arr,Tree* root,int i,int j)
{
if(arr[i+1][j*2]==0)
root->left = nullptr;
else
{
Tree* nodel = new Tree(arr[i+1][j*2]);
root->left = nodel;
}
if(arr[i+1][j*2+1]==0)
root->right = nullptr;
else
{
Tree* noder = new Tree(arr[i+1][j*2+1]);
root->right = noder;
}
if(i < arr.size()-2)
{
if(root->left != nullptr)
TreeConstruct(arr,root->left,i+1,j*2);
if(root->right != nullptr)
TreeConstruct(arr,root->right,i+1,j*2+1);
}
}
void inorder(Tree* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
inorder(root->left);
inorder(root->right);
}
int main(){
vector<vector<int> > arr = {{1},{2,3},{4,0,6,7},{1,2,0,0,5,5,3,2}};
Tree* root = new Tree(arr[0][0]);
TreeConstruct(arr,root,0,0);
// for(int i = 0;i < arr.size();i++)
// {
// for(auto x : arr[i])
// {
// cout << x << " ";
// }
// cout << endl;
// }
inorder(root1);
}
第二種方式是給出一個 層序遍歷的queue來創建,下面是兩種方式混在一下的完整代碼。代碼都經過測試的。#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Tree{
int val;
Tree* left;
Tree* right;
Tree(int a):val(a),left(nullptr),right(nullptr){}
};
void TreeConstruct(Tree* root,queue<int> &arr)
{
queue<Tree*> que;
que.push(root);
while(!arr.empty() && !que.empty())
{
Tree *node = que.front();
que.pop();
Tree* nodel = new Tree(arr.front());
arr.pop();
Tree* noder = new Tree(arr.front());
arr.pop();
node->left = nodel;
node->right = noder;
que.push(node->left);
que.push(node->right);
}
}
void TreeConstruct(vector<vector<int> >& arr,Tree* root,int i,int j)
{
if(arr[i+1][j*2]==0)
root->left = nullptr;
else
{
Tree* nodel = new Tree(arr[i+1][j*2]);
root->left = nodel;
}
if(arr[i+1][j*2+1]==0)
root->right = nullptr;
else
{
Tree* noder = new Tree(arr[i+1][j*2+1]);
root->right = noder;
}
if(i < arr.size()-2)
{
if(root->left != nullptr)
TreeConstruct(arr,root->left,i+1,j*2);
if(root->right != nullptr)
TreeConstruct(arr,root->right,i+1,j*2+1);
}
}
void inorder(Tree* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
inorder(root->left);
inorder(root->right);
}
void BreadthFirstSearch(Tree *root)
{
queue<Tree*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Tree *node = nodeQueue.front();
cout << node->val << ' ';
nodeQueue.pop();
if (node->left)
{
nodeQueue.push(node->left);
}
if (node->right)
{
nodeQueue.push(node->right);
}
}
}
int main(){
vector<vector<int> > arr = {{1},{2,3},{4,0,6,7},{1,2,0,0,5,5,3,2}};
vector<int> helpque = {1,2,0,4,5,6,7};
queue<int> arr1;
for(auto x : helpque)
{
arr1.push(x);
}
Tree* root = new Tree(arr[0][0]);
TreeConstruct(arr,root,0,0);
Tree* root1 = new Tree(arr1.front());
arr1.pop();
TreeConstruct(root1,arr1);
// for(int i = 0;i < arr.size();i++)
// {
// for(auto x : arr[i])
// {
// cout << x << " ";
// }
// cout << endl;
// }
BreadthFirstSearch(root1);
inorder(root1);
}