Sequential linked list and balanced binary tree for Algorithms

Source: Internet
Author: User

Description:

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.


Given an ordered linked list, a balanced binary search tree is required.


Resolution: The structure of the forward traversal of the Binary Search Tree is a binary search tree. To balance the structure of the final binary search tree as much as possible, that is to say, we only need to make the number of nodes on both sides as even as possible. (Note that the uniformity here is a recursive concept, that is to say, the number of left and right Subtrees of each node should be as even as possible ). We first thought of binary.


The following code came into being:


Treenode * sortedlisttobst (listnode * head) {If (Head = nullptr) Return (treenode *) nullptr; listnode * fast = head-> next; listnode * slow = head, * Prev = nullptr; while (Fast & fast-> next) {Prev = slow; fast = fast-> next; slow = slow-> next ;} // have left tree or not if (prev) Prev-> next = nullptr; // if the next value is null, the structure of the linked list is changed. Else head = nullptr; treenode * node = new treenode (slow-> Val); fast = slow-> next; slow-> next = nullptr; treenode * l = sortedlisttobst (head); treenode * r = sortedlisttobst (FAST); node-> left = L; node-> right = r; return node ;}

Although the above Code can complete the balance binary search tree we need, the above Code has a fatal drawback: Memory leakage,

In the above Code, the original linked list node is completely invisible during execution. That is to say, if the node is obtained through the new operator, after the function is executed, pointers of most nodes are no longer accessible,Memory leakage.


So,We cannot use the form of split linked list to build a balanced binary search tree,We cannot change the structure of the linked list. What should we do.What we need to do is to record the head knots and lengths of the current linked list.. The implementation is as follows:


Class solution {// although the previous code is AC, it will change the list structure, which will lead to memory leakage, so all the previous code is wrong. PRIVATE: int Len (listnode * head) {int ret = 0; while (head) {++ ret; head = head-> next;} return ret ;} treenode * helper (listnode * head, int Len) {If (Head = nullptr | Len <= 0) return nullptr; listnode * cur = head; int mid = (LEN + 1)/2-1; int TMP = mid; while (TMP) {cur = cur-> next; -- TMP ;} treenode * root = new treenode (cur-> Val); root-> left = helper (Head, mid); root-> right = helper (cur-> next, len-mid-1 ); return root;} public: treenode * sortedlisttobst (listnode * head) {const int n = Len (head); treenode * root = helper (Head, N ); return root ;}};

Therefore, the memory leakage problem in the above Code is eliminated, because in the whole process, we have not changed the point of any node in the linked list.


Here, the most important thing to mention is the destruction of linked lists. When a linked list is destroyed, either the return value, or the input parameter is a reference to the linked list, or a pointer to the pointer. Otherwise, memory risks may occur:


That is to say, the code for deleting a linked list is:

Void destory (listnode * & head) {listnode * TMP = nullptr; while (head) {TMP = head-> next; Delete head; head = nullptr; // The safe way to delete the pointer is to assign a null head = TMP;} head = nullptr after the delete operation; // Of course, the header node is no exception, if the input parameter is not a pointer or reference, the head still points to a certain area of the memory before the next value is assigned .}





Sequential linked list and balanced binary tree for Algorithms

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.