# Binary Tree Basic Operation for Known C++ Algorithms

Posted by closer on Thu, 23 Jan 2020 06:29:28 +0100

# Test Tree

```	TreeNode* root = new TreeNode(1);
TreeNode* left = new TreeNode(2);
TreeNode* right = new TreeNode(3);
TreeNode* left2 = new TreeNode(4);
TreeNode* right2 = new TreeNode(5);
TreeNode* left3 = new TreeNode(6);
TreeNode* right3 = new TreeNode(7);

root->left = left;
root->right = right;
left->left = left2;
left->right = right2;
right->left = left3;
right->right = right3;
```

# Traversal of binary trees (recursive version)

```//Recursive post-version traversal
void BinarytreeTraversal(TreeNode* root) {
if (root == nullptr)
return;
BinarytreeTraversal(root->left);
BinarytreeTraversal(root->right);
cout << root->val << " ";//Change the position of the root node to traverse before, during and after

}
```

# Pre-traversal of binary trees (non-recursive, single-stack)

```void PreorderTraversal(TreeNode* root) {
if (root == nullptr)
return;
stack<TreeNode*> s1;
TreeNode* cur;
s1.push(root);
while (!s1.empty()) {
cur = s1.top();
s1.pop();
cout << cur->val << " ";
if (cur->right)
s1.push(cur->right);
if (cur->left)
s1.push(cur->left);
}
}
```

# Middle-order traversal of binary trees (non-recursive, single-stack)

```void InorderTraversal(TreeNode* root) {
if (root == nullptr)
return;
stack<TreeNode*> s1;
TreeNode* cur = root;
TreeNode* node;
while ((!cur) || (!s1.empty())) {
if (cur) {
s1.push(cur);
cur = cur->left;
}
else if (!cur) {
node = s1.top();
s1.pop();
cout << node->val << " ";
cur = node->right;
}
}
}
```

# Binary Tree Postorder Traversal

#### Method 1: (Non-recursive double stack)

```void PostorderTraversal(TreeNode* root) {
stack<TreeNode*> s1;
stack<TreeNode*> s2;
TreeNode* cur = nullptr;
if (root == nullptr)
return;
s1.push(root);
while (!s1.empty()) {
cur = s1.top();
s1.pop();
s2.push(cur);
if(cur->left)
s1.push(cur->left);
if(cur->right)
s1.push(cur->right);
}

while (!s2.empty()) {
cout << s2.top()->val << " ";
s2.pop();
}
}
```

#### Method 2: (Non-recursive stack)

```void PostorderTraversal2(TreeNode* root) {
if (root == nullptr)
return;
stack<TreeNode*> s1;
TreeNode* h = root;
TreeNode* c = nullptr;
s1.push(h);
c = s1.top();
while (!s1.empty()) {
if (c->left && h != c->left && h != c->right) {
s1.push(c->left);
}
else if (c->right && h != c->right) {
s1.push(c->right);
}
else {
c = s1.top();
cout << c->val << " ";
s1.pop();
h = c;
}
if(!s1.empty())
c = s1.top();
}
}
```

#### Binary Tree Traversal Layers

Width first traverses common queue structures, printing with line numbers

You can operate with last (the rightmost node of the current row being printed) and nlast (the rightmost node of the next row)

#### Serialization and Deserialization of Binary Trees

Serialization: front, middle, back traversal and layer-by-layer traversal

String hollow can be indicated with #, and end can be used!Express

Deserialization: Follow the string!Split, then deserialize according to the serialization rules

#### Subtrees of Binary Trees

Any entire tree with a node as its head.

#### Balanced Binary Tree (AVL Tree)

1. An empty tree is a balanced binary tree

2. A tree is not empty, and all subtrees satisfy the height difference of their respective left subtree and right subtree not more than 1

#### Search Binary Tree

Feature: The value of the head node of each subtree is larger than that of all the nodes of the respective left subtree and smaller than that of all the nodes of the respective right subtree.

Sequences traversed in middle order must be arranged from small to large.

Red-black trees, balanced binary trees, and so on, are actually different implementations of searching binary trees.(improve efficiency)

#### Give the header node to determine if it is a search Binary Tree

Using non-recursive, intermediate traversal achieves that each traversal value is larger than the previous one

#### Full Binary Tree

Except that the node on the rightmost layer has no nodes, there are two child nodes for each remaining layer.

Number of nodes = 2 layers power - 1, number of layers = log is bottom 2 (number of nodes + 1)

#### Complete Binary Tree

Except for the rightmost layer, the number of nodes in the other layers is full, and the missing nodes in the last layer are all concentrated on the right.

#### Give the header node to determine if it is a complete binary tree

By definition, you can traverse a binary tree layer by layer

#### Interview and Binary Tree Node Types in Engineering

Interview includes: data item, left child, with child

There is often one more pointer to the parent node in a project.

#### Successor Node

This node is the next node in the sequential traversal sequence.

#### Precursor Node

This node is the last node in the sequential traversal sequence.

#### There are three cases where two nodes are farthest from each other in a binary tree

1. Maximum distance on left subtree

2. Maximum distance on right subtree

3. Maximum distance from left child in left subtree + head node itself + maximum distance from right child in right subtree

The maximum distance between 123 is the maximum distance between two nodes

#### The rewriting of binary tree traversal requires proficiency  29 original articles published, 8 praised, 3947 visited