Binary Trees
COS2611 - Programming: Data Structures · Trees
Binary Trees
A binary tree is a data structure in which each node has at most two children. These children are referred to as the left child and the right child. A binary tree is a hierarchical structure that consists of nodes, where each node contains a value and pointers to its children. The top node is called the root. If a node does not have any children, it is called a leaf node.
Structure of a Binary Tree
The basic structure of a binary tree can be defined using a class in C++. Here is an example of how to define a binary tree node:
class Node {
int value;
Node* left;
Node* right;
Node(int val) {
value = val;
left = nullptr;
right = nullptr;
}
};In this class, each node contains an integer value and pointers to its left and right children. The constructor initializes the value and sets the children to nullptr, indicating that they do not point to any node initially.
Creating a Binary Tree
To create a binary tree, you need to create nodes and link them together. Here is an example of how to create a simple binary tree:
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->left->left = new Node(3);
root->left->right = new Node(7);This code creates a binary tree that looks like this:
10
/ \
5 15
/ \
3 7
Properties of Binary Trees
Binary trees have several important properties:
- Height: The height of a binary tree is the length of the longest path from the root to a leaf node. The height of an empty tree is -1, and the height of a tree with only one node is 0.
- Depth: The depth of a node is the length of the path from the root to that node. The depth of the root is 0.
- Balanced Trees: A binary tree is balanced if the height of the left and right subtrees of any node differ by at most one.
Types of Binary Trees
There are several types of binary trees, including:
- Full Binary Tree: A binary tree where every node has either 0 or 2 children.
- Complete Binary Tree: A binary tree in which all levels, except possibly the last, are fully filled, and all nodes are as far left as possible.
- Perfect Binary Tree: A binary tree in which all internal nodes have exactly two children and all leaf nodes are at the same level.
Binary Tree Traversal
Traversal refers to the process of visiting all nodes in a binary tree. There are three common methods for traversing a binary tree:
- In-order Traversal: Visit the left subtree, the root node, and then the right subtree. This traversal produces nodes in non-decreasing order for binary search trees.
- Pre-order Traversal: Visit the root node, the left subtree, and then the right subtree. This traversal is useful for creating a copy of the tree.
- Post-order Traversal: Visit the left subtree, the right subtree, and then the root node. This traversal is useful for deleting the tree.
Implementing Tree Traversal in C++
Here is how you can implement in-order, pre-order, and post-order traversal in C++:
void inOrder(Node* node) {
if (node == nullptr) return;
inOrder(node->left);
cout << node->value << " ";
inOrder(node->right);
}
void preOrder(Node* node) {
if (node == nullptr) return;
cout << node->value << " ";
preOrder(node->left);
preOrder(node->right);
}
void postOrder(Node* node) {
if (node == nullptr) return;
postOrder(node->left);
postOrder(node->right);
cout << node->value << " ";
}To use these functions, you would call them with the root of the tree:
inOrder(root);
preOrder(root);
postOrder(root);Watch Out for Common Mistakes
Watch out: Remember to check for null pointers when traversing the tree. Failing to do so can lead to segmentation faults or crashes.
Applications of Binary Trees
Binary trees are used in various applications, including:
- Binary Search Trees (BST): A binary tree where the left child is less than the parent and the right child is greater. This structure allows for efficient searching, insertion, and deletion.
- Expression Trees: Used to represent expressions in a hierarchical manner. Each internal node represents an operator, and each leaf node represents an operand.
- Heaps: A special type of binary tree that satisfies the heap property, which is used in priority queues.
Dynamic Memory Allocation
In C++, binary trees often use dynamic memory allocation to create nodes. This allows for flexibility in the size of the tree. When you create a node using the new operator, you must also ensure that you free the memory when the node is no longer needed to prevent memory leaks. Use the delete operator for this purpose:
delete root;
delete root->left;
delete root->right;Summary
- A binary tree is a hierarchical data structure with nodes having at most two children.
- Traversal methods include in-order, pre-order, and post-order.
- Dynamic memory allocation is important for creating and managing nodes in a binary tree.
- Binary trees have various applications, including binary search trees and expression trees.
Check your understanding
- What is a binary tree?
- Explain the difference between in-order, pre-order, and post-order traversal.
- What are the properties of a balanced binary tree?
- How do you dynamically allocate memory for a new node in C++?