Chapter 7. Trees & Binary Trees Lecture 14 Chapter 7. Trees & Binary Trees Set ADT Introduction to trees and binary trees ADS2 Lecture 14
Pictures of trees File System
Pictures of trees
ADS2 Lecture 14
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees unrooted
Pictures of trees unrooted
unrooted Pictures of trees
unrooted Pictures of trees ADS2 Lecture 14 12
Pictures of trees ADS2 Lecture 14 13
Pictures of trees ADS2 Lecture 14 14
Pictures of trees ADS2 Lecture 14 15
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Pictures of trees
Nonlinear data structure Natural way to organise data File system GUI 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
A tree T is a set of nodes with a parent-child relationship Formal definition General Trees A tree T is a set of nodes with a parent-child relationship Each node has one parent, apart from the root (which has no parent) A tree T is either empty or consists of a node r, the root of T, and a (possibly empty) set of trees whose 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 ADS2 Lecture 14
i e c b R d j g f h General Trees a node an edge a path a 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 j g f h a X n p Z
i e c b R d j g f h General Trees n nodes n-1 edges a no cycles X n p Z n nodes n-1 edges no cycles unique path from a node to root
How might we implement a general tree? ADS2 Lecture 14
i e c b R d j g f h General Trees a p Z X n Therefore we might implement a tree as follows Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children Note: there could be order amongst the children ADS2 Lecture 14
i e c b R d j g f h Depth General Trees a Tree<E> X n p Z Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children 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());
i e c b R d j g f h Height General Trees a Tree<E> X n p Z Tree<E> Node<E> root Node: <E> element Node<E> parent ArrayList<Node<E>> children 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;
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the parent then visit its children
i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children 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 i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c b R X n d p Z i g f h j 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 i e c b R d j g f h Preorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c b R X n d p Z i g f h j Visit the parent then visit its children preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } O(n) Whatever “visit” means
ADS2 Lecture 14
i e c b R d j g f h Parenthetic Representation General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children (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
i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the children then visit the parent Typical use is we want to “assemble” parts An actual use: du in unix
i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit the children then visit the parent postorder(Node<E> node) if (node != null) { for (Node<E> child : node.children()) postorder(child); visit(node); }
O(n) i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children 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)
O(n) R X n b d c p Z f h g j i a i e c b R d j g f h Postorder Traversal General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children R X n b d c p Z f h g j i a 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)
ADS2 Lecture 14
i e c b R d j g f h Other Traversals General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children Visit depth 0 nodes Visit 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 i e c b R d j g f h Other Traversals General Trees Tree<E> Node<E> root i e c b R d j g f h a X n p Z Node: <E> element Node<E> parent ArrayList<Node<E>> children a c p Z i b d g j R X n f h Visit depth 0 nodes Visit depth 1 nodes … Visit depth height nodes O(n) Also Known As (aka) Breadth First Search (bfs) Note: preorder = dfs!
Binary Trees ADS2 Lecture 14
Binary Trees A 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
And this is crucial height (h) log2(n+1)-1 ≤ h ≤ n-1
A Forest! t m w p u z v
This is also a binary tree 3 5 7 9 11
Balance A typical binary tree: 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 efficiently on balanced trees. A binary tree in which no value occurs in more than one node is injective. We deal only with injective binary trees. ADS2 Lecture 14
Node of binary tree Binary tree node: A question: How would we represent the nodes of a tree (not binary)? We don’t know how many children each node has Binary tree node: 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; plus getters and setters ADS2 Lecture 14
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 p - a node of a binary tree p.left - node of a binary tree - the left subtree p.right - node of a binary tree - the right subtree if p.left = q - p is the parent of q and q is the left child of p if p.right = q - p is the parent of q and q is the right child of p. p Left subtree of p Right subtree of p ADS2 Lecture 14
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 sequence The 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
Example (inorder traversal) Inorder traversal is a b c d e f g h i j ADS2 Lecture 14
Traversals contd. 2. Preorder traversal – defined recursively The preorder traversal of an empty tree is the empty sequence The 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) Preorder traversal is e c b a d i g f h j ADS2 Lecture 14
Traversals contd. Postorder traversal defined recursively: The postorder traversal of an empty tree is the empty sequence The 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) Postorder traversal is a b d c f h g j i e ADS2 Lecture 14
An Euler Walk
An ArrayList Representation of a Binary Tree Nodes have an integer position We put the nodes in an Array or ArrayList ADS2 Lecture 14
An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 ADS2 Lecture 14
An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 We have an ArrayList<Node<E>> T A Node<E> has an integer position attribute The root has position 1, i.e. T[1] is the root node T[i].position() == i ADS2 Lecture 14
An ArrayList Representation of a Binary Tree 3 1 2 4 8 5 7 6 12 13 We have an ArrayList<Node<E>> T A Node<E> has an integer position attribute The root has position 1, i.e. T[1] is the root node T[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
Putting an expression into a tree An Expression Tree (or species tree) ADS2 Lecture 14
Putting an expression into a tree An Expression Tree (or species tree) We use two stacks S1 of Node<E> S2 of String ADS2 Lecture 14
Putting an expression into a tree An Expression Tree (or species 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
No Expense Spared ADS2 Lecture 14
.