Python Data Structure-Example of Binary Tree statistics and conversion

Source: Internet
Author: User

1. Obtain the depth of a binary tree

Is the last level of the Binary Tree, such:



Implementation Code:
Copy codeThe Code is as follows:
Def getheight (self ):
'''Retrieve Binary Tree extension '''
Return self. _ get_tree_height (self. root)

Def _ get_tree_height (self, root ):
If root is 0:
Return 0
If root. left is 0 and root. right is 0:
Return 1
Else:
Left = self. _ get_tree_height (root. left)
Right = self. _ get_tree_height (root. right)
If left <right:
Return right + 1
Else:
Return left + 1

Ii. Leaf statistics

The leaf is the left pointer and right pointer of the node of the Binary Tree pointing to the empty node respectively.
Copy codeThe Code is as follows:
Def getleafcount (self ):
'''Retrieve the leaf count of a binary tree '''
Return self. _ count_leaf_node (self. root)

Def _ count_leaf_node (self, root ):
Res = 0
If root is 0:
Return res
If root. left is 0 and root. right is 0:
Res + = 1
Return res
If root. left is not 0:
Res + = self. _ count_leaf_node (root. left)
If root. right is not 0:
Res + = self. _ count_leaf_node (root. right)
Return res

3. Count branch nodes of Leaves

The left and right pointers of other nodes relative to the leaf node point to other nodes.

Copy codeThe Code is as follows:
Def getbranchcount (self ):
'''Retrieve the number of binary tree branch node '''
Return self. _ get_branch_node (self. root)

Def _ get_branch_node (self, root ):
If root is 0:
Return 0
If root. left is 0 and root. right is 0:
Return 0
Else:
Return 1 + self. _ get_branch_node (root. left) + self. _ get_branch_node (root. right)

Iv. Tree interchange between left and right trees

Copy codeThe Code is as follows:
Def replacelem (self ):
'''Switch between left and right subtree of all nodes of a binary tree '''
Self. _ replace_element (self. root)

Def _ replace_element (self, root ):
If root is 0:
Return
Root. left, root. right = root. right, root. left
Self. _ replace_element (root. left)
Self. _ replace_element (root. right)

These methods and operations use recursion. In fact, binary tree definition is also a kind of recursion. The final complete code is attached:

Copy codeThe Code is as follows:
#-*-Coding: utf-8 -*-


Class TreeNode (object ):

Def _ init _ (self, left = 0, right = 0, data = 0 ):
Self. left = left
Self. right = right
Self. data = data


Class BinaryTree (object ):

Def _ init _ (self, root = 0 ):
Self. root = root

Def is_empty (self ):
If self. root is 0:
Return True
Else:
Return False

Def create (self ):
Temp = input ('enter a value :')
If temp is '#':
Return 0
Treenode = TreeNode (data = temp)
If self. root is 0:
Self. root = treenode

Treenode. left = self. create ()
Treenode. right = self. create ()

Def preorder (self, treenode ):
'Pre-order (NLR) traversal'
If treenode is 0:
Return
Print treenode. data
Self. preorder (treenode. left)
Self. preorder (treenode. right)

Def inorder (self, treenode ):
'In-order, LNR'
If treenode is 0:
Return
Self. inorder (treenode. left)
Print treenode. data
Self. inorder (treenode. right)

Def postorder (self, treenode ):
'Post-order (LRN) traversal'
If treenode is 0:
Return
Self. postorder (treenode. left)
Self. postorder (treenode. right)
Print treenode. data

Def preorders (self, treenode ):
'Pre-order (NLR) Non-recursive traversal'
Stack = []
While treenode or stack:
If treenode is not 0:
Print treenode. data
Stack. append (treenode)
Treenode = treenode. left
Else:
Treenode = stack. pop ()
Treenode = treenode. right

Def inorders (self, treenode ):
'In-order (LNR) Non-recursive traversal'
Stack = []
While treenode or stack:
If treenode:
Stack. append (treenode)
Treenode = treenode. left
Else:
Treenode = stack. pop ()
Print treenode. data
Treenode = treenode. right

Def postorders (self, treenode ):
'Post-order (LRN) Non-recursive traversal'
Stack = []
Pre = 0
While treenode or stack:
If treenode:
Stack. append (treenode)
Treenode = treenode. left
Elif stack [-1]. right! = Pre:
Treenode = stack [-1]. right
Pre = 0
Else:
Pre = stack. pop ()
Print pre. data

# Def postorders (self, treenode ):
# 'Post-order (LRN) Non-recursive traversal'
# Stack = []
# Queue = []
# Queue. append (treenode)
# While queue:
# Treenode = queue. pop ()
# If treenode. left:
# Queue. append (treenode. left)
# If treenode. right:
# Queue. append (treenode. right)
# Stack. append (treenode)
# While stack:
# Print stack. pop (). data

Def levelorders (self, treenode ):
'Sequence (post-order, LRN) Non-recursive traversal'
From collections import deque
If not treenode:
Return
Q = deque ([treenode])
While q:
Treenode = q. popleft ()
Print treenode. data
If treenode. left:
Q. append (treenode. left)
If treenode. right:
Q. append (treenode. right)

Def getheight (self ):
'''Retrieve Binary Tree extension '''
Return self. _ get_tree_height (self. root)

Def _ get_tree_height (self, root ):
If root is 0:
Return 0
If root. left is 0 and root. right is 0:
Return 1
Else:
Left = self. _ get_tree_height (root. left)
Right = self. _ get_tree_height (root. right)
If left <right:
Return right + 1
Else:
Return left + 1

Def getleafcount (self ):
'''Retrieve the leaf count of a binary tree '''
Return self. _ count_leaf_node (self. root)

Def _ count_leaf_node (self, root ):
Res = 0
If root is 0:
Return res
If root. left is 0 and root. right is 0:
Res + = 1
Return res
If root. left is not 0:
Res + = self. _ count_leaf_node (root. left)
If root. right is not 0:
Res + = self. _ count_leaf_node (root. right)
Return res

Def getbranchcount (self ):
'''Retrieve the number of binary tree branch node '''
Return self. _ get_branch_node (self. root)

Def _ get_branch_node (self, root ):
If root is 0:
Return 0
If root. left is 0 and root. right is 0:
Return 0
Else:
Return 1 + self. _ get_branch_node (root. left) + self. _ get_branch_node (root. right)

Def replacelem (self ):
'''Switch between left and right subtree of all nodes of a binary tree '''
Self. _ replace_element (self. root)

Def _ replace_element (self, root ):
If root is 0:
Return
Root. left, root. right = root. right, root. left
Self. _ replace_element (root. left)
Self. _ replace_element (root. right)

Node1 = TreeNode (data = 1)
Node2 = TreeNode (node1, 0, 2)
Node3 = TreeNode (data = 3)
Node4 = TreeNode (data = 4)
Node5 = TreeNode (node3, node4, 5)
Node6 = TreeNode (node2, node5, 6)
Node7 = TreeNode (node6, 0, 7)
Node8 = TreeNode (data = 8)
Root = TreeNode (node7, node8, 'root ')


Bt = BinaryTree (root)

Print u '''

Binary Tree generated

------------------------
Root
7 8
6
2 5
1 3 4

-------------------------

'''

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.