Learning algorithms: Binary Tree

Binary Tree

concept

Elements in the tree is called a node
relationship between the nodes adjacent wires called parent-child relationship
Image.png

node
  1. A node is a parent node of node B, B node is a child node of the node A.
  2. Parent nodes C, D is a single node of the same node, so they are referred to as cross- sibling
  3. The no parent node is called the root node
  4. Node has no children is called a leaf node or a leaf node
tree
  1. Height node: node to the leaf node of the longest path (number of edges)
  2. Depth nodes: the root node to this node experienced by the number of edges
  3. Number of floors node: The node depth +1
  4. Height of the tree: the height of the root node

Image [2].png

Height: measure up from the height of the leaf node is 0
Depth: measured from top to bottom, the depth of the root node 0
layers: the depth and the like, the start of counting is 1

Binary Tree

Image [3].png

The binary number 2, leaf nodes are at the bottom, except leaf nodes, each node has two child nodes of the left and right, this is called binary full binary tree

Reference numeral 3 in the binary tree, the leaf nodes are the bottom layers, the last layer against the left leaf node are arranged, and in addition to the last layer, the other layers have the number of nodes reaches the maximum, the binary tree is called a complete binary tree .

storage
  • Chain storage method
    for each node has three fields, a data store, a pointer to the other two right and left child node.
    Image [4].png

  • Sequential storage method
    based on the sequential storage array method, the root node is stored at index position i = 1, then left node stored at index position 2 * i = 2, the right child node is stored in the position 1 = 2 * i = 3, so
    Image [5].png
    if the binary tree is a complete binary tree, that is the most storage arrays with a way to save memory

Binary tree traversal

Traversing the following three ways: a preorder traversal , in order traversal and postorder
before, during and after the node in the sequence represents its left and right subtrees of nodes traversed printing.

  • Preorder traversal means that, for any node in the tree, the first printing of this node, and then print it left subtree, and finally print its right subtree
  • Means a traversal sequence, for any node in the tree, the first print its left subtree, and then print its own, and finally prints its right subtree
  • Postorder means that, for any node in the tree, the first print its left subtree, and then print it right subtree, and finally print the node itself.
    UTOOLS1576592045092.png

Before the postorder is a recursive process.
Preorder traversal, in fact, is the first to print the root node, then recursively prints the left subtree, right subtree recursively print the rear left
key recurrence formula is that if you want to solve the problem A, assume sub-problem B, C has been resolved, how to use and then look at B, C solution A,


//前序遍历的递推公式:
preOrder(r) = print r->preOrder(r->left)->preOrder(r->right);

//中序遍历的递推公式:
inOrder(r) = inOrder(r->left)->print r->inOrder(r->right);

//后序遍历的递推公式:
postOrder(r) = postOrder(r->left)->postOrder(r->right)->print r;

void preOrder(Node* root) {
  if (root == null) return;
  print root // 此处为伪代码,表示打印 root 节点
  preOrder(root->left);
  preOrder(root->right);
}

void inOrder(Node* root) {
  if (root == null) return;
  inOrder(root->left);
  print root // 此处为伪代码,表示打印 root 节点
  inOrder(root->right);
}

void postOrder(Node* root) {
  if (root == null) return;
  postOrder(root->left);
  postOrder(root->right);
  print root // 此处为伪代码,表示打印 root 节点
}

Binary tree traversal time complexity:
up to be accessed twice for each node, the number n is proportional to the time and complexity of the nodes is O (n)

Guess you like

Origin www.cnblogs.com/jinlin/p/12073609.html