You are on page 1of 25

Chapter 5

Trees

1
A 2-dimensional structure
• A linked list is a one-dimensional data structure — each element is
followed by exactly one other element (except for the last).

2
A 2-dimensional structure
• We can extend the concept of linked list so that an element is
followed by a number of other elements
A
B C D E F G

H I J K L M N

P Q

A genealogy tree. A data structure to store A’s (biological) descendants

3
Tree
• We thus move from a one-dimensional linked list to a two-
dimensional structure.
• We call this structure a tree. A tree imposes a hierarchical structure
on a collection of elements.

4
Basic terminology
• A tree is a collection of nodes. One node is distinguished as the root.
Two nodes in the collection can be related by the parent-child relation
(represented by an edge).

5
Definition
• Formally, a tree can be defined recursively:
• A single node is a tree. The node is also the root of the tree
• Suppose n is a node and T1, T2, ..., Tk are trees with roots n1, n2,..., nk,
respectively. If we make n the parent of n1, ..., nk, the resulting structure is a
tree. In the tree, n is the root and T1, T2, ..., Tk are the subtrees of the root.
Nodes n1, n2,..., nk are called the children of node n.

𝑛
𝑛1 𝑛2 𝑛3

𝑇1 𝑇2 𝑇3

6
Definition
• Note: In our definition, a tree must have at least one node (base
case). We extend our definition so that a set with no nodes is also a
tree, called the null tree.
• Q: Does every node have a parent?
• Q: Does every node have children?

7
Definition
• If <n1, n2,..., nk> is a sequence of nodes in a tree such that ni is the
parent of ni+1 for 1  i < k, then this sequence is called a path from
node n1 to node nk.

n1
n2
n3

n4
n5

8
Definition
• The length of a path is the number of links in the path.
• e.g., length of path <n1, n2,..., nk> is k-1.
• e.g., length of path <n1> is 0.
n1
n2
n3
A path of length 4
n4
n5

9
Definition
• If there is a path from a node a to a node b, then
• a is an ancestor of b, and
• b is a descendant of a.
• Q: Can a node be an ancestor of itself?
n1
n1 is an ancestor n2
of n5 n3

n4
n5

10
Definition
• If a is an ancestor (descendant) of b and if a not = b, we call a a
proper ancestor (descendant) of b.
• A node with no children is called a leaf node, or an external node.
• A node that is not a leaf is called an internal node.
• A subtree of a tree is a node of the tree, together with all its
descendants.
• If nodes a and b have the same parent, they are siblings.

11
Definition
• The number of children of a node x is called the degree of x.
• The length of the path from the root r to a node x is the depth of x.
(What is the depth of the root?)
• The height of a tree is the largest depth of any node.
• A collection of trees is a forest.
• The children of a node are ordered from left-to-right. The following 2
trees are different.
a a

b c c b
12
Definition
• A tree is an unordered tree if we ignore the order of children.
• Q: How many edges (branches) are there in a tree of n nodes?
A: n-1.

13
Tree implementation
• How do we represent a node?
• e.g., suppose I want to build a data structure to represent a genealogy
tree.
• Attempt #1:
struct tree_node {
element ele;
tree_node *child[4];}
• Q: problem?

14
Tree implementation
• Attempt #2:
struct tree_node {
element ele;
tree_node *child[40];}
/* assume nobody on this earth can ever have >40 children */
• Problem?
• We do not know ahead of time how many children there are for a node.
• Q: what data structure do we use to store a collection of elements without prior
knowledge on the size of the collection?

15
Tree implementation
• Attempt #3: For each node, we keep a linked list of its child nodes.
Each node has a pointer that points to the list. The leftmost child -
right sibling representation.
A
B C D E F G

H I J K L M N

P Q
leftmost-child pointer
right-sibling pointer

16
Tree implementation
struct tree_node {
element ele;
tree_node *lc;
tree_node *rs;}

Q: What is the right sibling of root?

17
Tree traversals
• Sometimes we would like to visit all the nodes in a tree in some order,
e.g., searching a file in a directory

C:

Program Files Documents Downloads

Google Windows Games Words Letters Spreadsheets Video Audio

Classics Pop

18
Tree traversal
• There are 3 popular visiting orders: preorder, postorder, and inorder.
Root of tree
PREORDER (n) {
if (n == NULL) return; 𝑛
visit n;
/* suppose n has k subtrees: T1, T2, …, Tk */ 𝑛1 𝑛2 .... 𝑛3
/* ordered from left to right */
for i = 1 to k
𝑇1 𝑇2 𝑇3
/* i.e., from left to right */
PREORDER (T_i);
}

19
Tree Traversal
1

2 3 4

5 6 7

8 9 10

Pre-order: 1, 2, 3, 5, 8, 9, 6, 10, 4, 7
Post-order: 2, 8, 9, 5, 10, 6, 3, 7, 4, 1
20
Postorder and Inorder
POSTORDER (n) { INORDER (n) {
if (n == NULL) return; // for binary trees only
for i = 1 to k if (n== NULL) return;
POSTORDER (T_i); INORDER(left subtree);
visit n; visit n;
} INORDER(right subtree);
}

21
Tree traversal
• Q: What is the complexity of the traversal algorithms?
• A: O(n), where n is the number of nodes in the tree.

22
Binary trees
• A binary tree is a tree in which no node has more than 2 children. A
node can have a left child, a right child, or both, or none.
• If no node in a binary tree has single child, we call the tree a
full binary tree.

23
Complete binary tree
• A complete binary tree of height h is a full binary tree in which all
leaves are at the same depth (h).
• Q: How many nodes are there in a complete binary tree of height h?

24
Binary tree implementation
• Since every node in a binary tree has at most 2 children, we can
define a node by

struct tree_node { Sometimes, we add a “parent


pointer” to each node so that
element ele;
accessing the parent of a node
tree_node *left;
is easier. This is similar to
tree_node *right; “prev-pointer” in linked list.
}

25

You might also like