Implement binary tree using Python

Source: Internet
Author: User

Recently, I am interested in Python. I have basically read the simple Python Tutorials that are widely circulated on the Internet, and I have tried it myself. I feel that python is indeed an elegant language.

I saw a python Binary Tree Code on the Internet a few days ago. I also learned to write the code and added the Traversal method for the binary tree.

The Code is as follows:

I wonder if the format is messy.

#! /Usr/bin/Python # * _ * coding: UTF-8 * _ * import osclass node: def _ init _ (self, value = none, parent = none, left = none, right = none): Self. value = value self. parent = parent self. left = left self. right = rightdef wideorder (n): If n = none: Return L = List () L. append (n) while Len (l)> 0: TMP = L. pop (0) print TMP. value if TMP. left! = None: L. append (TMP. Left) If TMP. Right! = None: l. append (TMP. right) def preorder (n): If n = none: Return print n. value preorder (N. left) preorder (N. right) def midorder (n): If n = none: Return midorder (N. left) print n. value midorder (N. right) def aftorder (n): If n = none: Return aftorder (N. left) aftorder (N. right) print n. valuedef getdepth (n): If n = none: Return 0; n_left = getdepth (N. left) n_right = getdepth (N. right) If n_left> = n_right: Return n_left + 1 else: Return n_right + 1 class tree: def _ init _ (Self): Self. root = none self. size = 0 self. depth = 0 def insert (self, n): If n = none: return if self. root = none: Self. root = n self. size = self. size + 1 self. depth = 1 return TMP = self. root tmpdepth = 1 While true: tmpdepth = tmpdepth + 1 if n. value <= TMP. value: If TMP. left = none: N. parent = TMP. left = n self. size = self. size + 1 self. depth = max (self. depth, tmpdepth) Break else: TMP = TMP. left Elif n. value> TMP. value: If TMP. right = none: N. parent = TMP. right = n self. size = self. size + 1 self. depth = max (self. depth, tmpdepth) Break else: TMP = TMP. right def find (self, V): If v = none or self. root = none: Print 'none' return TMP = self. root while true: If v = TMP. value: Print 'Find it', V break Elif v <TMP. value: If TMP. left = none: Print 'none' break else: TMP = TMP. left else: If TMP. right = none: Print 'none' break else: TMP = TMP. righttree = tree () tree. insert (node (5) tree. insert (node (2) tree. insert (node (-3) tree. insert (node (4) tree. insert (node (3) tree. insert (node (6) tree. insert (node (-1) tree. insert (node (7) print U' first traverse 'preorder (tree. root) print U' traverse 'middorder (tree. root) print U' and traverse 'aftorder (tree. root) print u'breadth traversal 'wideorder (tree. root) print 'classic depth ', getdepth (tree. root) print 'tree size % d depth % d' % (tree. size, tree. depth)

Python's rich modules and script features are suitable for rapid development, and it is also convenient to demonstrate some 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.