Statistics and conversion Examples of binary tree of Python data structure

Source: Internet
Author: User
First, get the depth of the binary tree

Is the final level of the binary tree, such as:



Implementation code:

The code is as follows:


def getheight (self):
"Get Binary Tree Depth"
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. statistics of Leaves

A leaf is a binary tree node with a left pointer and a right pointer pointing to an empty node, respectively.

The code is as follows:


def getleafcount (self):
"Get binary tree leaves number"
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

Third, the branch node of the statistic leaf

Other nodes relative to leaf nodes left and right pointers point to other nodes

The code is as follows:


def getbranchcount (self):
"Get Binary tree branch node number"
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)

Four or two fork tree around tree swap

The code is as follows:


def replacelem (self):
"Binary tree all nodes of the left and right sub-tree Exchange"
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)

All of these methods and operations are recursive. In fact, the definition of binary tree is also a kind of recursion. Attach the final complete code:

The 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 (PRE-ORDER,NLR) traversal '
If TreeNode is 0:
Return
Print Treenode.data
Self.preorder (Treenode.left)
Self.preorder (Treenode.right)

def inorder (Self, TreeNode):
' Middle order (IN-ORDER,LNR ')
If TreeNode is 0:
Return
Self.inorder (Treenode.left)
Print Treenode.data
Self.inorder (Treenode.right)

def postorder (Self, TreeNode):
' Sequential (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 (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):
' Middle order (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-shipment (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-delivery (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):
"Get Binary Tree Depth"
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):
"Get binary tree leaves number"
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):
"Get Binary tree branch node number"
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):
"Binary tree all nodes of the left and right sub-tree Exchange"
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 ' '

Generated two-fork tree

------------------------
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.