logo

二分探索木のバランスをとる

BST を指定すると ( B イナリ S 耳を傾ける T ree) がアンバランスである可能性がある場合は、可能な限り最小の高さを持つバランスの取れた BST に変換します。

例:

Input:  30  /  20  /  10 Output:  20  /   10 30   Input:  4  /  3  /  2  /  1 Output:  3 3 2  /  /  /   1 4 OR 2 4 OR 1 3 OR ..   /   2 1 4   Input:  4  /   3 5  /   2 6   /   1 7 Output:  4  /   2 6  /  /  1 3 5 7>
推奨練習法 ノーマルBSTからバランスBSTまで ぜひ試してみてください!

シンプルな解決策 これは、Inorder でノードをトラバースし、AVL ツリーのような自己均衡 BST に 1 つずつ挿入することです。このソリューションの時間計算量は O(n Log n) であり、最悪の場合、AVL ツリーの高さが以下になる可能性があるため、このソリューションは可能な最小の高さを保証しません。 1.44*log 2 n



アン 効率的なソリューション 可能な限り最小の高さでバランスの取れた BST を O(n) 時間で構築できます。以下に手順を示します。

  1. 指定された BST を順番に走査し、結果を配列に格納します。このステップには O(n) 時間がかかります。 BST の順序トラバーサルでは常にソートされたシーケンスが生成されるため、この配列はソートされることに注意してください。
  2. 説明した再帰的アプローチを使用して、上記で作成したソートされた配列からバランスのとれた BST を構築します。 ここ 。すべての要素を 1 回だけ走査し、要素の処理には O(1) 時間がかかるため、このステップにも O(n) 時間がかかります。

以下は上記の手順の実装です。

C++




// C++ program to convert a left unbalanced BST to> // a balanced BST> #include> using> namespace> std;> struct> Node> {> >int> data;> >Node* left, *right;> };> /* This function traverse the skewed binary tree and> >stores its nodes pointers in vector nodes[] */> void> storeBSTNodes(Node* root, vector &nodes)> {> >// Base case> >if> (root==NULL)> >return>;> >// Store nodes in Inorder (which is sorted> >// order for BST)> >storeBSTNodes(root->左、ノード);>>' nodes.push_back(root);> >storeBSTNodes(root->そうですね、ノード);>>' /* Recursive function to construct binary tree */> Node* buildTreeUtil(vector &nodes,>int> start,> >int> end)> {> >// base case> >if> (start>終了)> >return> NULL;> >/* Get the middle element and make it root */> >int> mid = (start + end)/2;> >Node *root = nodes[mid];> >/* Using index in Inorder traversal, construct> >left and right subtress */> >root->left = buildTreeUtil(nodes, start, mid-1);>> >root->right = buildTreeUtil(nodes,mid+1,end);>> >return> root;> }> // This functions converts an unbalanced BST to> // a balanced BST> Node* buildTree(Node* root)> {> >// Store nodes of given BST in sorted order> >vector nodes;> >storeBSTNodes(root, nodes);> >// Constructs BST from nodes[]> >int> n = nodes.size();> >return> buildTreeUtil(nodes, 0, n-1);> }> // Utility function to create a new node> Node* newNode(>int> data)> {> >Node* node =>new> Node;> >node->データ = データ;>> >node->left = ノード->right = NULL;>> >return> (node);> }> /* Function to do preorder traversal of tree */> void> preOrder(Node* node)> {> >if> (node == NULL)> >return>;> >printf>(>'%d '>, node->データ);>>' preOrder(node->左);>> preOrder(node->右);>> // Driver program> int> main()> {> >/* Constructed skewed binary tree is> >10> >/> >8> >/> >7> >/> >6> >/> >5 */> >Node* root = newNode(10);> >root->left = newNode(8);>>' root->left->left = newNode(7);>> >root->left->left->left = newNode(6);>> >root->左->左->左->左 = newNode(5);>> >root = buildTree(root);> >printf>(>'Preorder traversal of balanced '> >'BST is : '>);> >preOrder(root);> >return> 0;> }>

>

プロローグとは何ですか

>

ジャワ


シス



// Java program to convert a left unbalanced BST to a balanced BST> import> java.util.*;> /* A binary tree node has data, pointer to left child> >and a pointer to right child */> class> Node> {> >int> data;> >Node left, right;> >public> Node(>int> data)> >{> >this>.data = data;> >left = right =>null>;> >}> }> class> BinaryTree> {> >Node root;> >/* This function traverse the skewed binary tree and> >stores its nodes pointers in vector nodes[] */> >void> storeBSTNodes(Node root, Vector nodes)> >{> >// Base case> >if> (root ==>null>)> >return>;> >// Store nodes in Inorder (which is sorted> >// order for BST)> >storeBSTNodes(root.left, nodes);> >nodes.add(root);> >storeBSTNodes(root.right, nodes);> >}> >/* Recursive function to construct binary tree */> >Node buildTreeUtil(Vector nodes,>int> start,> >int> end)> >{> >// base case> >if> (start>終了)> >return> null>;> >/* Get the middle element and make it root */> >int> mid = (start + end) />2>;> >Node node = nodes.get(mid);> >/* Using index in Inorder traversal, construct> >left and right subtress */> >node.left = buildTreeUtil(nodes, start, mid ->1>);> >node.right = buildTreeUtil(nodes, mid +>1>, end);> >return> node;> >}> >// This functions converts an unbalanced BST to> >// a balanced BST> >Node buildTree(Node root)> >{> >// Store nodes of given BST in sorted order> >Vector nodes =>new> Vector();> >storeBSTNodes(root, nodes);> >// Constructs BST from nodes[]> >int> n = nodes.size();> >return> buildTreeUtil(nodes,>0>, n ->1>);> >}> >/* Function to do preorder traversal of tree */> >void> preOrder(Node node)> >{> >if> (node ==>null>)> >return>;> >System.out.print(node.data +>' '>);> >preOrder(node.left);> >preOrder(node.right);> >}> >// Driver program to test the above functions> >public> static> void> main(String[] args)> >{> >/* Constructed skewed binary tree is> >10> >/> >8> >/> >7> >/> >6> >/> >5 */> >BinaryTree tree =>new> BinaryTree();> >tree.root =>new> Node(>10>);> >tree.root.left =>new> Node(>8>);> >tree.root.left.left =>new> Node(>7>);> >tree.root.left.left.left =>new> Node(>6>);> >tree.root.left.left.left.left =>new> Node(>5>);> >tree.root = tree.buildTree(tree.root);> >System.out.println(>'Preorder traversal of balanced BST is :'>);> >tree.preOrder(tree.root);> >}> }> // This code has been contributed by Mayank Jaiswal(mayank_24)>

>

>

Python3




# Python3 program to convert a left> # unbalanced BST to a balanced BST> import> sys> import> math> # A binary tree node has data, pointer to left child> # and a pointer to right child> class> Node:> >def> __init__(>self>,data):> >self>.data>=>data> >self>.left>=>None> >self>.right>=>None> # This function traverse the skewed binary tree and> # stores its nodes pointers in vector nodes[]> def> storeBSTNodes(root,nodes):> > ># Base case> >if> not> root:> >return> > ># Store nodes in Inorder (which is sorted> ># order for BST)> >storeBSTNodes(root.left,nodes)> >nodes.append(root)> >storeBSTNodes(root.right,nodes)> # Recursive function to construct binary tree> def> buildTreeUtil(nodes,start,end):> > ># base case> >if> start>終了:> >return> None> ># Get the middle element and make it root> >mid>=>(start>+>end)>/>/>2> >node>=>nodes[mid]> ># Using index in Inorder traversal, construct> ># left and right subtress> >node.left>=>buildTreeUtil(nodes,start,mid>->1>)> >node.right>=>buildTreeUtil(nodes,mid>+>1>,end)> >return> node> # This functions converts an unbalanced BST to> # a balanced BST> def> buildTree(root):> > ># Store nodes of given BST in sorted order> >nodes>=>[]> >storeBSTNodes(root,nodes)> ># Constructs BST from nodes[]> >n>=>len>(nodes)> >return> buildTreeUtil(nodes,>0>,n>->1>)> # Function to do preorder traversal of tree> def> preOrder(root):> >if> not> root:> >return> >print>(>'{} '>.>format>(root.data),end>=>'')> >preOrder(root.left)> >preOrder(root.right)> # Driver code> if> __name__>=>=>'__main__'>:> ># Constructed skewed binary tree is> ># 10> ># /> ># 8> ># /> ># 7> ># /> ># 6> ># /> ># 5> >root>=> Node(>10>)> >root.left>=> Node(>8>)> >root.left.left>=> Node(>7>)> >root.left.left.left>=> Node(>6>)> >root.left.left.left.left>=> Node(>5>)> >root>=> buildTree(root)> >print>(>'Preorder traversal of balanced BST is :'>)> >preOrder(root)> > # This code has been contributed by Vikash Kumar 37>

Linuxのショートカット

>

>

C#




using> System;> using> System.Collections.Generic;> // C# program to convert a left unbalanced BST to a balanced BST> /* A binary tree node has data, pointer to left child> >and a pointer to right child */> public> class> Node> {> >public> int> data;> >public> Node left, right;> >public> Node(>int> data)> >{> >this>.data = data;> >left = right =>null>;> >}> }> public> class> BinaryTree> {> >public> Node root;> >/* This function traverse the skewed binary tree and> >stores its nodes pointers in vector nodes[] */> >public> virtual> void> storeBSTNodes(Node root, List nodes)> >{> >// Base case> >if> (root ==>null>)> >{> >return>;> >}> >// Store nodes in Inorder (which is sorted> >// order for BST)> >storeBSTNodes(root.left, nodes);> >nodes.Add(root);> >storeBSTNodes(root.right, nodes);> >}> >/* Recursive function to construct binary tree */> >public> virtual> Node buildTreeUtil(List nodes,>int> start,>int> end)> >{> >// base case> >if> (start>終了)> >{> >return> null>;> >}> >/* Get the middle element and make it root */> >int> mid = (start + end) / 2;> >Node node = nodes[mid];> >/* Using index in Inorder traversal, construct> >left and right subtress */> >node.left = buildTreeUtil(nodes, start, mid - 1);> >node.right = buildTreeUtil(nodes, mid + 1, end);> >return> node;> >}> >// This functions converts an unbalanced BST to> >// a balanced BST> >public> virtual> Node buildTree(Node root)> >{> >// Store nodes of given BST in sorted order> >List nodes =>new> List();> >storeBSTNodes(root, nodes);> >// Constructs BST from nodes[]> >int> n = nodes.Count;> >return> buildTreeUtil(nodes, 0, n - 1);> >}> >/* Function to do preorder traversal of tree */> >public> virtual> void> preOrder(Node node)> >{> >if> (node ==>null>)> >{> >return>;> >}> >Console.Write(node.data +>' '>);> >preOrder(node.left);> >preOrder(node.right);> >}> >// Driver program to test the above functions> >public> static> void> Main(>string>[] args)> >{> >/* Constructed skewed binary tree is> >10> >/> >8> >/> >7> >/> >6> >/> >5 */> >BinaryTree tree =>new> BinaryTree();> >tree.root =>new> Node(10);> >tree.root.left =>new> Node(8);> >tree.root.left.left =>new> Node(7);> >tree.root.left.left.left =>new> Node(6);> >tree.root.left.left.left.left =>new> Node(5);> >tree.root = tree.buildTree(tree.root);> >Console.WriteLine(>'Preorder traversal of balanced BST is :'>);> >tree.preOrder(tree.root);> >}> }> >// This code is contributed by Shrikant13>

>

>

JavaScript

果物はいくつありますか




> >// JavaScript program to convert a left> >// unbalanced BST to a balanced BST> > >class Node> >{> >constructor(data) {> >this>.left =>null>;> >this>.right =>null>;> >this>.data = data;> >}> >}> > >let root;> > >/* This function traverse the skewed binary tree and> >stores its nodes pointers in vector nodes[] */> >function> storeBSTNodes(root, nodes)> >{> >// Base case> >if> (root ==>null>)> >return>;> > >// Store nodes in Inorder (which is sorted> >// order for BST)> >storeBSTNodes(root.left, nodes);> >nodes.push(root);> >storeBSTNodes(root.right, nodes);> >}> > >/* Recursive function to construct binary tree */> >function> buildTreeUtil(nodes, start, end)> >{> >// base case> >if> (start>終了)> >return> null>;> > >/* Get the middle element and make it root */> >let mid = parseInt((start + end) / 2, 10);> >let node = nodes[mid];> > >/* Using index in Inorder traversal, construct> >left and right subtress */> >node.left = buildTreeUtil(nodes, start, mid - 1);> >node.right = buildTreeUtil(nodes, mid + 1, end);> > >return> node;> >}> > >// This functions converts an unbalanced BST to> >// a balanced BST> >function> buildTree(root)> >{> >// Store nodes of given BST in sorted order> >let nodes = [];> >storeBSTNodes(root, nodes);> > >// Constructs BST from nodes[]> >let n = nodes.length;> >return> buildTreeUtil(nodes, 0, n - 1);> >}> > >/* Function to do preorder traversal of tree */> >function> preOrder(node)> >{> >if> (node ==>null>)> >return>;> >document.write(node.data +>' '>);> >preOrder(node.left);> >preOrder(node.right);> >}> > >/* Constructed skewed binary tree is> >10> >/> >8> >/> >7> >/> >6> >/> >5 */> >root =>new> Node(10);> >root.left =>new> Node(8);> >root.left.left =>new> Node(7);> >root.left.left.left =>new> Node(6);> >root.left.left.left.left =>new> Node(5);> >root = buildTree(root);> >document.write(>'Preorder traversal of balanced BST is :'> +>''>);> >preOrder(root);> > >

>

>

出力

Preorder traversal of balanced BST is : 7 5 6 8 10>

時間計算量: O(n)、ちょうど木を 2 回横断しているところです。一度は順番に走査し、次にバランスの取れたツリーを構築します。
補助スペース: O(n)、余分なスペースは、ベクトル内の順序トラバーサルのノードを格納するために使用されます。また、再帰呼び出しスタックによって占有される追加スペースは O(h) です。ここで、h はツリーの高さです。

この記事は寄稿されました アディティヤ・ゴエル 。 techcodeview.com が気に入って貢献したい場合は、記事を書いて [email protected] に記事をメールすることもできます。