CS1102C-Lecture_7-tree

# CS1102C-Lecture_7-tree - Trees 1 1 Readings...

This preview shows pages 1–23. Sign up to view the full content.

1 1 Trees

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

View Full Document
2 2 Readings ± Required [Carrano & Prichard] Chapter 10

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

View Full Document
4 4 A Tree as a Data Structure • shown upside down • Used to represent relationships • has a hierarchy
5 5 Definitions node edge Data objects (the circles) in a tree are called nodes . Links between nodes are called edges .

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

View Full Document
6 6 Relationship ± A is a parent of B and C ± B and C are children of A ± B and C are siblings (with the same parent A) D E A B F C
7 7 Relationship ± D is an ancestor of B. ± B is a descendant of A and D. ± Definition: Node X is an ancestor of node Y if ² X is a parent of Y, or ² X is a parent of some node Z and Z is an ancestor of Y. D E A B F C

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

View Full Document
8 8 Tree Nodes root (has no parent) internal nodes (has one parent and at least one child) leaves (has no children) Every node (except the root) of a tree has one parent. A node with no children is a leaf node.
9 9 subtree Subtree A node and all of its descendants form a subtree Q: Can a leaf be a subtree?

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

View Full Document
10 10 Tree is recursive ! A tree is either • nothing, or • A node, with some set of subtrees, each of which is a tree ...
11 11 Level of a node ± Number of nodes on the path from the root to the node ² level of root is 1 ² level of A is 2 A B C level

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

View Full Document
12 12 Height of a tree ± Maximum level of the nodes in the tree is the height of the tree A B C height = 4
13 13 Size of a tree ± Number of nodes in the tree is the size of the tree A • The size of this tree is 10. • The size of the subtree rooted at A is 4.

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

View Full Document
14 14 Applications of Trees A tree can be used to represent data that is hierarchical in Nature
15 15 File systems Desktop My Documents My Computer C: D: H: I:

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

View Full Document
16 16 Arithmetic Expressions (a+b) * (a-b) + 2 + 2 * - + a b a b Q: How do you construct such a tree from a given arithmetic expression?
17 17 General Trees ± An n -ary tree ² A tree whose nodes each can have no more than n children

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

View Full Document
18 18 Binary Trees Each node has at most 2 ordered children
19 19 Binary Tree Each node has at most 2 ordered children. Q: What is the meaning of “ order ”?

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

View Full Document
20 20 Binary Tree is Recursive
21 21 Full Binary Tree ± All nodes at a level < h have two children. (where h is the height of the tree) h Q: Is this definition the same as “all nodes except the leaf nodes have 2 children”?

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

View Full Document
22 Complete Binary Tree ± Full down to level h-1 ± level h filled in from left to right. h
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 76

CS1102C-Lecture_7-tree - Trees 1 1 Readings...

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

View Full Document
Ask a homework question - tutors are online