Python binary tree traversal

Basic overview of binary trees:

      A binary tree is a finite number of elements. If it is empty, it is an empty binary tree, or there is a node called the root node. The left and right child nodes of the binary tree are on both sides of the root node. The binary tree has the following properties:

  1. Every node of a binary tree does not have a node with degree greater than 2
  2. The i-th level of a binary tree has at most 2^{i-1} nodes
  3. A binary tree of depth k has at most 2^k - 1 nodes
  4. In a binary tree, the number of nodes N0 with degree 0 is 1 greater than the number of nodes N2 with degree 2, that is, there is N2 + 1 = N0
#coding:utf-8
'BiTree'

class Node(object):
    'Node Defination'
    def __init__(self,item):
        self.item = item
        self.left = None
        self.right = None

class Tree(object):
    'Bitree Defination'
    def __init__(self):
        self.root = None

    def add(self,item):
        node = Node(item)
        if self.root is None:
            self.root = node

        else:
            q = [self.root]

            while True:
                pop_node = q.pop(0)
                if pop_node.left is None:
                    pop_node.left = node
                    return
                elif pop_node.right is None:
                    pop_node.right = node
                    return
                else:
                    q.append(pop_node.left)
                    q.append(pop_node.right)


    def traverse(self):#level traversal
        if self.root is None:
            return  None
        q = [self.root]
        res = [self.root.item]
        while q != []:
            pop_node = q.pop(0)
            if pop_node.left is not None:
                q.append(pop_node.left)
                res.append(pop_node.left.item)

            if pop_node.right is not None:
                q.append(pop_node.right)
                res.append(pop_node.right.item)
        return  res

    def preorder(self,root): #Preorder traversal
        if root is None:
            return []
        result = [root.item]
        left_item = self.preorder(root.left)
        right_item = self.preorder(root.right)
        return result + left_item + right_item

    def inorder(self,root): #Inorder traversal
        if root is None:
            return []
        result = [root.item]
        left_item = self.inorder(root.left)
        right_item = self.inorder(root.right)
        return left_item + result  + right_item

    def postorder(self,root): #Postorder traversal
        if root is None:
            return []
        result = [root.item]
        left_item = self.postorder(root.left)
        right_item = self.postorder(root.right)
        return left_item  + right_item + result

if __name__=='__main__':
    t = Tree()
    for i in range(10):
        t.add(i)

    print "Order traversal:", t.traverse()
    print "Preorder traversal:",t.preorder(t.root)
    print "Inorder traversal:",t.inorder(t.root)
    print "Postorder traversal:",t.postorder(t.root)
Output result:
Level order traversal: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Preorder traversal: [0, 1, 3, 7, 8, 4, 9, 2, 5, 6]
Inorder traversal: [7, 3, 8, 1, 9, 4, 0, 5, 2, 6]
Post-order traversal: [7, 8, 3, 9, 4, 1, 5, 6, 2, 0]

here for 

if __name__=='__main__':
“Make a script both importable and executable”

It means that the script module you write can be imported into other modules for use, and the module itself can also be executed .

Here is an example to explain:

#test.py
def func():
  print "we are in %s"%__name__
if __name__ == '__main__':
  func()
Output result:
we are in __main__
It means that the content in the if statement is executed, the func() function is called, and now the func function is called in another module
 
 
#testtest
from test import func
func()
Output result:
we are in moudle

That is to say, the content in the if condition is not executed.

Summarize:

       If you directly execute a *.py file, if __name__ == '__main__' in the file is True, it is equivalent to debug the code of this module; if the file is imported from another module (testtest.py) through import, At this time, __name__ is the name of the module (test) instead of __main__. In short, adding if __name__ == '__main__' and adding debugging code to the debugging code can make the debugging code not be executed when the module is called. If When you want to troubleshoot the code of this module, you can directly debug and execute


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593777&siteId=291194637