📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Some Types of Trees
  • K-ary trees
  • Trees with special properties
  • Spacetime Complexity of Trees
  • Space complexity
  • Time complexity
  • Properties of Trees
  • Complete vs. incomplete
  • Full
  • Perfect
  1. Other
  2. Algo Expert

Trees

Trees are a type of a graph where

  • It's rooted, i.e., has a root node

  • Every node has child nodes

  • Each node only has one parent

  • Structure is directed, i.e., edges have direction (typically pointing downwards towards children)

  • Structure is acyclic, i.e., you can't return to previous nodes when traversing

  • Structure is connected, i.e., every branch has to be attached to the tree

Real-world examples of trees are an org chart of a company or a family tree.

Note: Sometimes nodes in a tree can be set up to always have pointers back to the root node, which can be useful when you want to get back to the root quickly.

Some special terminology:

  • The nodes at the bottom without any children are called leaf nodes

  • Any path that starts at the root node and ends at a leaf node is called a branch

  • When referring to how far down a tree goes, we call that depth or height or levels

Some Types of Trees

K-ary trees

Binary trees are the most common type of trees. They have all of the requirements of trees plus each node can only have at most 2 child nodes (thus why it's called binary).

Similarly, ternary trees basically have at most 3 child nodes per node.

Generalizing, we call these trees k-ary trees, where these trees have at most k child nodes per node.

Trees with special properties

Binary search trees (BSTs) are binary trees where they satisfy some special "binary search" property.

Min heaps and max heaps are also binary trees where they respectively satisfy some special "min heap" or "max heap" property.

Tries are trees that store characters in a string, allowing you to perform interesting operations on those characters/strings by taking advantage of the tree-like structure.

Spacetime Complexity of Trees

Space complexity

Storing a tree is almost always O(n) where n is the number of nodes in the tree.

Time complexity

Traversing a tree is generally O(n).

One exception though is when traversing through a binary search tree. When you search through a BST that's balanced, you can roughly eliminate half of the tree at every branching point. As a result, traversal is O(log n) for BSTs.

If, however, the binary search tree was imbalanced because all of the nodes were skewed to the right, you don't benefit from eliminating nodes anymore. In the worst case, the skew is so bad that the nodes form a straight line, which makes traversal O(n) since it's basically like traversing down a linked list.

Pro tip: In a coding interview, pay attention to if the binary tree is balanced. You can only be confident that traversal is, on average, O(log n) when it's balanced.

Note: There are some really fancy trees out there like red-black trees or AVL trees where they have optimizations to constantly re-balance the tree in order to maintain the logarithmic complexity. So it goes without saying that the logarithmic property is important!

Properties of Trees

Complete vs. incomplete

A tree is considered complete when

  1. All of its upper levels are completely filled up with nodes

  2. Its bottom level (level for leaf nodes) is filled up from left to right (does not have to be completely filled up though)

  • This just means you can't have gaps between leaf nodes, and you can't have empty space at the left-hand side of the bottom level

Otherwise, a tree is considered incomplete.

Full

A tree is considered full when each node either has

  • exactly k child nodes (for k-ary trees), OR

  • No child nodes.

Basically, every slot is filled up!

Perfect

A tree is considered perfect when all the leaf nodes are at the same depth/level. In other words, the bottom level is completely filled up.

Visually, that means the tree is shaped like a triangle!

PreviousGraphsNextAws Solutions Architect

Last updated 3 years ago