Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7. Trees & Binary Trees

Similar presentations


Presentation on theme: "Chapter 7. Trees & Binary Trees"— Presentation transcript:

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

2 Pictures of trees File System

3 Pictures of trees

4 ADS2 Lecture 14

5 Pictures of trees

6 Pictures of trees

7 Pictures of trees

8 Pictures of trees

9 Pictures of trees unrooted

10 Pictures of trees unrooted

11 unrooted Pictures of trees

12 unrooted Pictures of trees ADS2 Lecture 14 12

13 Pictures of trees ADS2 Lecture 14 13

14 Pictures of trees ADS2 Lecture 14 14

15 Pictures of trees ADS2 Lecture 14 15

16 Pictures of trees

17 Pictures of trees

18 Pictures of trees

19 Pictures of trees

20 Pictures of trees

21 Pictures of trees

22 Pictures of trees

23 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

24 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

25 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

26 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

27 How might we implement a general tree?
ADS2 Lecture 14

28 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

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

30 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;

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 ADS2 Lecture 14

50 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

51 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

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

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

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

55 ADS2 Lecture 14

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

57 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!

58 Binary Trees ADS2 Lecture 14

59 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

60 nodes intenal nodes 0 leaf nodes edges height ?

61 nodes intenal nodes 0 leaf nodes edges height

62 nodes intenal nodes 1 leaf nodes edges height

63 nodes intenal nodes 3 leaf nodes edges height

64 nodes intenal nodes 7 leaf nodes edges height

65 nodes n intenal nodes (nI) h ≤ nI ≤ 2h-1 leaf nodes (nE) ≤ 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 intenal nodes 7 leaf nodes edges height

66 And this is crucial height (h) log2(n+1)-1 ≤ h ≤ n-1

67 A Forest! t m w p u z v

68 This is also a binary tree
3 5 7 9 11

69 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

70 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

71 For a binary tree of strings

72 For a binary tree of strings

73 For a binary tree of strings

74 For a binary tree of strings

75 For a binary tree of strings
And what is that?

76 For a binary tree of strings

77 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

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

79 Example (inorder traversal)
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 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

81 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

82

83

84 An Euler Walk

85 An ArrayList Representation of a Binary Tree
Nodes have an integer position We put the nodes in an Array or ArrayList ADS2 Lecture 14

86 An ArrayList Representation of a Binary Tree
3 1 2 4 8 5 7 6 12 13 ADS2 Lecture 14

87 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

88 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

89 Putting an expression into a tree An Expression Tree (or species tree)
ADS2 Lecture 14

90 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

91 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

92 No Expense Spared ADS2 Lecture 14

93 .


Download ppt "Chapter 7. Trees & Binary Trees"

Similar presentations


Ads by Google