# Algorithm learning diary day06

Establish an ordered tree and traverse it, provided that the data is orderly. There are four methods to traverse an ordered tree: pre order, middle order, post order and hierarchical traversal

D (root node) L (left subtree) R (right subtree)

The preorder traversal is DLR. First access the root node, and then from left to right. A left subtree accesses the left subtree, and no left subtree accesses the right subtree

The middle order traversal is LDR. First access the left subtree, then from left to right. After accessing the root node on the left, continue to access the right half subtree, from left to right

The subsequent traversal is LRD. First access the left subtree, then from left to right. After the left access, skip the root node, continue to access the right half subtree, from left to right, and finally access the root node

Layered traversal is obviously a layer by layer downward access. After accessing the first layer, you can continue to access the second layer

```import numpy as np
class Node():                            #Define node classes
def __init__(self,data=''):
self.data=data                  #Node value
self.left=None                  #Left child node
self.right=None                 #Right child node
def __str__(self):                  #Implicit read current node method
return str(self.data)           #Returns the current node value

class BuildTree():
def __init__(self,firstData):
self.rootNode=Node(firstData)   #The root node of the tree. The default data value is empty
def addNodes(self,NewDatas): #Add nodes to the ordered tree, and newdata is the list. The premise is that the data has been sorted
Datalength=len(NewDatas)+1      #1 is the root node
depth=np.floor(np.log2(Datalength))+1  #Find the depth of the tree, and np.floor() returns the maximum integer not greater than the input parameter. log2 is the value of the logarithm of Datalength based on 2
i=2
while i<=depth:                 #Handle the nodes of each layer of the tree
currNN=2**(i-1)             #Maximum number of nodes in current layer
j=0
while j<currNN:             #Add nodes to the current hierarchy until it is full
j+=1
if NewDatas:             #When the list is not empty, the elements on the left will pop up
data=NewDatas.pop(0) #Delete the element entering the new node in the list
else:
break;               #The last layer node must consider the case that the value provided by the list is not full
Node1=Node(data)         #Generate new node
print('Number of nodes%d'%(data))
if j%2==0:               #Add left node for j=1 and right node for j=2
curr.right=Node1      #Add right child node to current node
stock.pop(0)         #Remove the front node address
else:
curr.left=Node1      #Add left child node to current node
i+=1                       #Tree layer plus 1
line1=[1,20,30,40,50,60,70,80]         #Value of node
B1=BuildTree(line1)                 #Establish a binary tree instance with root node
B1.addNodes(line1[1:])                 #Build the tree from node 2
#===================================
def preoder(root):                     #Recursive traversal
p1=[]
if root:                           #Non empty node
p1.append(root.data)
p1+= preoder(root.left)        #Recursive call to left node
p1+= preoder(root.right)       #Recursive call to right node
L1=preoder(B1.rootNode)
print(L1)
#=====================================
def middle(root):                     #Recursive traversal
p1=[]
if root:                           #Non empty node
p1+= middle(root.left)        #Recursive call to left node
p1.append(root.data)
p1+= middle(root.right)       #Recursive call to right node
L1=middle(B1.rootNode)
print(L1)
#=====================================
def post(root):                     #Postorder traversal
p1=[]
if root:                           #Non empty node
p1+= post(root.left)        #Recursive call to left node
p1+= post(root.right)       #Recursive call to right node
p1.append(root.data)
L1=post(B1.rootNode)
print(L1)
#====================================
def level(root):
q1=[root.data,root.left, root.right]#Store data, left and right node addresses
p1=[]                               #Record the value of the node
while q1:
em= q1.pop(0)                   #Jump an element from the list
if em:
if isinstance(em,Node):       #Determine whether the element is a node address
q1.append(em.data)        #Put the values in the node into the list
q1.append(em.left)        #Put the address of the left child node in the node into the list
q1.append(em.right)       #Put the address of the right child node in the node into the list
else:
p1.append(em)            #Put node data into the list
return p1
L1=level(B1.rootNode)
print(L1)
```

Graph theory is a branch of mathematics, which takes graph as the research object.

• Graph in graph theory is a graph composed of several given points and lines connecting two points. This graph is usually used to describe a specific relationship between things. Points represent things, and lines connecting two points represent that there is this relationship between the corresponding two things.
• Graph can be divided into directed graph and undirected graph. Every edge of a graph defines a direction, which is called a directed graph; A graph whose edges have no direction is called an undirected graph.
• The number of edges in and out of a node in a graph is called the degree of the node. The degree of the node is represented by d(v), and v represents the node.
• If an edge of a node starts and ends with the node, it is called a self ring. The realization of graph structure is mainly divided into adjacency matrix and adjacency table.