Parent_child_child brother notation of commonly used data structure tree

1. Parental representation of the tree

We talked about binary trees before, today we will discuss how to represent an ordinary tree. There are three commonly used representation methods: parent representation, child representation, and child brother representation. Let's first talk about parental notation. As the name implies, parental notation is to store the subscript of its parent node in each node. In this way, the structural relationship of each node in the tree can be expressed, and the parent node can be accessed quickly.
As shown in the figure below, we store an ordinary tree in the form of a list in Figure 2.
Insert picture description here
Insert picture description here
The specific python implementation code is as follows (personal handwriting, don't like it):

#-*- coding:utf-8 -*-
class Node:
    def  __init__(self,val,parent):
        self.val = val
        self.parent = parent
class tree:
    def __init__(self):
        self._array = []
    def addNode(self,val,parent):
        node = Node(val,parent)
        self._array.append(node)
    def show(self):
        for i,v in enumerate(self._array):
            print('节点下标为 = {} 值为 = {} 父节点下标为{}'.format(i,v.val,v.parent))
    def findparent(self,node):
        return self._array[node.parent]
tree = tree()
tree.addNode('R',-1)
tree.addNode('A',0)
tree.addNode('B',0)
tree.addNode('C',0)
tree.addNode('D',1)
tree.addNode('E',1)
tree.addNode('F',3)
tree.addNode('G',6)
tree.addNode('H',6)
tree.addNode('K',6)
tree.show()
node = Node('K',6)
node_parent = tree.findparent(node)
print('父节点为={}'.format(node_parent.val))

2. Child representation

Above we learned a way to store a normal tree, adding a pointer to its parent node in each node. Next, let’s talk about the child notation. The idea is very similar to the parent notation, but instead of storing the pointer information of the child node in each node, as shown in the following figure: a node may have multiple child nodes, so we Use a linked list to store the subscripts of child nodes.
Insert picture description here
The specific implementation code is as follows:

#-*- coding:utf-8 -*-
class Node:
    def  __init__(self,val,children):
        self.val = val
        self.children = children
class childrentree:
    def __init__(self):
        self._array = []
    def addNode(self,val,children):
        node = Node(val,children)
        self._array.append(node)
    def show(self):
        for i,v in enumerate(self._array):
            print('节点下标为 = {} 值为 = {} 孩子节点下标为{}'.format(i,v.val,v.children))
    def findChildren(self,node):
        chilren = [self._array[i].val for i in node.children]
        return chilren
tree = childrentree()
tree.addNode('R',[1,2,3])
tree.addNode('A',[4,5])
tree.addNode('B',[])
tree.addNode('C',[6])
tree.addNode('D',[])
tree.addNode('E',[])
tree.addNode('F',[7,8,9])
tree.addNode('G',[])
tree.addNode('H',[])
tree.addNode('K',[])
tree.show()
node = Node('F',[7,8,9])
node_children = tree.findChildren(node)
print('节点值为={},孩子节点为={}'.format(node.val,node_children))

3. Child brother notation

The methods discussed above are how to store an ordinary tree. Next, we will talk about how to convert an ordinary tree into a binary tree. We use child sibling notation, each node contains pointers to child nodes and pointers to sibling nodes. We make the child node the left child node of the node, and the sibling node the right child node of the node. This completes the conversion from a normal tree to a binary tree.
The specific code is as follows:

#-*- coding:utf-8 -*-
class Node:
    def  __init__(self,val,children,brother):
        self.val = val
        self.children = children
        self.brother = brother
class childrenbrotherree:
    def __init__(self):
        self._array = []
    def addNode(self,val,children,brother):
        node = Node(val,children,brother)
        self._array.append(node)
    def show(self):
        for i,v in enumerate(self._array):
            print('节点下标为 = {} 值为 = {} 孩子节点下标为{},兄弟节点下标为{}'.format(i,v.val,v.children,v.brother))
    def findChildren(self,node):
        chilren = [self._array[i].val for i in node.children]
        return chilren
    def findBrother(self,node):
        brother = [self._array[i].val for i in node.brother]
        return brother
tree = childrenbrotherree()
tree.addNode('R',[1],[])
tree.addNode('A',[4],[2])
tree.addNode('B',[],[3])
tree.addNode('C',[6],[])
tree.addNode('D',[],[5])
tree.addNode('E',[],[])
tree.addNode('F',[7],[])
tree.addNode('G',[],[8])
tree.addNode('H',[],[9])
tree.addNode('K',[],[])
tree.show()
node = Node('F',[7],[])
node_children = tree.findChildren(node)
print('节点值为={},孩子节点为={}'.format(node.val,node_children))

4. The forest is transformed into a binary tree

Above we discussed how to convert an ordinary tree into a binary tree, the child brother notation. Going further, how to convert multiple disjoint trees into binary trees? This is the conversion of the forest to a binary tree that we will talk about next. The core idea is to first use the child brother notation to convert each tree into a binary tree, and then use the root node of the first tree as the root node of the entire tree, and the root nodes of other trees as the root node of the first tree. The sibling nodes, and then use the child sibling notation to connect the entire tree together. The specific process is as follows.
Insert picture description here

5. Summary

What we discussed above is how to represent an ordinary tree, and how to represent an ordinary tree or even a forest as a binary tree. The most commonly used representation method is child sibling notation, which stores the subscripts of child nodes and sibling nodes in each node. The child node is the left child node and the sibling node is the right child node, thus converting an ordinary tree into a binary tree . After converting multiple trees into a binary tree, using the child sibling notation for the root node, you can convert a forest into a binary tree.

6. Abnormal talent

I have been looking for the right answer and have been missing it. I asked why there are so many impossible things in this world. I feel that I have worked hard enough. Why is it still so unknown. Who is willing to live a nine-to-five life, who is willing to admit that he is not worthy of his favorite person in his life. I want myself to do something different because I feel that I am living too ordinary. It has nothing to do with other people's eyes, but I am tired of this mediocrity. Whether it is the subconscious mind or the collective consciousness, I just don't want to be a puppet controlled by certain things. I want to do something, not because it helps my career, nor because it can make me excellent, not because of any of the reasons I said. Just because no one else has done it. The so-called chic, interesting, value, and mission do not have any appeal to me, because such shadows must be found in history. What fascinates me are the undefined, meaningless things that have never happened. Because some new lives are being awakened, some new abilities are being awakened by themselves, because of "Innate Talent"!

The recent super-hot Douyin Divine Comedy "Friendships" matches the temperament of Miss Polaris from "Talented", you must have heard it!

Guess you like

Origin blog.csdn.net/gaobing1993/article/details/108911246