970 likes | 1.22k Views
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.
E N D
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 unrooted
Pictures of trees unrooted
Pictures of trees unrooted
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
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
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
a e c i b d g j f h R General 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 Z p n X
a e c i b d g j f h R General Trees • n nodes • n-1 edges • no cycles • unique path from a node to root Z p n X
How might we implement a general tree? ADS2 Lecture 14
a e c i b d g j f h R General Trees Z p Therefore we might implement a tree as follows • Tree<E> • Node<E> root n X • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Note: there could be order amongst the children ADS2 Lecture 14
a e c i b d g j f h R Depth General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p Depth of a node is how far it is from the root. n X depth(Node<E> node) if (node.isRoot()) return 0; return 1 + depth(node.parent());
a e c i b d g j f h R Height General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p • Height of a node is • If node is a leaf then 0 • Otherwise 1 + the maximum of the height of its children n X 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;
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p a c b R X n d p Z i g f h j Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means
a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p a c b R X n d p Z i g f h j Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } O(n) Whatever “visit” means
a e c i b d g j f h R Parenthetic Representation General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X (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