1
Chapter 7. Trees & Binary Trees
- Set ADT
- Introduction to trees and binary trees
Lecture 14
ADS2 Lecture 14
Pictures of trees
File System
Pictures of trees
ADS2 Lecture 14 4
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of treesunrooted
Pictures of treesunrooted
Pictures of treesunrooted
12ADS2 Lecture 14
unrooted Pictures of trees
13ADS2 Lecture 14
Pictures of trees
14ADS2 Lecture 14
Pictures of trees
15ADS2 Lecture 14
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
ADS2 Lecture 14 23
General Trees
• Nonlinear data structure• Natural way to organise data
• File system• GUI• Databases• Websites• Inheritance relation in Java classes• Books (chapters, sections, subsections, subsubsections)
• “nonlinear” organisational structure• Not just “before” “after”• “above” “below” “part of”
• Relationships are typically• Parent• Children• Ancestors• Descendents• Siblings
ADS2 Lecture 14 24
General TreesFormal definition
A tree T is either empty or consists of a node r, the root of T, and a (possibly empty) set of treeswhose roots are the children of r
An edge in T is a pair of nodes (u,v) where u is parent of v or v is parent of u
A tree T is a set of nodes with a parent-child relationshipEach node has one parent, apart from the root (which has no parent)
• a node• an edge• a path• a child• a parent• an ancestor• a descendant• siblings• depth of a node• height of a node• height of a tree• a leaf• an internal node• the root• a subtree
i
e
c
b
R
d jg
f h
a
X n
p Z
General Trees
• n nodes• n-1 edges• no cycles• unique path from a node to root
i
e
c
b
R
d jg
f h
a
X n
p Z
General Trees
ADS2 Lecture 14 27
How might we implement a general tree?
ADS2 Lecture 14
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Therefore we might implement a tree as follows
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Note: there could be order amongst the children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Depth
Depth of a node is how far it is from the root.
depth(Node<E> node) if (node.isRoot()) return 0; return 1 + depth(node.parent());
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Height
Height of a node is•If node is a leaf then 0•Otherwise 1 + the maximum of the height of its children
height(Node<E> node) if (node.isLeaf()) return 0; int h = 0; for (Node<E> child : node.children()) h = Math.max(h,height(child)); return 1 + h;
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); }
Whatever “visit” means
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); }
Whatever “visit” means
a c b R X n d p Z i g f h j
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Preorder Traversal
Visit the parent then visit its children
preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); }
Whatever “visit” means
O(n)
a c b R X n d p Z i g f h j
ADS2 Lecture 14 49
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Parenthetic Representation
(a (c (b ((R),(X),(n))),(d)),(p),(Z),(i (g ((f),(h))),(j)))
Also called Caley Notation (I think) and is a preorder print
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Postorder Traversal
Visit the children then visit the parent
Typical use is we want to “assemble” parts
An actual use: du in unix
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Postorder Traversal
Visit the children then visit the parent
postorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); }
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Postorder Traversal
Visit the children then visit the parent
prostorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); }
O(n)
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Postorder Traversal
Visit the children then visit the parent
prostorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); }
O(n)
R X n b d c p Z f h g j i a
ADS2 Lecture 14 55
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Other Traversals
Visit depth 0 nodesVisit depth 1 nodes …Visit depth height nodes
Also Known As (aka) Breadth First Search (bfs)
General Trees
i
e
c
b
R
d jg
f h
a
X n
p Z
Node:•<E> element•Node<E> parent•ArrayList<Node<E>> children
Tree<E>•Node<E> root
Other Traversals
Visit depth 0 nodesVisit depth 1 nodes …Visit depth height nodes
Also Known As (aka) Breadth First Search (bfs)
O(n)
a c p Z i b d g j R X n f h
Note: preorder = dfs!
ADS2 Lecture 14 58
Binary Trees
59
Binary TreesA binary tree is a tree in which each node has
• A reference to a left node• A value• A reference to a right node
A binary tree is either empty or Contains a single node r (the root)whose left and right subtrees are binary trees The tree is accessed via a reference to the root.The nodes with both child references null are the leaves.
recursive definition!
ADS2 Lecture 14
• nodes 0• intenal nodes 0• leaf nodes 0• edges 0• height ?
• nodes 1• intenal nodes 0• leaf nodes 1• edges 0• height 0
• nodes 3• intenal nodes 1• leaf nodes 2• edges 2• height 1
• nodes 7• intenal nodes 3• leaf nodes 4• edges 6• height 2
• nodes 15• intenal nodes 7• leaf nodes 8• edges 14• height 3
• nodes n• intenal nodes (nI) h ≤ nI ≤ 2h-1• leaf nodes (nE) 1 ≤ nE ≤ 2h • edges (e) e = n-1• height (h) h+1 ≤ n ≤2h+1-1
height (h) log2(n+1)-1 ≤ h ≤ n-1
• nodes 15• intenal nodes 7• leaf nodes 8• edges 14• height 3
height (h) log2(n+1)-1 ≤ h ≤ n-1
And this is crucial
i
e
c
b
a
d jg
f h
w
t
m
p zu
v
A Forest!
3
5
7
9
11
This is also a binary tree
69
This tree is quite well balanced.An extreme unbalanced tree might have no right pointers
- would look more like a linked list.Generally tree-based algorithms work most efficientlyon balanced trees.A binary tree in which no value occurs in more than one node is injective. We deal only with injective binary trees.
Balance
A typical binary tree:
i
e
c
b
a
d jg
f h
ADS2 Lecture 14
70
Node of binary treeBinary tree node:
ADS2 Lecture 14
public class BTNode<E>{ private E element; private BTNode<E> left; private BTNode<E> right;
/**Creates a node with null references*/ public BTNode(){ this(null,null,null); }
/** Creates node with the given element, L and R nodes*/ public BTNode(E e, BTNode<E> l,BTNode<E> r){ element = e; left=l; right=r; }
A question:
How would we represent the nodes of a tree (not binary)?
We don’t know how many children each node has
plus getters and setters
For a binary tree of strings
For a binary tree of strings
For a binary tree of strings
For a binary tree of strings
For a binary tree of strings
And what is that?
For a binary tree of strings
Definitions
ADS2 Lecture 14 77
p - a node of a binary treep.left - node of a binary tree - the left subtreep.right - node of a binary tree - the right subtreeif p.left = q - p is the parent of q and q is the left child of
pif p.right = q - p is the parent of q and q is the right child
of p.
i
e
c
b
a
d jg
f h
p
Left subtree of p
Right subtree of p
78
Traversals
A traversal of a binary tree is a sequence of nodes of the tree.
Special traversals:
1. Inorder traversal - defined recursively
The inorder traversal of an empty tree is the empty sequenceThe inorder traversal of a non-empty tree is: the inorder traversal of the left subtree (a sequence) the root value (a sequence with one member) the inorder traversal of the right subtree (a sequence)
ADS2 Lecture 14
79
Example (inorder traversal)
i
e
c
b
a
d jg
f h
Inorder traversal is a b c d e f g h i j
ADS2 Lecture 14
80
Traversals contd.2. Preorder traversal – defined recursively
The preorder traversal of an empty tree is the empty sequenceThe preorder traversal of a non-empty tree is:
the root value (a sequence with one member)the preorder traversal of the left subtree (a sequence)the preorder traversal of the right subtree (a sequence)
i
e
c
b
a
d jg
f h
Preorder traversal is e c b a d i g f h j
ADS2 Lecture 14
81
Traversals contd.
Postorder traversal defined recursively:
The postorder traversal of an empty tree is the empty sequenceThe postorder traversal of a non-empty tree is:
the postorder traversal of the left subtree (a sequence)the postorder traversal of the right subtree (a sequence)the root value (a sequence with one member)
i
e
c
b
a
d jg
f h
Postorder traversal is a b d c f h g j i e
ADS2 Lecture 14
i
e
c
b
a
d jg
f h
i
e
c
b
a
d jg
f h
i
e
c
b
a
d jg
f h
An Euler Walk
ADS2 Lecture 14 85
An ArrayList Representation of a Binary Tree
i
e
c
b
a
d jg
f h
Nodes have an integer positionWe put the nodes in an Array or ArrayList
ADS2 Lecture 14 86
An ArrayList Representation of a Binary Tree
i
e
c
b
a
d jg
f h
3
1
2
4
8
5 76
12 13
ADS2 Lecture 14 87
An ArrayList Representation of a Binary Tree
i
e
c
b
a
d jg
f h
3
1
2
4
8
5 76
12 13
We have an ArrayList<Node<E>> T A Node<E> has an integer position attributeThe root has position 1, i.e. T[1] is the root nodeT[i].position() == i
ADS2 Lecture 14 88
An ArrayList Representation of a Binary Tree
i
e
c
b
a
d jg
f h
3
1
2
4
8
5 76
12 13
We have an ArrayList<Node<E>> T A Node<E> has an integer position attributeThe root has position 1, i.e. T[1] is the root nodeT[i].position() == i
Left of T[i] is T[i*2]Right of T[i] is T[i*2 + 1]Parent of T[i] is T[i/2]
ADS2 Lecture 14 89
An Expression Tree (or species tree)Putting an expression into a tree
ADS2 Lecture 14 90
An Expression Tree (or species tree)Putting an expression into a tree
We use two stacks•S1 of Node<E> •S2 of String
ADS2 Lecture 14 91
An Expression Tree (or species tree)Putting an expression into a tree
We use two stacks•S1 of Node<E> •S2 of String
if we read “(“ S1.push(new Node(null,”!”,null))if we read is in {+,-,*,/} S2.push(operator)if we read “)” •Node r = S1.pop()•Node l = S1.pop()•Node node = S1.pop()•String op = S1.pop()•node.setLeft(l); node.setElement(op); node.setRight(r)•S1.push(node)if we read String s and it is something else (a leaf) S1.push(new Node(null,s,null))
ADS2 Lecture 14 92
No Expense Spared
.