Tree Data Structure Intro

What is a tree data structure?

A tree is a hierarchical data structure consisting of nodes connected by edges. It has the following properties:

  • One node is designated as the root node.
  • Every node (except the root) is connected to exactly one parent node.
  • Each node can have zero or more child nodes.
  • Nodes with no children are called leaf nodes.

Why do we use tree data structures?

  • Hierarchical Data Representation: Trees are used to represent data with hierarchical relationships, such as a file system.
  • Efficient Searching: Trees like binary search trees (BST) allow for efficient searching, insertion, and deletion operations.
  • Sorted Data: Trees like AVL trees and Red-Black trees maintain sorted data, allowing quick access and updates.
  • Multi-way Trees: Trees like B-trees are used in databases and file systems for efficient data retrieval.

Practical use cases of tree data structures:

  • File systems in operating systems.
  • HTML DOM (Document Object Model) in web browsers.
  • Family trees and organizational charts.
  • Decision trees in machine learning.
  • Abstract Syntax Trees in compilers.
  • Trie data structure for efficient string searching.
  • Binary Search Trees for maintaining sorted data.

Important Concepts in Tree Data Structure

1. Tree Terminology

  • Node: Basic unit of a tree, containing data and links to children.
  • Root: The top node in a tree.
  • Edge: The link between two nodes.
  • Child: A node directly connected to another node when moving away from the root.
  • Parent: A node directly connected to another node when moving towards the root.
  • Leaf: A node with no children.
  • Subtree: A tree consisting of a node and its descendants.
  • 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.

2. Types of Trees

  • Binary Tree: Each node has at most two children.
  • Binary Search Tree (BST): A binary tree with the left child less than the parent node and the right child greater than the parent node.
  • AVL Tree: A self-balancing BST where the height of the two child subtrees of any node differ by at most one.
  • Red-Black Tree: A self-balancing BST with additional properties for balancing.
  • N-ary Tree: Each node can have at most N children.
  • Segment Tree: Used for storing intervals or segments.
  • Trie (Prefix Tree): Used for storing a dynamic set of strings.

3. Tree Traversals

Understanding different tree traversal methods is crucial:

  • Inorder Traversal (Left, Root, Right): Used for BST to retrieve data in sorted order.
  • Preorder Traversal (Root, Left, Right): Used to create a copy of the tree.
  • Postorder Traversal (Left, Right, Root): Used to delete the tree.
  • Level Order Traversal: Used for breadth-first traversal.

4. Common Operations

  • Insertion: Adding a new node to the tree.
  • Deletion: Removing a node from the tree.
  • Searching: Finding a node in the tree.
  • Balancing: Ensuring the tree remains balanced (e.g., AVL trees, Red-Black trees).

5. Applications of Trees

  • Binary Search Trees (BST): Efficient searching, insertion, and deletion.
  • Heaps: Priority queues.
  • Tries: Autocomplete and spell checker.
  • Segment Trees: Range queries.
BT implementation in more controable way using recursion

Code Output
Level Order Traversal:
1 2 3 4 5 6 7 5 

This implementation provides the following features:

  1. setRoot(int val): Sets the root of the tree.
  2. addLeft(int parentVal, int childVal): Adds a left child to a node with the given parent value.
  3. addRight(int parentVal, int childVal): Adds a right child to a node with the given parent value.
  4. findNode(TreeNode current, int val): A helper method to find a node with a given value.
  5. levelOrderTraversal(): Performs a level order traversal of the tree.
Basic Implementation Of Binary Tree
BT Basic Implementation

Code Output
Level order traversal of binary tree is - 
1 2 3 4 5 6 7