Python Binary Search Tree and two-way linked list conversion implementation method, python binary

Source: Internet
Author: User

Python Binary Search Tree and two-way linked list conversion implementation method, python binary

This article describes how to implement the Python Binary Search Tree and two-way linked list. We will share this with you for your reference. The details are as follows:

# Encoding = utf8''' question: enter a binary search tree and convert it into a sorted two-way linked list. It is required that no new node can be created, and only the point of the node pointer in the tree can be adjusted. '''Class BinaryTreeNode (): def _ init _ (self, value, left = None, right = None): self. value = value self. left = left self. right = rightdef create_a_tree (): node_4 = BinaryTreeNode (4) node_8 = BinaryTreeNode (8) node_6 = BinaryTreeNode (6, node_4, node_8) node_12 = BinaryTreeNode (12) node_16 = BinaryTreeNode (16) node_14 = BinaryTreeNode (14, node_12, node_16) node_10 = BinaryTreeNode (10, node_6, node_14) return node_10def print_a_tree (root): if root is None: return print_a_tree (root. left) print root. value, '', print_a_tree (root. right) def print_a_resource_list (head): print 'linked _ list: 'while head is not None: print head. value, '', head = head. right print ''def create_resource_list (root): ''' construct a two-way linked list of the tree. Return the leftmost node and rightmost node pointer ''' if root is None: return (None, None) # recursively construct the two-way linked list (l_1, r_1) = create_resource_list (root. left) left_most = l_1 if l_1 is not None else root (l_2, r_2) = create_1__list (root. right) right_most = r_2 if r_2 is not None else root # connect the left and right subtree with the root. left = r_1 if r_1 is not None: r_1.right = root. right = l_2 if l_2 is not None: l_2.left = root # as a two-way linked list, return to the leftmost node on the upper layer and rightmost node pointer return (left_most, right_most) if _ name _ = '_ main _': tree_1 = create_a_tree () print_a_tree (tree_1) (left_most, right_most) = create_pai_list (tree_1) alias (left_most) pass

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.