Trees

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
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?

  • Data structure made of nodes connected by edges
  • One node is the root (the top)
  • An empty tree root points to null
  • If a tree is not empty we have a “root” node
  • Each node can have children (nodes below it)
  • Every node is connected to the root by exactly one path
  • Nodes with no children are leaves
  • Each node (except the root) has exactly one parent
  • No cycles - no loops back to start
  • A tree with n nodes has exactly n-1 edges

Tree – advantages

  • Fast searching - in some types of trees (a balanced binary search tree, for example), 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 (the tree below n is 16) nodes and n-1 edges (the tree below has 15 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

Nodes with no children are leaves.

Nodes B, C, H, I, K, L, M, N, 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 are only children (they have no 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

We can define a path from an ancestor to a descendant. 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 a node is the longest path from that 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. Here’s a tree of height 3:

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

Gradescope – In-class exercise

Complete the in-class gradescope exercise on trees

Implementation

Our tree data structure is made of nodes

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

A Tree has a reference to a “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:
    • left and right children
    • balanced, complete and full trees (next slides)

We will be implementing two types of binary trees: Binary Search Trees (linked), and a Heap (array)

Full Binary Tree

Every node has 0 or 2 children.

BST 10 10 5 5 10--5 15 15 10--15 3 3 5--3 8 8 5--8

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. A tree can be both full and complete at the same time.

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

Balanced Binary Tree

A balanced binary tree is a binary tree where the height 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.