Ordered single-chain table to BST tree + time complexity requirements O (n)

Source: Internet
Author: User

Ordered single-chain table to BST tree + time complexity requirements O (n)
Problem description

There are usually multiple methods to convert an ordered array or linked list into a relatively balanced BST tree.
1. when an ordered array A is converted into a bst, convert the element A [m] in the middle of the array to the root, and then recursively convert it to A [1 .. m-1], A [m + 1 .. n. Time Complexity T (n) = 2 * T (n/2) + O (1), T (n) = O (n)
2. For ordered linked lists, you can also perform this operation based on the above method. However, to get A [m], we need to traverse the n/2 length array, so we need additional O (n) Time to get A [m]. Therefore, the time complexity is T (n) = 2 * T (n/2) + O (n), and T (n) = n log (n) is obtained ).
OK. The problem arises. If we want to convert the ordered linked list to BST, it will also be O (n) time, what should we do?

Solution

For the issue of ordered linked list, you can refer to the recursive solution. The difference is that when performing left half-side recursion, the head node of the single-chain table is updated at the same time.
When LinkNode * head is input as a parameter, after the conversion is complete, the head will be changed to the End Node of the single-chain table-> next, that is, NULL. When a LinkNode node is processed each time, the head needs to shift one node to the right. The details are as follows:

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; // node Structure of a single-chain table struct LinkNode {int value; LinkNode * next; LinkNode (int v): value (v), next (NULL ){}}; // BST node Structure struct BstNode {int value; BstNode * left, * right; BstNode (int v): value (v), left (NULL), right (NULL) {}}; class Solution {public: void solve () {// test case int arr [] = {1, 2, 3, 4, 5, 6, 7}; LinkNode * head = NULL, * p = NULL; for (int I = 0; I <sizeof (arr)/sizeof (int); I ++) {if (head = NULL) head = p = new LinkNode (arr [I]); else p-> next = new LinkNode (arr [I]), p = p-> next ;} bstNode * root = convert (head, 0, sizeof (arr)/sizeof (int)-1); printBstNode (root); printf ("\ n ");} // move ahead the head pointer when carrying out the inorder traversal. // in this way, we avoid n * log (n) time complexity BstNode * convert (LinkNode * & head, int s, int e) {BstNode * root = NULL; if (s> e) {} else if (s = e) {root = new BstNode (head-> value); head = head-> next ;} else {int m = s + (e-s)/2; BstNode * left = convert (head, s m-1); root = new BstNode (head-> value ); head = head-> next; BstNode * right = convert (head, m + 1, e); root-> left = left; root-> right = right ;} return root;} // for printing out the content of a BstNode tree, inorder printing void printBstNode (BstNode * root) {if (root = NULL) return; printBstNode (root-> left); printf ("% d", root-> value); printBstNode (root-> right) ;}}; int main () {Solution; solution. solve ();}
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.