(Binary) Trees

CSCI 1933 – Introduction to Algorithms and Data Structures
Adriana Picoral

Linear vs. Hierarchical

  • Arrays and linked lists store data in a line
  • Trees store data in a hierarchy instead

Tree

What is a tree in computer science?

  • A tree is a collection of nodes, which can be empty.
  • An empty tree points to null
  • If a tree is not empty we have a “root” node
  • The “collection” of nodes are stored as either connections to the root node, through a path of nodes collected to the root node

Tree Nodes

A tree is a data structure made of nodes connected by edges, where:

  • One node is the root (the top)
  • Each node can have children (nodes below it)
  • Nodes with no children are leaves
  • Each node (except the root) has exactly one parent

Tree – key properties

  • No cycles - no loops back to start
  • Connected - there’s a path from the root to every node
  • A tree with n nodes has exactly n-1 edges

Tree – advantages

  • Fast searching - in some types of trees (binary search tree), search is \(O(\log n)\)
  • Efficient for many other operations - insertion, deletion, traversal
  • Many problems are inherently represented by tree (hierarchical relationships)

Tree – example

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

A is the root.

There are n nodes and n-1 edges.

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

B is a child of A

F is a child of A

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

Node A is a parent of C

Node E is a parent of I

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

A Node with no children are leaves.

Nodes B, C, H, I, K, L, M, B, O, P are all leaves

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

Nodes with the same parent are siblings

Nodes I and J are siblings. K, L, and M are siblings.

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

Nodes H and N do not have any siblings (only children)

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

We can define a path from a parent to its children. The path from A to O is: A-E-J-O

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

The length of the path is three (the number of edges)

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

The height of the node is the longest path from the node to a leaf:

  • F has a height of 1
  • All leaves are a height of 0

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Tree – example

The height of the tree is the height of the root

Tree A A B B A--B C C A--C D D A--D E E A--E F F A--F G G A--G H H D--H I I E--I J J E--J K K F--K L L F--L M M F--M N N G--N O O J--O P P J--P

Implementation

Our tree data structure is made of nodes

  • Each node has pointers to children – how many children?

A Tree has a pointer to some “root” node

  • Binary (2 children, left and right) tree is a common implementation.
  • Typically, some type of ordering will be imposed on the elements in a binary tree.

Binary Trees – Another Higher Order Data Structure

  • Binary tree terms
    • root – the base of the tree (level 1)
    • empty tree (no items) is a tree of depth 0
    • left and right children, parents, siblings
    • subtree
    • leaves
    • complete* and full* trees

*definitions of these can vary, use ours

Full Binary Tree

Each level is completed filled with all children

BST 10 10 5 5 10--5 15 15 10--15 3 3 5--3 8 8 5--8 12 12 15--12 20 20 15--20

Complete Binary Tree

In a complete binary tree is a binary all levels except the last level are completely filled and all the leaves in the last level are all to the left side.

BST 10 10 5 5 10--5 15 15 10--15 3 3 5--3 null 5--null

Note: A full tree is also a complete tree, but not the other way around

Balanced Binary Tree

A balanced binary tree is a binary tree where the depth of the two subtrees of every node never differ by more than 1.

Below is a balanced binary tree but not a complete binary tree.

BST 10 10 5 5 10--5 15 15 10--15 null 15--null 20 20 15--20

Every complete binary tree is balanced but not the other way around.

Typically, some type of ordering will be imposed on the elements in a binary tree.