In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to create a balanced binary tree in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create a balanced binary tree by python.
1. The core of generating balanced tree is partial_tree method.
It takes a sequence and a number as parameters and returns a sequence recursively. The first is a structural tree, and the second is an element that is not included in the book.
2. The overall idea of the implementation is that each incoming sequence is divided into the left part, the vertex and the right part, until it can not be further split, and then returned layer by layer, and finally combined into a balanced binary tree.
Example
List_to_tree method converts ordered list into balanced binary tree A binary tree is divided into tree vertex, left subtree and right subtree, in which the value of the left subtree is smaller than that of the top node. The value of the right subtree is larger than the vertex of the tree. "" def make_tree (entry, left, right): # the method of creating a tree return (entry, left) Right) def entry (tree): # get the vertices of the tree return tree [0] def left_branch (tree): # get the left subtree return tree [1] def right_branch (tree): # get the right subtree return tree [2] def list_to_tree (elements): return partial_tree (elements, len (elements)) [0] def partial_tree (elts, n): if n = 0: return (() Elts) else: left_size = (n-1) 2 left_result = partial_tree (elts, left_size) left_tree = left_result [0] non_left_elts = left_result [1] right_size = n-(left_size + 1) this_entry = non_left_elts [0] right_result = partial_tree (non_left_ arts [1:] Right_size) right_tree = right_result [0] remaing_elts = right_result [1] # print ("entry", this_entry) # print ("left_tree", left_tree) # print ("right_tree", right_tree) return (make_tree (this_entry, left_tree, right_tree) Remaing_elts) if _ _ name__ = = "_ main__": tree = list_to_tree ((1,3,5,7,9)) print ("generated balanced binary tree is:", tree) print ("vertex of the tree:", entry (tree)) print ("left subtree:", left_branch (tree)) print ("right subtree:", right_branch (tree)) I believe you have a deeper understanding of "how to create a balanced binary tree in python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.