The concept and terminology of tree

Keywords: data structure

concept
Tree is an abstract data type or a data structure that implements this abstract data type, which is used to simulate a data set with the nature of tree structure. It is a set with hierarchical relationship composed of n (n > = 1) finite nodes. It is called "tree" because it looks like an upside down tree, that is, it has roots up and leaves down.

term

  • The degree of a node. The number of subtrees contained in a node becomes the degree of the node
  • In a tree, the degree of the largest node becomes the degree of the number
  • Node with zero degree of leaf node or end parent node
  • Parent node or child node if a node contains child nodes, the node becomes the parent of its child nodes
  • Child node or child node the root node of the subtree contained in a node becomes the child node of the node
  • Sibling nodes nodes with the same parent node are called sibling nodes
  • The level of a node is defined from the root. The root is the first level, and the child node of the root is the second level, and so on.
  • The height or depth of a tree the maximum level of nodes in a tree
  • The nodes of the parent node in the same layer are cousins to each other
  • The ancestor of the child node is from the root to all nodes on the branch through which the node passes
  • Any node in the subtree with a node as the root is called the descendant of the node.

Concept of binary tree
A binary tree is a tree structure in which each node has at most two subtrees. Subtrees are usually called "left subtree" and "right subtree".

Binary tree code implementation

class Node(object):
    """node"""
    def __init__(self,item):
        self.elem = item
        self.lchild = None
        self.rchild = None


class Tree(object):
    """Binary tree"""
    def __init__(self):
        self.root = None

    def add(self,item):
        node = Node(item)
        if self.root is None:
            self.root = node
            return
        queue = [self.root]
        while queue:
            cur_node = queue.pop(0)
            if cur_node.lchild is None:
                cur_node.lchild = node
                return
            else:
                queue.append(cur_node.lchild)
            if cur_node.rchild is None:
                cur_node.rchild = node
                return
            else:
                queue.append(cur_node.rchild)

    def breadth_travel(self):
        """Breadth traversal"""
        if self.root is None:
            return
        queue = [self.root]
        while queue:
            cur_node = queue.pop(0)
            print(cur_node.elem,end=" ")
            if cur_node.lchild is not None:
                queue.append(cur_node.lchild)
            if cur_node.rchild is not None:
                queue.append(cur_node.rchild)

    def preoder(self,node):
        """Preorder traversal (left and right root)"""
        if node is None:
            return
        print(node.elem,end=" ")
        self.preoder(node.lchild)
        self.preoder(node.rchild)

    def inoder(self, node):
        """Middle order traversal (left root right)"""
        if node is None:
            return
        self.inoder(node.lchild)
        print(node.elem, end=" ")
        self.inoder(node.rchild)

    def postoder(self, node):
        """Middle order traversal (left and right roots)"""
        if node is None:
            return
        self.postoder(node.lchild)
        self.postoder(node.rchild)
        print(node.elem, end=" ")

Among them, traversal is divided into four types. The order of these three types of traversal is different, resulting in different codes

Let's look at the detection code

if __name__ == "__main__":
    tree = Tree()
    tree.add(0)
    tree.add(1)
    tree.add(2)
    tree.add(3)
    tree.add(4)
    tree.add(5)
    tree.add(6)
    tree.add(7)
    tree.add(8)
    tree.add(9)
    tree.breadth_travel()
    print(" ")
    tree.preoder(tree.root)
    print(" ")
    tree.inoder(tree.root)
    print(" ")
    tree.postoder(tree.root)

First, a tree is established, and then elements 1, 2, 3, 4, 5, 6, 7, 8 and 9 are added to the tree. First, the tree is traversed in breadth, then in order, then in order, and finally in order.

Output results

0 1 2 3 4 5 6 7 8 9  
0 1 3 7 8 4 9 2 5 6  
7 3 8 1 9 4 0 5 2 6  
7 8 3 9 4 1 5 6 2 0 

Finally, let's talk about how to determine a binary tree by traversal.

First of all, we know that there is an order of light that we can't determine, because an order can't determine his left and right. Therefore, we must have two orders, and must contain the middle order, because the middle order can determine the left subtree and the right subtree, so we must need two orders to determine a tree.

Posted by Geof on Sun, 05 Dec 2021 02:12:21 -0800