Description

In computer science, a tree is a hierarchical data structure consisting of nodes, where each node may have zero or more child nodes, and exactly one parent node—except for the root node, which has no parent. Trees are widely used for organizing and representing data that naturally forms hierarchies, such as file systems, organizational charts, and abstract syntax trees.

Trees enable efficient searching, insertion, and deletion operations and form the basis of many algorithms and data structures, including binary search trees, heaps, and tries.

Basic Terminology

TermDescription
NodeFundamental unit containing data and links
RootThe topmost node with no parent
ChildA node directly connected below another node
ParentThe node directly above a child node
LeafA node with no children
EdgeConnection between two nodes
SubtreeA node and all its descendants
DepthNumber of edges from root to the node
HeightLength of the longest path from node to a leaf
DegreeNumber of children of a node

Types of Trees

1. Binary Tree

  • Each node has at most two children (left and right).
  • Often used in search and sorting algorithms.

2. Binary Search Tree (BST)

  • Binary tree with ordered property:
    • Left child < parent < right child.
  • Enables fast lookup, insertion, and deletion (average O(log n)).

3. Balanced Trees

  • Ensures tree height remains logarithmic relative to number of nodes.
  • Examples: AVL Trees, Red-Black Trees, B-Trees.

4. Heap

  • Complete binary tree satisfying heap property:
    • Max-heap: parent ≥ children
    • Min-heap: parent ≤ children
  • Used in priority queues and sorting.

5. Trie (Prefix Tree)

  • Tree for storing strings where each node represents a character.
  • Efficient for prefix searches and autocomplete.

6. N-ary Tree

  • Nodes can have any number of children.
  • Useful for general hierarchies.

Common Tree Traversals

TraversalDescriptionOrder Example
PreorderVisit node, left subtree, right subtreeRoot, Left, Right
InorderLeft subtree, node, right subtreeLeft, Root, Right
PostorderLeft subtree, right subtree, nodeLeft, Right, Root
Level-orderVisit nodes level by level (BFS)Top to bottom, left to right

Tree Operations

  • Insertion: Add a new node to the tree.
  • Deletion: Remove a node, restructure as necessary.
  • Search: Find if a value exists.
  • Traversal: Visit nodes in a specific order.
  • Balancing: Reorganize tree to maintain efficiency.

Applications of Trees

  • File Systems: Directory structures form a tree.
  • Databases: Indexing with B-Trees.
  • Compilers: Abstract Syntax Trees (AST) for parsing code.
  • Networking: Routing tables.
  • AI: Decision trees and game trees.
  • XML/JSON Parsing: Document Object Model (DOM) trees.

Implementing a Binary Tree in Python

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

def inorder(root):
    if root:
        inorder(root.left)
        print(root.val)
        inorder(root.right)

# Example Usage
root = Node(1)
root.left = Node(2)
root.right = Node(3)
inorder(root)

Tree Height and Depth

  • Depth: Number of edges from root to node.
  • Height: Number of edges on longest path from node to leaf.

Balanced Trees

Balance affects search performance.

  • AVL Tree: Maintains balance factor ≤ 1
  • Red-Black Tree: Uses color properties to balance
  • B-Tree: Multi-way balanced tree for databases/filesystems

Advanced Tree Concepts

  • Suffix Trees: Store all suffixes of a string, useful in string matching.
  • Segment Trees: Efficiently query intervals.
  • Fenwick Trees (Binary Indexed Trees): Query cumulative frequencies.
  • Merkle Trees: Used in cryptography and blockchain to verify data integrity.

Related Terms

  • Graph
  • Binary Search
  • Heap
  • Trie
  • Linked List
  • Recursion
  • Depth-First Search (DFS)
  • Breadth-First Search (BFS)
  • Data Structure
  • Algorithm