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?
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
Node – implementation
Our tree data structure is made of nodes
Each node has pointers to children (left and right)
We will be looking at tree’s with two or fewer children (hence left and right).
But in general, tree’s nodes may have more children
Node.java
public class Node<T> { private T data; private Node<T> left; private Node<T> right; public Node(T data) { this.data = data; left = null; right = null; } public T getData() { return data; } public void setData(T value) { this.data = data; } public Node<T> getLeft() { return left; } public void setLeft(Node<T> node) { left = node; } public Node<T> getRight() { return right; } public void setRight(Node<T> node) { right = node; }}
Tree – implementation
A Tree has a pointer to some “root” node
Tree.java
public class Tree<T> { private Node<T> root; public Tree(Node<T> node) { root = node; } public void addNode(Node<T> root, Node<T> node) { if (root.getLeft() == null) { root.setLeft(node); } else if (root.getRight() == null) { root.setRight(node); } else { addNode(root.getLeft(), node); // this is not great } } public void addNode(Node<T> node) { addNode(root, node); } public String toString(Node<T> node) { String message = node.getData() + " "; if (node.getLeft() != null) { message += toString(node.getLeft()); } if (node.getRight() != null) { message += toString(node.getRight()); } return message; } public String toString() { return toString(root).trim(); }}