Abstract data type of binary tree

Abstract data type of binary tree

1. Theoretical analysis

  1. Nodes are the basis of binary trees, and nodes are usually used to store information related to applications.
  2. As a representation of a binary tree, it is also necessary to record the structural information of the binary tree. At least it is necessary to ensure that the parent-child relationship of the node can be checked, for example, the left/right child node can be found from a node

The following is the definition of a basic binary tree abstract data type:

ADT BinTree:   # 一个二叉树抽象数据类型
    BinTree(self, data, left, right)  # 构造操作,创建一个新二叉树
    is_empty(self)    # 判断self是否为一个空二叉树
    num_nodes(self)	  # 求二叉树的结点个数
    data(self)		  # 获取二叉树根存储的数据 
    left(self)		  # 获取二叉树的左子树
    right(self)       # 获取二叉树的右子树
    set_left(self, btree)   # 用btree取代原来的左子树
    set_right(self, btree)  # 用btree取代原来的右子树
    traversal(self)		   # 遍历二叉树中各结点数据的迭代器
    forall(self, op)	   # 对二叉树中的每个结点的数据执行操作op

The basic operation of a binary tree should include the creation of a binary tree. The construction of a binary tree needs to be based on two existing binary trees and a piece of data that you want to save at the root node of the tree. The representation of an empty binary tree is a problem, because the empty binary tree has no information room. In the implementation, it can be represented by a special value, such as None in Python. The actual implementation can also introduce a structure that specifically represents a binary tree, and place the tree node under its jurisdiction.

2. Traverse the binary tree

  1. Every binary tree hasThe only root node, It can be regarded as the unique identifier of this binary tree, and it is the entrance to the processing process based on the tree structure. Starting from the root node, all the information in the tree should be found, and the basis is to find two child nodes from the parent node.

    therefore,In practice, the root node of a commonly used binary tree represents this binary tree, and its left and right subtrees are represented by their root nodes.

  2. Each node in the binary tree may store some data, so it is also a collection type data structure. For any collection structure, there is a problem of processing the data elements stored in it one by one, that is, to traverse the elements and traverse a binary tree. A systematic way to access each node in the binary tree once. This process can be implemented based on the basic operations of the binary tree, and the data in the nodes may be manipulated during the traversal.

  3. Many complex binary tree operations need to be implemented based on traversal, such as finding the parent node of a node. Doing this in a binary tree is like finding the previous node in a singly linked list.

  4. If each node of the binary tree has a unique identifier, the given binary tree uniquely determines its first root sequence, back root sequence and symmetric sequence. ButGiven any kind of traversal sequence of a binary tree, the corresponding binary tree cannot be uniquely determined

  5. If you know the symmetric sequence of a binary tree, and know another traversal sequence (whether it is the first root or the last root sequence), you can uniquely determine the corresponding binary tree

The basic way of traversing a binary tree

  1. Depth-first traversal

    1. First-root order traversal (DLR) -----> First-root order
    2. Middle root sequence traversal (LDR), also known as symmetric sequence --------> middle root sequence
    3. Post-root sequence traversal (LRD) ------> Post-root sequence
  2. Breadth-first traversal, also known as hierarchical order traversal

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-8HY9ODmd-1610347997607)(C:\Users\93623\AppData\Roaming\Typora\typora-user-images\ image-20210111102051381.png)]

First root order traversal: ABDHEICFJKG (starting from the top root, always looking for the bottom root)

Middle root order traversal: DHBEIAJFKCG (starting from the bottom leaf)

Post-root order traversal: HDIEBJKFGCA (starting from the bottom leaf)

Breadth-first traversal: ABCDEFGHIJK

Traverse and search

A binary tree can be regarded as a state space, the root node corresponds to the initial state of the state space, and the adjacency relationship between the connection states of the parent and child nodes.

A binary tree traversal is a search that covers the entire state space. The methods and implementation techniques related to state space search can be transplanted to the binary tree traversal problem as they are, such as: recursive search methods, stack-based non-recursive search (depth-first traversal) , Queue-based breadth-first search corresponds to layer order traversal.

Traversal is a systematic process of node enumeration. In practice, it may not be necessary to check all nodes, and sometimes it needs to end after finding the required information. This kind of search may also be required on a binary tree.

In the search process of the state space, record the connection from one state to another state, and treat it as a link between nodes, and you will find that this search process actually constructs a tree, called a search tree. In other words, the structure formed in this way is not a binary tree but a general tree.

List implementation of binary tree

  1. To put it simply, a binary tree node is a triple, and the elements are the left and right subtrees and the node data. Python's list or tuple can be used to combine these three elements. The difference between the two is only in variability. To implement a non-changeable binary tree, you can use tuple as a combination mechanism.To implement a binary tree that can modify the structure, you should use list.
  2. Design and implementation
    1. The binary tree is a recursive structure, and python's list is also a recursive structure, so it is easy to implement a binary tree based on the list type
    2. Empty tree is represented by None
    3. A non-empty binary tree is represented by a three-element table [d,l,r]

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-hR7ed6WS-1610347997612) (C:\Users\93623\AppData\Roaming\Typora\typora-user-images\ image-20210111135804215.png)]

[A, [B, None,None], [C,[D,[F,None, None],[G, None, None], [E, [I, None, None], [H, None, None]]]]

# 二叉树的实现和操作
def BinTree(data, left=None, right=None):
    return (data, left, right)

def is_empty_BinTree(btree):
    return btree is None

def root(btree):
    return btree[0]

def left(btree):
    return btree[1]

def right(btree):
    return btree[2]

def set_root(btree, data):
    btree[0]=data
 
def set_left(btree, left):
    btree[1] = left
  
def set_right(btree, right):
    btree[2]= right

The constructor BinTree provides default values ​​for the latter two parameters, mainly for ease of use. It means that these two subtrees are empty when the left and right subtrees are not given. Based on the nested call of the constructor, arbitrarily complex binary trees can be made, for example:

t1 = BinTree(2, BinTree(4), BinTree(8))

This is equivalent to writing: t1 = [2,[4, None, None], [8, None, None]]

You can modify any part of the binary tree, for example

set_left(left(t1), BinTree(5))

Among them, the left subtree of the left subtree of t1 is replaced by BinTree(5), so that the value of t1 becomes: [2,[4,[5,None, None]], [8,None, None]]

This is a binary tree with a height of 2, and the number of nesting levels inside the list is equal to the height of the tree

The left subtree of the left subtree of t1 is replaced by BinTree(5), so that the value of t1 becomes: [2,[4,[5,None, None]], [8,None, None]]

This is a binary tree with a height of 2, and the number of nesting levels inside the list is equal to the height of the tree

Guess you like

Origin blog.csdn.net/weixin_46129834/article/details/112469526