日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二进制搜索树_将排序的数组转换为二进制搜索树

發布時間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二进制搜索树_将排序的数组转换为二进制搜索树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二進制搜索樹

Problem statement:

問題陳述:

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

給定一個數組,其中元素按升序排序,請將其轉換為高度平衡的BST。

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

對于此問題,將高度平衡的二叉樹定義為一個二叉樹,其中每個節點的兩個子樹的深度相差不超過1。

Example:

例:

Let the sorted array to be: [-1, 3, 6, 8] The corresponding balanced BST is:3/ \-1 6\8

Solution:

解:

The algorithm is simply finding the median in the sorted array and assigning it as a root. Then process the subtrees recursively.

該算法只是在排序后的數組中找到中位數并將其分配為根。 然后遞歸處理子樹。

Let the function to build the balanced BST from the sorted array is:

讓該函數根據排序后的數組構建平衡的BST:

buildBST() which has parameters: sorted array, lower index, higher index

buildBST()具有以下參數: 排序數組 , 較低索引 , 較高索引

has return type: TreeNode* // returns the root actually

具有返回類型: TreeNode * //實際上返回根

Function buildBST(sorted array, lower index , higher index)1. Base case:IF lower index>higher indexReturn NULL2. Declare middle index as (lower index + higher index)/23. root=createnode(array[middle index]);4. Create the left subtree recursivelyRoot->left=buildBST(sorted array, lower index, middle index-1)5. Create the right subtree recursivelyRoot->left=buildBST(sorted array, middle index-1,higher index)6. Return root END FUNCTION

In the main function we call,

在主函數中,我們稱之為

Root=buildBST (sorted array, 0, size of array-1) .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

C++ implementation

C ++實現

#include <bits/stdc++.h> using namespace std;// TreeNode node type class TreeNode{public: int val; //valueTreeNode *left; //pointer to left childTreeNode *right; //pointer to right child };// creating new node TreeNode* newnode(int data) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = data; node->left = NULL; node->right = NULL; return(node); }TreeNode* buildBST(vector<int> nums,int low,int high){if(low>high)return NULL;int mid=(low+high)/2;TreeNode* root=newnode(nums[mid]); //creates new nodesroot->left=buildBST(nums,low,mid-1);root->right=buildBST(nums,mid+1,high);return root; }TreeNode* sortedArrayToBST(vector<int>& nums) {TreeNode* root=buildBST(nums,0,nums.size()-1);return root; }void levelOrder(TreeNode* root) {cout<<"root: \n";queue<TreeNode*> q;if(root==NULL){cout<<"empty tree\n";}int count=1; TreeNode* temp;q.push(root);q.push(NULL);while(!q.empty()){temp=q.front();q.pop();if(temp==NULL){if(!q.empty()){cout<<"\nend of level: "<<count++<<endl;q.push(NULL);}}else{cout<<temp->val<<" ";if(temp->left)q.push(temp->left);if(temp->right)q.push(temp->right);}}cout<<"\nend of level: "<<count<<endl; }int main(){int n,no;cout<<"enter no of elements\n";cin>>n;vector<int> v;cout<<"enter the sorted array\n";while(n--){cin>>no;v.push_back(no);}TreeNode* root=sortedArrayToBST(v);cout<<"displaying level order traversal\n";levelOrder(root);return 0; }

Output

輸出量

enter no of elements 4 enter the sorted array -1 3 6 8 displaying level order traversal root: 3 end of level: 1 -1 6 end of level: 2 8 end of level: 3 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Example with explanation

帶說明的例子

For simplicity all nodes are represented by its value Let the sorted array to be: A=-1, 3, 6, 8So in the main function it calls: Root=buildBST( A, 0, 3) ---------------------------------------------------buildBST( A, 0, 3) base case isn’t met mid= 1 root= A[1]=3 3->left=buildBST( A, 0, 0) 3->right= buildBST( A, 2, 3) Return 3 (node) ---------------------------------------------------buildBST( A, 0, 0) base case isn’t met mid= 0 root= A[0]=-1 -1->left=buildBST(A, 0, -1) (-1)->right= buildBST(A, 1, 0) Returns -1(node) ---------------------------------------------------buildBST( A, 2, 3) base case isn’t met mid= 2 root= A[2]=6 6->left=buildBST(A, 2, 1) (6)->right= buildBST(A, 3, 3) Returns 6(node) ---------------------------------------------------buildBST( A, 0, -1) base case is met Returns null ---------------------------------------------------buildBST( A, 1, 0) base case is met Returns null ---------------------------------------------------buildBST( A, 2, 1) base case is met Returns null ---------------------------------------------------buildBST( A, 3, 3) base case isn’t met mid= 3 root= A[3]=8 8->left=buildBST(A, 3, 2) (8)->right= buildBST(A, 4, 3) Returns 8(node) ---------------------------------------------------buildBST( A, 3, 2) base case is met Returns null ---------------------------------------------------buildBST( A, 4, 3) base case is met Returns nullSo, 8->left=NULL 8->right=NULL 6->left=NULL 6->right=8 -1->left=NULL -1->right=NULL 3->left=-1 3->right=6So the tree becomes:3/ \-1 6\8

翻譯自: https://www.includehelp.com/icp/convert-sorted-array-to-binary-search-tree.aspx

二進制搜索樹

總結

以上是生活随笔為你收集整理的二进制搜索树_将排序的数组转换为二进制搜索树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。