A tree is a hierarchical data structure used in computer science to organize data.
It
consists of nodes connected by edges.
Key Terms in Trees
1. Node: A single element in a tree that contains data and references to child
nodes.
2. Root: The topmost node in a tree. It has no parent.
3. Parent Node: A node that has child nodes.
4. Child Node: A node that has a parent node.
5. Sibling Nodes: Nodes that share the same parent.
6. Leaf Node: A node that has no children (i.e., the last node in a branch).
7. Degree of Node: The number of children a node has.
8. Degree of Tree: The maximum degree of any node in the tree.
9. Depth of a Node: The number of edges from the root to that node.
10.Height of a Node: The number of edges on the longest path from that node to a
leaf.
11.Height of a Tree: The height of the root node.
12.Subtree: A portion of the tree that is itself a tree.
13.Path: A sequence of nodes connected by edges.
14.Level of a Node: The distance of a node from the root (Root is at level 0).
Types of Trees
1. General Tree: A tree where each node can have any number of children.
2. Binary Tree: A tree where each node has at most two children.
3. Binary Search Tree (BST): A binary tree where the left child contains smaller
values, and the right child contains larger values.
4. Balanced Tree: A tree where the height is maintained to be as small as possible.
5. Complete Binary Tree: A binary tree where all levels are completely filled except
possibly the last.
6. Full Binary Tree: A binary tree where each node has either 0 or 2 children.
7. Perfect Binary Tree: A binary tree where all leaf nodes are at the same level.
8. AVL Tree: A self-balancing binary search tree where the difference in height
between left and right subtrees is at most 1.
9. B-Tree: A self-balancing search tree used for database indexing.
🌲 BINARY TREES
A Binary Tree is a type of tree where each node has at most two children, referred to
as:
Left Child
Right Child
Types of Binary Trees
1. Strict (Proper) Binary Tree: Each node has either 0 or exactly 2 children.
2. Full Binary Tree: Every node has either 0 or 2 children, and all leaf nodes are at
the same depth.
3. Complete Binary Tree: All levels are completely filled except possibly the last,
which is filled from left to right.
4. Perfect Binary Tree: A complete binary tree where all internal nodes have
exactly 2 children, and all leaf nodes are at the same level.
5. Degenerate (Skewed) Binary Tree: A tree where each node has only one child,
making it look like a linked list.
Properties of Binary Trees
1. Number of Nodes (n):
o A binary tree with height hhh has at most 2h+1−12^{h+1} - 12h+1−1
nodes.
2. Number of Leaf Nodes (L):
o In a full binary tree, L=(n+1)/2L = (n + 1) / 2L=(n+1)/2.
3. Maximum Height of a Tree:
o A binary tree of nnn nodes has a height of at most n−1n-1n−1.
4. Minimum Height of a Tree:
o A binary tree with nnn nodes has a minimum height of log
2(n+1)−1\log_2(n+1) - 1log2(n+1)−1.
Binary Tree Traversal Methods
Tree traversal means visiting all the nodes of the tree. There are three main types of
Depth First Search (DFS) traversals:
1. Preorder Traversal (Root → Left → Right)
o Visit the root node first.
o Traverse the left subtree.
o Traverse the right subtree.
2. Inorder Traversal (Left → Root → Right)
o Traverse the left subtree.
o Visit the root node.
o Traverse the right subtree.
o Used in Binary Search Trees (BSTs) to retrieve elements in sorted order.
3. Postorder Traversal (Left → Right → Root)
o Traverse the left subtree.
o Traverse the right subtree.
o Visit the root node.
o Used for deleting a tree from memory.
There is also Breadth First Search (BFS) traversal:
4. Level Order Traversal
o Visit nodes level by level from top to bottom.
o Uses a queue (FIFO structure).
Binary Tree Representation in Memory
1. Linked Representation:
o Each node has data and pointers to left and right children.
o Suitable for dynamic trees.
2. Array Representation:
o Stores elements in an array (used for complete binary trees).
o If a node is at index i, its:
Left child is at 2i + 1
Right child is at 2i + 2
Parent is at (i-1)/2
Applications of Binary Trees
✅ Expression trees (used in compilers).
✅ Database indexing (B-Trees).
✅ Decision trees (used in AI and Machine Learning).
✅ File systems (hierarchical storage).
✅ Huffman Coding Trees (used in data compression).
✅ Binary Search Trees (efficient searching and sorting).