0% found this document useful (0 votes)
14 views

Trees Ds

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Trees Ds

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Trees in Data Structures

1. Introduction to Trees
A tree is a widely used abstract data structure that simulates a hierarchical tree structure with a set
of connected nodes. A tree is defined as a finite set of elements called nodes, where:
• The first node is the root.
• Each node has zero or more child nodes.
• A node with no children is called a leaf.

2. Key Terminology
• Root: The topmost node in the tree.
• Parent and Child: If a node is connected to another, the former is the parent, and the latter
is the child.
• Sibling: Nodes with the same parent.
• Leaf: Nodes with no children.
• Height: The length of the longest path from the root to a leaf.
• Depth: The length of the path from the root to the node.
• Subtree: A tree formed by any node and its descendants.

3. Properties of Trees
1. A tree with N nodes has N-1 edges.
2. Only one path exists between two nodes.
3. Trees are recursive structures; each child can be considered a subtree.

4. Types of Trees
4.1 General Tree
A tree where each node can have any number of children.

4.2 Binary Tree


A tree where each node has at most two children: left and right.
4.2.1 Types of Binary Trees:
1. Full Binary Tree: Every node has 0 or 2 children.
2. Complete Binary Tree: All levels except possibly the last are completely filled.
3. Perfect Binary Tree: All internal nodes have two children, and all leaves are at the same
level.
4. Skewed Binary Tree: All nodes have only one child (either left or right).
5. Balanced Binary Tree: The height difference between left and right subtrees of any node is
at most one.

4.3 Binary Search Tree (BST)


A binary tree with the property:
• The left subtree contains nodes with values less than the node’s value.
• The right subtree contains nodes with values greater than the node’s value.

4.4 AVL Tree


A self-balancing binary search tree where the height difference of the left and right subtrees of any
node is at most one.

4.5 Heap
A binary tree used for priority-based operations:
1. Max Heap: Parent nodes have values greater than or equal to their children.
2. Min Heap: Parent nodes have values less than or equal to their children.

4.6 B-Tree
A self-balancing tree optimized for systems that read and write large blocks of data.

5. Tree Traversals
Tree traversal is the process of visiting all nodes in a specific order.

5.1 Depth-First Search (DFS)


1. Preorder: Visit root, traverse left subtree, traverse right subtree.
2. Inorder: Traverse left subtree, visit root, traverse right subtree.
3. Postorder: Traverse left subtree, traverse right subtree, visit root.

5.2 Breadth-First Search (BFS)


Also called Level Order Traversal; nodes are visited level by level.
Example Code for Inorder Traversal (C):
c
Copy code
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
6. Tree Operations
6.1 Insertion
• Binary Tree: Insert nodes at the first available position in level order.
• Binary Search Tree: Insert nodes based on their value relative to the current node.

6.2 Deletion
• Binary Tree: Replace the node to be deleted with the deepest node.
• Binary Search Tree:
1. Node with no children: Delete directly.
2. Node with one child: Replace the node with its child.
3. Node with two children: Replace with the inorder successor or predecessor.

6.3 Search
• Binary Search Tree: Traverse left or right subtree based on the comparison with the root.

7. Properties and Applications of Binary Trees


7.1 Properties
1. Maximum nodes at level L: 2L.
2. Maximum nodes in a binary tree of height H: 2H+1−1.
3. Minimum height of a binary tree with N nodes: ⌈log2(N+1)⌉.

7.2 Applications
1. Expression Trees: Evaluate arithmetic expressions.
2. Search Operations: Binary Search Trees are used to optimize searching.
3. Priority Queues: Implemented using heaps.
4. Database Indexing: B-Trees are used for indexing large datasets.

8. AVL Tree Example


Rotation Operations:
1. LL Rotation: Right rotation to balance the tree.
2. RR Rotation: Left rotation to balance the tree.
3. LR Rotation: Left followed by right rotation.
4. RL Rotation: Right followed by left rotation.

9. Trie (Prefix Tree)


A special tree used for searching strings efficiently. Each edge represents a character.
Applications:
1. Autocomplete systems.
2. Spell checkers.
3. IP routing (longest prefix match).

10. Comparison of Trees


Type Search Time Insert Time Delete Time Memory Usage
Binary Search Tree O(log N) O(log N) O(log N) Moderate
AVL Tree O(log N) O(log N) O(log N) High
B-Tree O(log N) O(log N) O(log N) Moderate

You might also like