Trees Ds
Trees Ds
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.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.
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.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.