1 Basic Operation Of Tree Data Structure
- Create – create a tree in the data structure.
- Insert − Inserts data in a tree.
- Search − Searches specific data in a tree to check whether it is present or not.
- Traversal:
- Preorder Traversal – perform Traveling a tree in a pre-order manner in the data structure.
- In order Traversal – perform Traveling a tree in an in-order manner.
- Post-order Traversal –perform Traveling a tree in a post-order manner.
2 Properties of Tree Data Structure
Consider the following binary tree:
1
/ \
2 3
/ \
4 5
1 Number of Edges:
An edge connects two nodes in a tree. If a tree has N nodes, it will have (N-1) edges. There is exactly one path from each node to any other node in the tree.
- The tree has 5 nodes, so it will have (5 - 1) = 4 edges.
2 Depth of a Node:
The depth of a node is the length of the path from the root to that node. Each edge along the path adds 1 unit to the depth. Alternatively, it's the number of edges in the path from the root to the node.
- Depth of node 1: 0 (root node)
- Depth of node 2: 1
- Depth of node 3: 1
- Depth of node 4: 2
- Depth of node 5: 2
3 Height of a Node:
The height of a node is the length of the longest path from the node to a leaf node of the tree. It represents the longest downward path from the node to a leaf.
- Height of node 1: 2 (longest path to leaf: 1 → 2 → 4)
- Height of node 2: 1 (longest path to leaf: 2 → 4)
- Height of node 3: 1 (longest path to leaf: 3)
- Height of node 4: 0 (leaf node)
- Height of node 5: 0 (leaf node)
4 Height of the Tree:
The height of a tree is the length of the longest path from the root to a leaf node of the tree. It represents the longest downward path in the entire tree.
- The longest path from the root to a leaf node is 2 (root → 2 → 4 or root → 2 → 5), so the height of the tree is 2.
5 Degree of a Node:
The degree of a node is the total number of subtrees attached to that node. For a leaf node, the degree is 0. The degree of a tree is the maximum degree among all the nodes in the tree.
- Degree of node 1: 2 (two subtrees: 2 and 3)
- Degree of node 2: 2 (two subtrees: 4 and 5)
- Degree of node 3: 0 (leaf node)
- Degree of node 4: 0 (leaf node)
- Degree of node 5: 0 (leaf node)
3 Need for Tree Data Structure
1 Hierarchical Representation:
Trees are well-suited for representing hierarchical relationships, such as organizational structures, file systems, XML/HTML documents, and nested data.
2 Efficient Searching and Sorting:
Binary search trees (BSTs) provide efficient searching, insertion, and deletion operations with an average time complexity of O(log n), making them ideal for implementing search and sort algorithms.
3 Data Organization:
Trees can organize data in a structured and efficient manner, enabling quick access and manipulation of data elements.
4 Binary Heap:
Binary heaps, a type of binary tree, are used to implement priority queues, which are essential in algorithms like Dijkstra's shortest path algorithm and heap sort.
5 Representation of Arithmetic Expressions:
Expression trees are used to represent arithmetic expressions in a tree format, facilitating evaluation and manipulation of expressions.
6 Efficient Storage and Retrieval:
Tries (prefix trees) are specialized tree structures used for efficient storage and retrieval of strings, making them suitable for applications like autocomplete and spell checking.
7 Balancing and Optimization:
Self-balancing trees like AVL trees and Red-Black trees maintain balance in the tree structure, ensuring efficient performance of operations like insertion, deletion, and search.
8 Graph Representation:
Trees are a special type of graph with no cycles, making them suitable for representing hierarchical relationships and providing a foundation for graph algorithms and data structures.
9 Algorithm Design:
Many algorithms and data structures, such as depth-first search (DFS) and breadth-first search (BFS), are based on tree traversal techniques and concepts.
10 Memory Management:
Trees are used in memory management algorithms like binary buddy systems and binary trees for memory allocation and deallocation.
4 Relationship between number of nodes (n) and the height (h) in binary tree
#4.1 Maximum Number of Nodes at a Given Height:
- In a binary tree, the height h represents the longest path from the root node to any leaf node.
- At each level of the tree, the number of nodes that can exist doubles.
- At level 0 (the root level), there is 1 node.
- At level 1, there can be at most 2 nodes.
- At level 2, there can be at most 4 nodes.
- And so on…
Therefore, the maximum number of nodes at a given height is 2^h
.
#4.2 Maximum Height with a Given Number of Nodes:
- Conversely, if we know the number of nodes in a binary tree, we can determine the maximum height of the tree.
- For example, if there is only 1 node, the tree's height is
0
(h = 0
). - With 2 nodes, the tree's height is 1 (h=1).
- With 3 to 4 nodes, the tree's height is 2 (h = 2).
- With 5 to 8 nodes, the tree's height is 3 (h = 3).
- In general, if there are n nodes, the height of the tree is
log2(n)
, rounded up to the nearest integer.
#4.3 Relationship between Number of Nodes and Height:
We can express the relationship between the number of nodes (n) and the height (h) of a binary tree with the equation:
n = 2^(h+1)-1
Or equivalently:
h = log2(n+1) -1
#4.4 Maximum and minimum height
Maximum Height:
- The maximum height of a binary tree occurs when the tree is completely unbalanced, with each node having only one child, forming a linked list structure.
- In such a scenario, the height of the tree is equal to the number of nodes minus one.
- Therefore, the maximum height of a binary tree with n nodes is
h = n - 1
.
Minimum Height:
- The minimum height of a binary tree occurs when the tree is perfectly balanced, with each internal node having two children, and the height is minimized.
- For a binary tree with n nodes, the minimum height is achieved in a balanced binary tree, where the number of nodes in the left and right subtrees of any node differs by at most one.
- In a perfectly balanced binary tree, the height is approximately log₂(n), rounded up to the nearest integer.
- Therefore, the minimum height of a binary tree with n nodes is h = ⌈log₂(n + 1)⌉ - 1, where ⌈x⌉ represents the ceiling function, rounding x up to the nearest integer.
Example:
Suppose we have a binary tree with 15 nodes:
- Maximum Height: h = 15 - 1 = 14
- Minimum Height (for a perfectly balanced tree): h = ⌈log₂(15 + 1)⌉ - 1 = ⌈log₂(16)⌉ - 1 = ⌈4⌉ - 1 = 4 - 1 = 3