Skip to content

Latest commit

 

History

History
97 lines (74 loc) · 2.19 KB

File metadata and controls

97 lines (74 loc) · 2.19 KB

Tree

What

A tree is Graph with no cycles - Nodes : V - edges : V - 1

Why

Tree usages - Hierarchical relationships - Manage sorted data - Enable fast searching operations

Terms

  • Root
  • Parent
  • Children
  • Ancestor
  • Descendent
  • Siblings
  • Leaf
  • Depth
    • Height (max Depth)

Type of trees

How do we represent trees?

Traversal Algorithm

  • Depth-First Search (DFS) Algorithm
  • Breadth-First Search (BFS) Algorithm

Tree Traversal

Achieved by DFS

  • Pre-Order
    • 1st time you visit
    • "Root", Left, Right
  • In-Order
    • 2nd time you visit
    • Left, "Root", Right
  • Post-Order
    • 3rd time you visit
    • Left, Right, "Root"

https://www.youtube.com/watch?v=WLvU5EQVZqY

Achieved by BSF

  • Level-Order

4 Types of Tree Traversal Algorithms

Analytics

Say if we have a simple request to traverse all Nodes of the tree. The order doesn't matter. What would be the complexity in terms of time and space?

Let's assume the number of all nodes are n first. You can use h as a hight of the tree for DFS too.

DFS

  • Time
  • Space

BFS

  • Time
  • Space

Exercise1

dfs

bfs

Exercise2

Advanced🔥