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 – example
A is the root.
There are n nodes and n-1 edges.
Tree – example
B is a child of A
F is a child of A
Tree – example
Node A is a parent of C
Node E is a parent of I
Tree – example
A Node with no children are leaves.
Nodes B, C, H, I, K, L, M, B, O, P are all leaves
Tree – example
Nodes with the same parent are siblings
Nodes I and J are siblings. K, L, and M are siblings.
Tree – example
Nodes H and N do not have any siblings (only children)
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 – example
The length of the path is three (the number of edges)
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 – example
The height of the tree is the height of the root
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
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.
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.
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.