coding-interview|August 17, 2020|2 min read

Binary Tree Data Structure

TL;DR

A binary tree has at most two children per node. Know the difference between full, complete, and perfect binary trees — and understand that traversal order (in/pre/post) determines what you see.

Binary Tree Data Structure

A Binary tree is a data structure which has two children nodes attached to it, called left and right node. Do remember, its different from a Binary Search tree.

Few Basics if Binary Tree:

  • Parent node can have maximum two children nodes
  • Parent node can have Zero nodes as well, it will be the leaf node
  • There is no relation in the data between parent and children.

Basic Data Structure

Lets look at the basic data structure to denote a Binary Tree.

public class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

Above code is in Java. A tree node has three things:

  1. The data (It can be any data type, I have taken integer for simplicity)
  2. A pointer/reference to left child
  3. A pointer/reference to right child

Note the data type is same for left and right node.

Lets take a look at a representation of a Binary Tree:

        50
      /    \
    80      30
  /    \      \
20     40      10

Create Sample Binary Tree with code

Lets see a small code on how we can create above tree with the class Node data structure.

public Node buildSampleTree() {
  Node root = new Node(50);
  root.left = new Node(80);
  root.right = new Node(30);
  
  root.left.left = new Node(20);
  root.left.right = new Node(40);
  
  root.right.right = new Node(10);
  
  return root;
}

In above sample function, we are creating our tree with the data I shown above. For Node 30, we have only right child. Its left pointer/reference is null. Similarly for all leaf nodes, their left and right child are null.

Some Considerations

This data structure can be used at any place where you want to represent upto 2 children. But there is no relationship among the node values.

For example, To search an element you need to search entire tree. Since you can not determine whether your node lies in left or right side, unlike Binary Search tree

Related Posts

Binary Search Tree (BST) Data Structure

Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes…

Determine if a string has all unique characters

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all the characters…

How to calculate First Common Ancestor of two Nodes in Binary Tree

How to calculate First Common Ancestor of two Nodes in Binary Tree

First try to understand question. Its a binary tree, not a binary search tree…

How to calculate angle between hour and minute hand, given a time

How to calculate angle between hour and minute hand, given a time

This problem is a simple mathematical calculation. Lets start deriving some…

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…