day22 - COP 3503 Computer Science II CLASS NOTES DAY#22...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #22 TREES Techniques for defining a tree There are two basic techniques that can be used to define a tree. 1. Recursively : this allows for very simple algorithms to manipulate the tree. 2. Non-recursively : this allows for very straight-forward, easy and direct algorithms for manipulation of the tree. We did this in Chapter 6/7. Non-recursive Definition Tree: A tree is a set of nodes and a set of directed edges which connect pairs of nodes. It is a connected, cycle-free, directed graph. Rooted Tree: A tree in which one node is specified to be the root , (call it node c ). Every node (other than c ), call it b is connected by exactly one edge to exactly one other node, call it p . Given this situation, p is b ’s parent. Further, b is one of p ’s children. Path length: A unique path traverses from the root to each node in the tree. The number of edges along the path is the path length. Height: The number of edges from a specific node to its deepest leaf. Depth : The number of edges from the root to a specific node. Siblings: Nodes which have the same parent. Descendant: If there is a path from node a to node b then b is a descendant of a and a is an ancestor of b . Day 22 - 1 Note: A tree with n nodes has exactly n-1 edges since all nodes in the tree, except for the root, have exactly one edge leading to it. Chapter 17
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Proper descendant: If b is a descendant of a and a b then b is a proper descendant of a and a is a proper ancestor of b . The following diagrams illustrate the various properties of trees. There are x total nodes and thus x-1 total edges in the tree shown above. The depth of the tree to node c 1 is 2. Depth(1) = 1. Height(3) = 2, Height(2) = 0 Depth(root) = 0, Height(root) = 3 Node 3 is an ancestor of node 7. Node7 is a proper descendant of node 3. Nodes 4 and 5 are siblings. Day 22 - 2 c b b b c c c m m root c 1 2 3 4 5 6 7
Background image of page 2
Recursive Definition Basis: A tree is empty. Recursive step: A tree consists of a root node and zero or more non-empty subtrees whose roots are connected to the root node. Closure: Nothing else is a tree. root n subtrees (0 n ) Implementation The implementation must account for an unknown, potentially unbounded, and potentially widely varying number of children using some kind of data structure (such as a linked list) rather than explicit references from the node itself (this is too wasteful of space). This means that a node definition of: [data, chld1ptr, chld2ptr, chld3ptr, chld4ptr, …, chldnptr] won’t do! This is commonly done using a first child/next sibling technique in which the children of a node are maintained in a linked list of tree nodes.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2009 for the course COP 3503c taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 15

day22 - COP 3503 Computer Science II CLASS NOTES DAY#22...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online