Data structure - binary tree (1) as well as the preamble, in sequence, after traversal (Python implementation)

Last article we introduced the concept of the tree, and today we introduce a special tree - binary tree, binary tree very extensive, there are many features. We January to introduce today for everyone.

Binary Tree

As the name suggests, the binary tree is only two nodes of the tree, two nodes are the nodes left and right node, with special emphasis on, even if only one child is left should distinguish it right node or nodes.

There is generally a common binary tree binary tree, complete binary tree, full binary tree, binary tree trail, Huffman tree, binary sort tree, balanced binary tree, red-black tree, B tree so many types. We in this article a brief general binary tree complete binary tree and binary tree is full.

General binary tree

Very simple, as long as no more than two child nodes of the tree is a binary tree. A long way:

Binary Tree

Full Binary Tree

A full binary tree on the basis of the binary tree general requirements in addition to the last layer of nodes, each node must have two child nodes.

Full Binary Tree

Full Nimata 树

Requires complete binary tree from the first layer to the second layer is composed of the inverse of a full binary tree, nodes to meet the last layer arranged from left to right.

Full Nimata 树

Well, with regard to the concept of a binary tree, we introduced here, let's introduce the preamble binary tree, in sequence, after traversal.

Until then, let's create a binary tree:

class BinaryTree:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def get(self):
        return self.data

    def getLeft(self):
        return self.left

    def getRight(self):
        return self.right

    def setLeft(self, node):
        self.left = node

    def setRight(self, node):
        self.right = node

Well, here we have a good definition of a binary tree class, and add to it a little way and then we instantiate a binary tree:

binaryTree = BinaryTree(0)
binaryTree.setLeft(BinaryTree(1))
binaryTree.setRight(BinaryTree(2))
binaryTree.getLeft().setLeft(BinaryTree(3))
binaryTree.getLeft().setRight(BinaryTree(4))
binaryTree.getRight().setLeft(BinaryTree(5))
binaryTree.getRight().setRight(BinaryTree(6))

Examples of the binary tree is a good long look like this:

Preorder traversal

Next, we performed a preorder traversal of this tree. Prior to this, we explain what is preorder traversal.

Earlier we introduced a depth-first traversal and breadth-first traversal of the tree, not repeat them here.

Order preorder traversal of traversal of the tree is the first parent node, and then traverse the tree left node and right node to traverse the tree, and so on.

For our binary well defined above, its preorder traversal result: 0 -> 1 -> 3 -> 4 -> 2 -> 5 -> 6

For the preamble, in sequence, after traversal, the recursive approach is very convenient. Here we will use recursion to achieve this:

def preorderTraversal(now, result=[]):
    if now == None:
        return result
    result.append(now.data)
    preorderTraversal(now.left, result)
    preorderTraversal(now.right, result)
    return result


print(preorderTraversal(binaryTree))

The results: [0, 1, 3, 4, 2, 5, 6], and is not the result we manually traverse the same as before it.

Preorder

Inorder traversal order is: left node of the first tree traversal, then traverse the tree a parent node, right node then traversing the tree.

For our binary tree created above, it preorder result: 3 -> 1 -> 4 -> 0 -> 5 -> 2 -> 6

When the pre-order traversing the parent node is to traverse, so that result.append(now.data), in traversing the front left node and a right node.

Preorder traversal of the first node left, it result.append(now.data)is necessary in the rear left node traversal, the traversal of the front right node.

def intermediateTraversal(now, result=[]):
    if now == None:
        return result
    intermediateTraversal(now.left, result)
    result.append(now.data)
    intermediateTraversal(now.right, result)
    return result


print(intermediateTraversal(binaryTree))

Results of the:[3, 1, 4, 0, 5, 2, 6]

Postorder

Postorder order: the left node of the first tree traversal, then traverse right node of the tree, then the parent node of the tree traversal.

For our binary tree created above, it postorder traversal result: 3 -> 4 -> 1--> 5 -> 6 -> 2 -> 0

Corresponding recursive equation is:

def postorderTraversal(now, result=[]):
    if now == None:
        return
    postorderTraversal(now.left, result)
    postorderTraversal(now.right, result)
    result.append(now.data)
    return result

print(postorderTraversal(binaryTree))

Results of the:[3, 4, 1, 5, 6, 2, 0]

Well, today the three of us on the binary tree traversal sequence described here, and then we will then introduce more binary type and application, remember to pay attention to my article. About Us preorder traversal, you have other methods to achieve it, leave a message to tell us.

Guess you like

Origin www.cnblogs.com/dongyangblog/p/11210820.html