The binary tree knows the preface or the sequence in order to find another arrangement.

Source: Internet
Author: User


Traversal of binary tree:

Pre-sequence Traversal: root node-> Zoozi tree-> right subtree

Sequence traversal: Zoozi tree-> root node-> right subtree

Subsequent traversal: Zoozi tree-> right subtree-> root node

Ask for three kinds of traversal of the following tree:

Pre-sequence traversal: ABDEFGC

In-sequence traversal: DEBGFAC

Subsequent traversal: EDGFBCA detailed two-fork tree operation can look at the article I wrote earlier

Two fork Tree Java

Known pre-sequence, middle-order traversal, to find the subsequent traversal

the first value of the forward traversal is the root node, then in the middle sequence traversal to find this value, then the left part of this value is the current binary tree part of the left subtree sequence traversal results, the right part of this value is the current binary tree of the right subtree part of the first-order traversal results.

as shown above: the first sequence A is the root node, and then see the order inside of a, because the middle order is (left root right), so a left DEBGF for the Zoozi tree, right C for the right subtree

so recursive call Zuozi: Zuozi the sequence traversal of the first sequence traversal is: BDEFG, DEBGF

then recursively call the right subtree: C, c

The process of recursion:

1 Determine the root, determine the left subtree, and determine the right subtree.

2 recursion in China.

3 recursion in the right subtree.

4 Print the current root. Known in order and sequence, to find the first order traversal

The last node is the root node after the subsequent traversal.

The process of recursion:

1 Determine the root, determine the left subtree, and determine the right subtree.

2 recursion in China.

3 recursion in the right subtree.

4 Print the current root.



If it is an online written test, use the following code directly, very useful


#-*-coding:utf-8-*-#!/usr/bin/python #Filename: btreenode.py ' ' Created on 2011-4-11 @author: Boyce @contact: Boyce.y
    Wr@gmail.com @version: 1.0 ' class Btree: ' represent a No in a binary tree. ' Def __init__ (self, c= '/0 ', L=none, r=none): ' Initializes the node ' s data ' SE
        LF.E = c Self.left = L Self.right = r def preordertraverse (BT): ' Returns the sequence traversal result ' if BT: Return '%s%s%s '% (BT.E, Preordertraverse (Bt.left), Preordertraverse (bt.right)) return ' Def inordertraverse (BT ): ' Returns the sequence traversal result ' if Bt:return '%s%s%s '% (Inordertraverse (bt.left), BT.E, Inordertraverse (BT.R
ight)) return "; def postordertraverse (BT): ' Returns the subsequent traversal result ' if Bt:return '%s%s%s '% (Postordertraverse (bt.left),
    Postordertraverse (bt.right), BT.E) return ' Def printbtree (BT, depth): ' Recursively print this binary tree, * indicates that the node is null ' ' If not bt:ch= ' * ' else:ch = bt.e ' ' #ch = (lambda x: (x and X.E) or ' * ') (BT) ch = bt.e If bt Else ' * ' if (d Epth > 0): print '%s%s%s '% ((depth-1) * ', '--', ch ') Else:print ch if not bt:re
    Turn printbtree (bt.left, depth + 1) printbtree (bt.right, depth + 1) def buildbtreefromprein (Preo, ino): " Refactoring the Binary tree ' if (preo = = ' or Ino = = ') According to the sequence and sequence traversal results: return None pos = Ino.find (preo[0)) if (POS & Lt 0): Return None return Btree (Preo[0], Buildbtreefromprein (Preo[1:pos + 1], ino[0:pos), Buildbtreefrom
    Prein (Preo[pos + 1:], Ino[pos + 1:]) #return nd def buildbtreefrominpost (Ino, PO): "The binary tree is reconstructed according to the sequence and sequential traversal results    
    ' if (ino = = ' or po = = '): return None pos = Ino.find (Po[-1]) if (POS < 0): return None 
    
Return Btree (Po[-1], Buildbtreefrominpost (Ino[0:pos], Po[0:pos)), Buildbtreefrominpost (Ino[pos + 1:], po[pos:-1]) if __name__ = = ' __main__ ': Preo = ' abdgcefh ' ino = ' dgbaechf ' po = ' GDBEHFCA ' BT = Buildbtreefromprein (Preo, INO) pr int ' Build from preorder & inorder ' print ' preorder:%s '% (Preordertraverse (BT)) print ' inorder:%s '% (Inord
    Ertraverse (BT)) print ' Postorder:%s '% (Postordertraverse (BT)) print ' The btree is (* means no such a node): ' Printbtree (BT, 0) bt = buildbtreefrominpost (Ino, PO) print ' Build from Inorder & postorder ' print ' Preord Er:%s '% (Preordertraverse (BT)) print ' inorder:%s '% (Inordertraverse (BT)) print ' Postorder:%s '% (Postordertra
 Verse (BT)) print ' The btree is (* means no such a node): ' Printbtree (BT, 0)


Reference: http://www.cnblogs.com/fzhe/archive/2013/01/07/2849040.html

http://blog.csdn.net/hinyunsin/article/details/6316185

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.