Ds 3
Ds 3
Unit 3 – Trees
- Example:
Figure: Tree (a)
- Types of tree:
- General tree
- Binary tree
- Binary Search tree
- Expression tree
2. Degree of Node:
• The maximum number of child nodes of any node is known as degree of node.
• Degree of node is the number of nodes connected to a particular node.
• Example: Degree of each node of above given tree (a) is given below.
3. Degree of Tree:
• The maximum number of child nodes of root node is known as degree of tree.
• Degree of tree is the number of nodes connected to a root node.
• Example: For above given tree (a), degree of tree is 2 as no of child nodes of root
node A is 2.
• In-degree of Node: No of incoming edges to that particular node is known as In-
degree of
Node.
• Out-degree of Node: No of outgoing edges from that particular node is known as
Out
degree of Node.
4. Root Node:
5. Parent Node:
7. Leaf Node:
8. Levels:
• Level of a node represents the generation of a node.
• If the root node is at level 0, then its next child node is at level 1, its grandchild is
at level 2,
• and so on.
• Example:
9. Sibling:
• The nodes which have the same parent node are known as Sibling node.
• In above given tree (a), node B and C both are sibling node.
10. Path:
• The sequence of consecutive edges from source node to destination node is called a
path.
• The length of path means no of edges present on that path.
• In above given tree (a) path A-B-D , A-C-E and A-C-F
• The length of A-B-D path is 2 because 2 edges are present on that path.
- Types of Trees:
There are three types of tree which are listed below:
1. General Tree
2. Binary Tree
3. Binary Search Tree
4. Expression Tree
1. General Tree:
• General tree is one of the types of tree.
• Every nodes of the general tree which has infinite number of children.
• In general tree, root has in-degree 0 and maximum out-degree N.
• In general tree, each node have in-degree one and maximum out-degree N.
• Sub-trees of general tree are not ordered.
• Example:
2. Binary Tree:
• Binary tree is one of the types of tree.
• Binary tree is a tree in which every nodes of tree have maximum two children.
• In binary tree, root has in-degree 0 and maximum out-degree 2.
• In binary tree, each node have in-degree one and maximum out-degree 2.
• Sub-trees of binary tree are ordered.
• There are five types of Binary tree:
1. Full Binary Tree
2. Complete Binary Tree
3. Skewed Binary Tree
4. Extended Binary Tree
1. Insertion
• Description: Add a new node to the binary tree. For a Binary Search Tree (BST), the
node is added based on its value.
• Example: In a BST, if the node to be inserted has a smaller value than the current node, it
is placed in the left subtree; otherwise, it is placed in the right subtree.
2. Deletion
• Description: Remove a node from the tree while maintaining the tree's properties.
Special care is needed when the node to be deleted has two children.
• Example: In a BST, if a node has two children, it is replaced by its in-order successor or
in-order predecessor.
3. Traversal
• Description: Visit all nodes in a binary tree in a specific order. Common traversal
methods include:
o In-order (Left, Root, Right): Used in BST to get nodes in sorted order.
o Pre-order (Root, Left, Right): Used for creating a copy of the tree.
o Post-order (Left, Right, Root): Used for deleting or freeing nodes.
o Level-order: Breadth-first traversal visiting nodes level by level.
4. Search
• Description: Find a node with a specific value in the binary tree. In a Binary Search Tree
(BST), the search is efficient because the tree is sorted.
• Example: In a BST, search proceeds down the tree either left (if the target value is
smaller) or right (if it is larger) from the root.
In a linked storage representation of a binary search tree (BST), each node is represented
as a structure or class with pointers (or references) to its left and right child nodes. This is a
dynamic representation where nodes are allocated in memory only when needed, unlike
array-based representations.
Creating a Binary Search Tree (BST) involves a few basic operations such as inserting nodes,
searching for elements, deleting nodes, and traversing the tree. Below is an explanation of each
basic operation, along with the C code example for creating and manipulating a BST.
#include <stdio.h>
#include <stdlib.h>
struct Node {
};
return newNode;
- Searching
In a Binary Search Tree (BST), searching for a key follows these steps:
• The left child of a node contains a value less than the node.
• The right child of a node contains a value greater than the node.
• This structure allows efficient searching, insertion, and deletion operations.
Searching Algorithm:
Example:
markdown
50
/ \
30 70
/ \ / \
20 40 60 80
Understanding the Task: When modifying an element in a binary search tree, we're essentially
updating the value associated with a specific node. To maintain the binary search tree property
(where the left child's value is less than the parent's, and the right child's value is greater), we
might need to restructure the tree.
Steps Involved:
Example: Let's consider a binary search tree where we want to change the value of 20 to 30.
50
/ \
30 70
/ \ / \
20 40 60 80
Steps:
Resulting Tree:
50
/ \
30 70
/ \ / \
30 40 60 80
Key Considerations:
• Tree Structure: While the value update itself is straightforward, the resulting tree might need to
be restructured to maintain the binary search property. This could involve rotations or other
balancing techniques, especially in large or unbalanced trees.
• Efficiency: The efficiency of the modification depends on the height of the tree. In a balanced
tree, the worst-case time complexity is O(log n), where n is the number of nodes. In an
unbalanced tree, it can be O(n) in the worst case.
By following these steps and considering potential restructuring needs, you can effectively
modify elements in a binary search tree while preserving its essential properties.
Inserting an Element in a Binary Search Tree
Understanding the Task: When inserting an element into a binary search tree, we're adding a
new node that maintains the tree's order property: the left child's value is less than the parent's,
and the right child's value is greater.
Steps Involved:
Example: Let's insert the value 65 into the following binary search tree:
50
/ \
30 70
/ \ / \
20 40 60 80
Steps:
Resulting Tree:
50
/ \
30 70
/ \ / \
20 40 60 80
/
55
Key Considerations:
• Efficiency: The efficiency of insertion depends on the height of the tree. In a balanced tree, the
worst-case time complexity is O(log n), where n is the number of nodes. In an unbalanced tree,
it can be O(n) in the worst case.
• Balancing: To maintain efficient search and insertion operations, it's often necessary to balance
the tree. Techniques like AVL trees or red-black trees can be used to ensure that the height of
the tree remains logarithmic.
By following these steps and considering balancing techniques, you can effectively insert
elements into a binary search tree while preserving its order property and maintaining efficient
operations.
Understanding the Task: When deleting an element from a binary search tree, we need to
remove the node containing that element while maintaining the tree's order property. The
approach depends on the number of children the node has.
Cases:
50
/ \
30 70
/ \ / \
20 40 60 80
Steps:
Resulting Tree:
50
/ \
40 70
/ / \
20 60 80
Key Considerations:
• Efficiency: The efficiency of deletion depends on the height of the tree. In a balanced tree, the
worst-case time complexity is O(log n), where n is the number of nodes. In an unbalanced tree,
it can be O(n) in the worst case.
• Balancing: To maintain efficient search, insertion, and deletion operations, it's often necessary
to balance the tree. Techniques like AVL trees or red-black trees can be used to ensure that the
height of the tree remains logarithmic.
By following these steps and considering balancing techniques, you can effectively delete
elements from a binary search tree while preserving its order property and maintaining efficient
operations.
Destroying a Binary Search Tree
Understanding the Process: Destroying a binary search tree involves a post-order traversal,
where we process the left subtree, then the right subtree, and finally the current node. At each
node, we delete the memory allocated to it.
Visual Example:
50
/ \
30 70
/ \ / \
20 40 60 80
Steps:
Visualization:
50
/ \
30 70
/ \ / \
20 40 60 80
50
/ \
70
/ \
60 80
50
(After deleting root)
Key Points:
• The tree is systematically dismantled, starting from the leaves and working up to the root.
• Each node is deleted after its children have been processed.
• This post-order traversal ensures that no dangling pointers are left.
Note: This is a simplified visualization. In a real implementation, the memory for each node
would be deallocated as it is processed, rather than showing them as remaining in the diagram.
•
To visit each and every nodes of a tree at least once is known as Tree Traversing.
•
There are three different Tree traversals technique given below:
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal
1. Preorder traversal:-
• In this traversal method first visit root element, then left sub tree and then right sub tree.
• Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
Step 3: Visit right sub tree in preorder
2. Inorder traversal:-
• In this traversal method first visit left element, then root element and then the right element.
• Procedure:-
Step 1: Visit left sub tree in inorder
Step 2: Visit root node
Step 3: Visit right sub tree in inorder
3. Postorder traversal:-
• In this traversal first visit left sub tree, then right sub tree and then the root element.
• Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
Step 3: Visit root node
Preorder: A, B, C
Inorder: B, A, C
Postorder: B, C, A
- Expression Evaluation
Trees are a natural representation for expressions, where each node represents an operator or
operand.
How it works:
*
/ \
+ 4
/ \
2 3
Sparse matrices are matrices where most elements are zero. Using trees can efficiently store and
manipulate these matrices.
A sparse matrix is a matrix that contains a large number of zero elements. This is in contrast to a
dense matrix, where most of the elements are non-zero.
1. Memory Efficiency: Sparse matrices can be stored much more efficiently than dense matrices,
especially when the matrix is very large and has many zero elements. This is because only the
non-zero elements need to be stored, along with their indices.
2. Computational Efficiency: Many algorithms that operate on matrices can be optimized for
sparse matrices. This is because operations on zero elements can be skipped, leading to
significant performance gains.
• Coordinate List (COO): This representation stores each non-zero element as a triple (row,
column, value).
• Compressed Sparse Row (CSR): This representation stores the row indices, column indices, and
values of the non-zero elements in separate arrays.
• Compressed Sparse Column (CSC): This representation is similar to CSR, but the column indices,
row indices, and values are stored in separate arrays.
Many operations on matrices can be performed efficiently on sparse matrices. Some common
operations include:
• Matrix-Vector Multiplication: This operation can be performed efficiently using the CSR or CSC
representation.
• Matrix-Matrix Multiplication: This operation can be performed efficiently using the CSR or CSC
representation, but it is generally more complex than matrix-vector multiplication.
• LU Decomposition: This operation can be performed efficiently using the CSR or CSC
representation, but it is generally more complex than matrix-vector multiplication.
Example:
(0, 2, 3)
(1, 1, 2)
(2, 0, 1)
(2, 3, 4)
Sparse matrices are used in many areas of science and engineering, including:
How it works:
1. Store only non-zero elements: Each node in the tree represents a non-zero element, storing its
row, column, and value.
2. Organize nodes: Nodes can be organized in various ways:
o Row-major order: Nodes are arranged by row, then column.
o Column-major order: Nodes are arranged by column, then row.
o Hash table: Nodes are stored in a hash table for quick lookup based on row and column.
1 0 0
0 2 0
0 0 3
*
/ \
2 3
/ \
1 2
In summary, trees provide powerful and flexible data structures for representing and
manipulating expressions and sparse matrices. They offer efficient storage and operations,
making them valuable tools in various computational tasks.
2-3 trees are a type of self-balancing binary search tree. They are designed to ensure that the
height of the tree remains balanced, meaning that the difference in height between any two leaf
nodes is at most one. This property guarantees efficient search, insertion, and deletion
operations.
Key Properties:
• Nodes:
o 2-node: Contains one key and two children.
o 3-node: Contains two keys and three children.
• Ordering: Keys within a node are always in ascending order.
• Balancing: The height of any two leaf nodes differs by at most one.
Operations:
1. Search:
o Traverse the tree, comparing the target key with the keys in each node.
o If the target key is found, return the node.
o If the target key is less than the leftmost key, search the left subtree.
o If the target key is greater than the rightmost key, search the right subtree.
o If the target key is between two keys, search the middle subtree.
2. Insertion:
o Insert the new key into the appropriate leaf node.
o If the leaf node becomes a 4-node, split it into two 2-nodes and rebalance the parent
node.
o If the parent node becomes a 4-node, repeat the splitting process.
3. Deletion:
o Locate the node containing the key to be deleted.
o If the node has two children, replace it with its inorder successor (the smallest key in the
right subtree) or inorder predecessor (the largest key in the left subtree).
o If the node has one or zero children, simply remove it.
o If the parent node becomes a 1-node, merge it with a sibling node and rebalance the
parent node.
Applications:
• Database systems: 2-3 trees can be used to implement efficient indexes for databases.
• File systems: They can be used to store and retrieve data in a file system.
• Network routing: 2-3 trees can be used to implement routing algorithms in networks.
In summary, 2-3 trees are a valuable data structure for applications that require efficient search,
insertion, and deletion operations on a set of ordered elements. Their height-balanced property
ensures good performance, making them a popular choice in various domains.
B-trees are a type of self-balancing binary search tree that are specifically designed for efficient
disk access. They are commonly used in database systems and file systems due to their ability to
handle large datasets and minimize disk I/O operations.
Key Properties:
• Order: Each node has at most m children and at least ⌈m/2⌉ children, where m is the order of the
B-tree.
• Keys: Each internal node contains m-1 keys that separate its children.
• Balancing: All leaf nodes are at the same depth, ensuring efficient search.
Operations:
1. Search:
o Traverse the tree, comparing the target key with the keys in each node.
o If the target key is found, return the node.
o If the target key is less than the leftmost key, search the leftmost child.
o If the target key is greater than the rightmost key, search the rightmost child.
o If the target key is between two keys, search the appropriate child.
2. Insertion:
o Insert the new key into the appropriate leaf node.
o If the leaf node becomes full (has more than m-1 keys), split it into two nodes and
rebalance the parent node.
o If the parent node becomes full, repeat the splitting process.
3. Deletion:
o Locate the node containing the key to be deleted.
o If the node has two children, replace it with its inorder successor (the smallest key in the
right subtree) or inorder predecessor (the largest key in the left subtree).
o If the node has one or zero children, simply remove it.
o If the parent node becomes underfull (has fewer than ⌈m/2⌉ keys), merge it with a
sibling node and rebalance the parent node.
Advantages of B-Trees:
• Efficient disk access: B-trees are designed to minimize the number of disk I/O operations, which
is crucial for large datasets.
• Height balance: Ensures efficient search, insertion, and deletion operations.
• Flexibility: The order of a B-tree can be adjusted to optimize performance for different
workloads.
Applications:
• Database systems: B-trees are widely used to implement indexes for databases, providing
efficient data retrieval and updates.
• File systems: They can be used to store and retrieve data in a file system, ensuring efficient
access to large files.
• Network routing: B-trees can be used to implement routing algorithms in networks, providing
efficient routing decisions.
In summary, B-trees are a powerful data structure for applications that require efficient storage
and retrieval of large datasets. Their ability to minimize disk I/O operations and their height-
balanced property make them a popular choice in various domains.
- B+ trees
B+ trees are specialized self-balancing search trees commonly used in databases and file
systems. They are optimized for efficient disk access, making them ideal for handling large
datasets.
Key Characteristics:
• Leaf Nodes: All data records are stored exclusively in leaf nodes.
• Internal Nodes: Internal nodes contain pointers to child nodes and keys to guide the search.
• Balanced Structure: The tree is balanced to ensure that the height of all leaf nodes is
approximately the same, minimizing the number of disk accesses required to find a specific
record.
• Ordered Keys: Keys within each node are stored in ascending order.
• Order (m): The maximum number of children a node can have is defined by its order, typically a
large value.
Structure:
Operations:
• Search: A search starts at the root node and follows pointers to child nodes based on the key
comparisons. The search terminates at a leaf node.
• Insertion: A new record is inserted into a leaf node. If the leaf node is full, it splits into two
nodes, and the parent node is updated.
• Deletion: A record is deleted from a leaf node. If the leaf node becomes underflowed, it
borrows or merges with a sibling node.
Advantages of B+ Trees:
• Efficient Disk Access: B+ trees are optimized for disk access by minimizing the number of disk
I/O operations required.
• Fast Searches: The balanced structure and ordered keys allow for efficient searches.
• Good Insertion and Deletion Performance: B+ trees handle insertions and deletions efficiently,
even for large datasets.
• Suitable for Range Queries: B+ trees are well-suited for range queries, where you need to find
all records within a specific key range.
Applications:
• Database Indexing: B+ trees are widely used to index database tables, allowing for efficient data
retrieval.
• File Systems: B+ trees are used in file systems to store directory and file metadata, enabling fast
file access.
• In-Memory Databases: B+ trees can also be used in-memory for certain applications, providing
efficient data storage and retrieval.
In summary, B+ trees are a powerful data structure for handling large datasets that require
efficient disk access. Their balanced structure, ordered keys, and leaf-based data storage make
them well-suited for various applications, particularly in databases and file systems.