Python Two fork Tree

Source: Internet
Author: User

Property 1: There are at most 2^ (i-1) nodes on the first layer of the binary tree (i>0).
Property 2: Two fork tree with a depth of k at most 2^k-1 nodes (k>0)
Property 3: For any binary tree, if its leaf node is N0, and the number of nodes with a degree of 2 is N2, then n0=n2+1;
Property 4: The depth of a complete binary tree with n nodes must be log2 (n+1)
Property 5: For a complete binary tree, if numbered from top to bottom, from left to right, the node numbered I, the left child number must be 2i, the right child number must be 2i+1, the parents of the number must be I/2 (I=1 when the root, except)

(1) Complete binary tree-if set two fork tree height is H, in addition to the H layer, the other layers (1~h-1) of the nodes are reached the maximum number, the H layer has leaf nodes, and leaf nodes are left to right sequentially arranged, this is a complete binary tree.

(2) Full two fork tree--except the leaf node each node has left and right cotyledons and leaf nodes are at the bottom of the two fork tree.

classNode (object):"""Node Class"""    def __init__(Self, elem=-1, Lchild=none, rchild=None): Self.elem=Elem Self.lchild=lchild Self.rchild=RchildclassTree (object):"""Tree Class"""    def __init__(Self, root=None): Self.root=RootdefAdd (Self, elem):"""Add a node to a tree"""node=Node (elem)#if the tree is empty, assign a value to the root node        ifSelf.root = =None:self.root=nodeElse: Queue=[] Queue.append (Self.root)#hierarchical traversal of existing nodes             whileQueue:#the first element of a popup queueCur =queue.pop (0)ifCur.lchild = =None:cur.lchild=nodereturn                elifCur.rchild = =None:cur.rchild=nodereturn                Else:                    #if the left and right subtrees are not empty, join the queue to continue judgingqueue.append (cur.lchild) queue.append (cur.rchild)
Traversal of a binary tree

The two important traversal modes of a tree are depth-first traversal and breadth-first traversal, and depth-first general recursion, and breadth-first general queue. Most of the algorithms that can be implemented recursively can also be implemented with stacks.

Depth-First traversal

There are three important methods for deep traversal. These three methods are often used to access the nodes of the tree, and they differ in the order in which each node is accessed. These three types of traversal are called First Order traversal (preorder), Middle sequence Traversal (inorder), and post-order traversal (postorder).

    • In order traversal, we first access the root node, then recursively use the first-order traversal to access the left subtree, and recursively use the first-order traversal to access the right sub-tree

Right subtree, left dial hand tree, root node

def preorder (self, root):       """ recursive implementation of first order traversal """      if root = = None          :return      print  root.elem      Self.preorder ( Root.lchild)      Self.preorder (root.rchild)
    • In the middle sequence traversal, we recursively use the middle order traversal to access the left subtree, then access the root node, and then recursively use the middle order traversal to access the right subtree.

Left dial hand Tree----The right subtree, root node

def inorder (self, root):       """ recursive implementation of sequential traversal """      if root = = None          :return      self.inorder (root.lchild      )  Print  root.elem      self.inorder (root.rchild)
    • After the sequential traversal in the post-operation traversal, we first recursively use the post-traversal to access the Saozi right subtree, the last access to the root node

root node, right subtree, left dial hand tree

def Postorder (self, root):       """ recursive implementation of subsequent traversal """      if root = = None          :return      self.postorder (root.lchild)      Self.postorder (root.rchild)      print Root.elem

Three sequence of traversal:

Results:
First Order: A B c d e F g h
Middle order: b d C e a f H g
Post-post: D e c B H G f a

Breadth-first traversal (hierarchical traversal)

From the root of the tree, from top to bottom, from left to right, traverse the entire tree node

defBreadth_travel (self, root):"""using queues to implement hierarchical traversal of trees"""        ifRoot = =None:returnQueue=[] queue.append (Root) whileQueue:node=queue.pop (0)PrintNode.elem,ifNode.lchild! =None:queue.append (node.lchild)ifNode.rchild! =None:queue.append (node.rchild)

Python Two fork Tree

Related Article

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.