Trees I think that I shall never see Trees I A poem lovely as a tree -- J. Kilmer Trees Anatomy of a Tree a • In CS, we look at trees • Node from the bottom up – Basic element of the tree b c – Contains a piece of information d e f g – Represented by the circles h i j Anatomy of a Tree Anatomy of a Tree • Child • Sibling a a – Direct descendant of a node – Nodes that share the – Node a has 2 children: b same parent b c b c and c – Nodes b and c are – A node may have multiple siblings children d e f g d e f g • Leaf • Parent – Direct ancestor of a node – Node that has no – The parent of node b and c children are Node a h i j h i j – Nodes d,e,h,i, and j are – A node can have only 1 leaves parent 1
Anatomy of a Tree Anatomy of a Tree a • Root a • Root – Topmost node of the – Topmost node of the tree tree b c b c – The root is an orphan: – The root is an orphan: It has no parent. It has no parent. d e f g d e f g – Node a is the root of – Node a is the root of this tree. this tree. h i j h i j Anatomy of a Tree Anatomy of a Tree a a • Trees are strictly • Subtree acyclic – All children of a node, can be considered the b c – A child node cannot be b c root of it’s own tree a parent to one of its ancestors – These are called d e f g d e f g subtrees – This figure is not a valid tree – I smell recursion! – Node c has 2 parents! h i j h i j Anatomy of a Tree Anatomy of a Tree a • Balanced Tree a • Binary Tree – A tree is balanced if all – A binary tree is a tree child subtrees have where each node has at b c b c similar depths most 2 children. – This tree is not – This tree is not a binary d e f g d e f g balanced since Node tree c’s subtree has a depth of 2 but Node b’s h i j h i j subtree has a depth of 1. 2
Anatomy of a Tree Anatomy of a Tree a • Binary Tree a • Full Binary Tree – But this tree is – A binary tree is full if b c b c • All of it’s leaf nodes are – Children of nodes in a of the same depth binary tree are referred • Each non-leaf node has to as left child or right d e f g d e f g 2 children child – This binary tree is not • Node h is the left child full. of Node f h i h i • Node i is the right child of Node f Anatomy of a Tree Anatomy of a Tree a a • Complete Binary Tree • Full Binary Tree – A binary tree is – But this one is. complete if b c b c • Each level (except the deepest) must contain as many nodes as d e f g d e f g possible • At the deepest level, all nodes as as far left as possible h i – This binary tree is not complete. Anatomy of a Tree Traversing a Tree a • Complete Binary Tree • A means to process all the nodes in a tree – But this one is! – A traversal starts at the root b c – Visits each node exactly once • Questions? • “Processes” the data in a node when visited d e f g – Nodes can be visited in different orders • Breadth-first traversal • Depth-first traversal h i 3
Anatomy of a Tree Anatomy of a Tree a • Breadth-first a • Depth-first – All the nodes at a given – At each node, the node level are visited before is visited as well as it’s b c b c the nodes at the next children’s subtrees. level – Different types based d e f g d e f g – Example: on order • a,b,c,d,e,f,g,h,i • Pre-order • In-order h i h i • Post-order Anatomy of a Tree Anatomy of a Tree a a • Pre-order • in-order – At each node – At each node b c • The node is visited first b c • In-order traversal of the left subtree • Pre-order traversal of the left subtree • The node is visited next d e f g d e f g • Pre-order traversal of • The In-order traversal the right subtree of the right subtree – Example: – Example: h i • a,b,d,h,i,e,c,f,g h i • h,d,i,b,e,a,f,c,g Anatomy of a Tree What are trees good for? a • post-order • Hierarchical relationships – At each node Performer b c • Post-order traversal of the left subtree isA isA • Post-order traversal of Actor Musician d e f g the right subtree • The node is visited next – Example: Guitarist Pianist Drummer h i • h,i,d,e,b,f,g,c,a 4
What are trees good for? What are trees good for? • Binary trees can be used to represent • Branching can also imply ordering of node decision taxonomies data Mammal? 45 no > yes < Bigger than a cat? Underwater? 30 55 yes no < > no yes > < Elephant Mouse Trout Bird 22 35 50 60 Summary Next Time • Trees • Coding a binary tree in Java – Terminology • Root • Children • Leaf – Trees are acyclic – Traversal • Breadth-first • Depth-first – Pre-order – In-order – Post-Order 5
Recommend
More recommend