Chapter 7. Trees & Binary Trees

93
1 Chapter 7. Trees & Binary Trees - Set ADT - Introduction to trees and binary trees Lecture 14 ADS2 Lecture 14

description

Lecture 14. Chapter 7. Trees & Binary Trees. Set ADT Introduction to trees and binary trees. Pictures of trees. File System. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. unrooted. Pictures of trees. unrooted. - PowerPoint PPT Presentation

Transcript of Chapter 7. Trees & Binary Trees

Page 1: Chapter 7.  Trees & Binary Trees

1

Chapter 7. Trees & Binary Trees

- Set ADT

- Introduction to trees and binary trees

Lecture 14

ADS2 Lecture 14

Page 2: Chapter 7.  Trees & Binary Trees

Pictures of trees

File System

Page 3: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 4: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 4

Page 5: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 6: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 7: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 8: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 9: Chapter 7.  Trees & Binary Trees

Pictures of treesunrooted

Page 10: Chapter 7.  Trees & Binary Trees

Pictures of treesunrooted

Page 11: Chapter 7.  Trees & Binary Trees

Pictures of treesunrooted

Page 12: Chapter 7.  Trees & Binary Trees

12ADS2 Lecture 14

unrooted Pictures of trees

Page 13: Chapter 7.  Trees & Binary Trees

13ADS2 Lecture 14

Pictures of trees

Page 14: Chapter 7.  Trees & Binary Trees

14ADS2 Lecture 14

Pictures of trees

Page 15: Chapter 7.  Trees & Binary Trees

15ADS2 Lecture 14

Pictures of trees

Page 16: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 17: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 18: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 19: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 20: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 21: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 22: Chapter 7.  Trees & Binary Trees

Pictures of trees

Page 23: Chapter 7.  Trees & Binary 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

Page 24: Chapter 7.  Trees & Binary Trees

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)

Page 25: Chapter 7.  Trees & Binary Trees

• 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

Page 26: Chapter 7.  Trees & Binary 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

Page 27: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 27

How might we implement a general tree?

Page 28: Chapter 7.  Trees & Binary Trees

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

Page 29: Chapter 7.  Trees & Binary Trees

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());

Page 30: Chapter 7.  Trees & Binary Trees

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;

Page 31: Chapter 7.  Trees & Binary Trees

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

Page 32: Chapter 7.  Trees & Binary Trees

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

Page 33: Chapter 7.  Trees & Binary Trees

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

Page 34: Chapter 7.  Trees & Binary Trees

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

Page 35: Chapter 7.  Trees & Binary Trees

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

Page 36: Chapter 7.  Trees & Binary Trees

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

Page 37: Chapter 7.  Trees & Binary Trees

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

Page 38: Chapter 7.  Trees & Binary Trees

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

Page 39: Chapter 7.  Trees & Binary Trees

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

Page 40: Chapter 7.  Trees & Binary Trees

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

Page 41: Chapter 7.  Trees & Binary Trees

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

Page 42: Chapter 7.  Trees & Binary Trees

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

Page 43: Chapter 7.  Trees & Binary Trees

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

Page 44: Chapter 7.  Trees & Binary Trees

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

Page 45: Chapter 7.  Trees & Binary Trees

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

Page 46: Chapter 7.  Trees & Binary Trees

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

Page 47: Chapter 7.  Trees & Binary Trees

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

Page 48: Chapter 7.  Trees & Binary Trees

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

Page 49: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 49

Page 50: Chapter 7.  Trees & Binary Trees

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

Page 51: Chapter 7.  Trees & Binary Trees

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

Page 52: Chapter 7.  Trees & Binary Trees

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); }

Page 53: Chapter 7.  Trees & Binary Trees

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)

Page 54: Chapter 7.  Trees & Binary Trees

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

Page 55: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 55

Page 56: Chapter 7.  Trees & Binary Trees

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)

Page 57: Chapter 7.  Trees & Binary Trees

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!

Page 58: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 58

Binary Trees

Page 59: Chapter 7.  Trees & 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

Page 60: Chapter 7.  Trees & Binary Trees

• nodes 0• intenal nodes 0• leaf nodes 0• edges 0• height ?

Page 61: Chapter 7.  Trees & Binary Trees

• nodes 1• intenal nodes 0• leaf nodes 1• edges 0• height 0

Page 62: Chapter 7.  Trees & Binary Trees

• nodes 3• intenal nodes 1• leaf nodes 2• edges 2• height 1

Page 63: Chapter 7.  Trees & Binary Trees

• nodes 7• intenal nodes 3• leaf nodes 4• edges 6• height 2

Page 64: Chapter 7.  Trees & Binary Trees

• nodes 15• intenal nodes 7• leaf nodes 8• edges 14• height 3

Page 65: Chapter 7.  Trees & Binary Trees

• 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

Page 66: Chapter 7.  Trees & Binary Trees

height (h) log2(n+1)-1 ≤ h ≤ n-1

And this is crucial

Page 67: Chapter 7.  Trees & Binary Trees

i

e

c

b

a

d jg

f h

w

t

m

p zu

v

A Forest!

Page 68: Chapter 7.  Trees & Binary Trees

3

5

7

9

11

This is also a binary tree

Page 69: Chapter 7.  Trees & Binary Trees

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

Page 70: Chapter 7.  Trees & Binary Trees

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

Page 71: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

Page 72: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

Page 73: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

Page 74: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

Page 75: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

And what is that?

Page 76: Chapter 7.  Trees & Binary Trees

For a binary tree of strings

Page 77: Chapter 7.  Trees & Binary Trees

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

Page 78: Chapter 7.  Trees & Binary Trees

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

Page 79: Chapter 7.  Trees & Binary Trees

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

Page 80: Chapter 7.  Trees & Binary Trees

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

Page 81: Chapter 7.  Trees & Binary Trees

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

Page 82: Chapter 7.  Trees & Binary Trees

i

e

c

b

a

d jg

f h

Page 83: Chapter 7.  Trees & Binary Trees

i

e

c

b

a

d jg

f h

Page 84: Chapter 7.  Trees & Binary Trees

i

e

c

b

a

d jg

f h

An Euler Walk

Page 85: Chapter 7.  Trees & Binary Trees

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

Page 86: Chapter 7.  Trees & Binary Trees

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

Page 87: Chapter 7.  Trees & Binary Trees

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

Page 88: Chapter 7.  Trees & Binary Trees

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]

Page 89: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 89

An Expression Tree (or species tree)Putting an expression into a tree

Page 90: Chapter 7.  Trees & Binary Trees

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

Page 91: Chapter 7.  Trees & Binary Trees

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))

Page 92: Chapter 7.  Trees & Binary Trees

ADS2 Lecture 14 92

No Expense Spared

Page 93: Chapter 7.  Trees & Binary Trees

.