lec04-trees

# lec04-trees - What is a (General) Tree? A (general) tree is...

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

What is a (General) Tree? A (general) tree is a set of nodes with the following properties: The set can be empty. Otherwise, the set is partitioned into k+1 disjoint subsets: a tree consists of a distinguished node r, called root , and zero or more nonempty subtrees T 1 , T 2 , … , T k , each of whose roots are connected by an edge to r. 17/10/2011 CS202 - Fundamentals of Computer Science II 1 • T is a tree if either: – T has no nodes, or – T is of the form r . . . T 1 T 2 . . . . . . T k where r is a node and T 1 , T 2 , . .., T k are trees.

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

View Full Document
What is a (General) Tree? (cont.) • The root of each subtree is said to be a child of r, and r is the parent of each subtree’s root. • If a tree is a collection of N nodes, then it has N-1 edges. 17/10/2011 CS202 - Fundamentals of Computer Science II 2 A path from node n 1 to n k is defined as a sequence of nodes n 1 ,n 2 , …,n k such that n i is the parent of n i+1 (1 i < k): – There is a path from every node to itself. – There is exactly one path from the root to each node.
A Tree – Example A B C D E F G H I J K L M N 17/10/2011 CS202 - Fundamentals of Computer Science II 3 P Q –Node A has 6 children : B, C, D, E, F, G. –B, C, H, I, P, Q, K, L, M, N are leaves in the tree above. –K, L, M are siblings since F is their parent.

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

View Full Document
Tree Terminology Parent – The parent of node n is the node directly above n in the tree. Child – The child of node n is the node directly below n in the tree. – If node m is the parent of node n, node n is the child of node m. Root – The only node in the tree with no parent. Leaf – A node with no children. 17/10/2011 CS202 - Fundamentals of Computer Science II 4 Siblings – Nodes with a common parent. Ancestor – An ancestor of node n is a node on the path from the root to n. Descendant – A descendant of node n is a node on the path from n to a leaf. Subtree – A subtree of node n is a tree that consists of a child (if any) of n and the child’s descendants (a tree which is rooted by a child of node n).
Level of a Node Level – The level of node n is the number of nodes on the path from the root to node n. Definition – The level of node n in a tree T: – If n is the root of T, the level of n is 1. 17/10/2011 CS202 - Fundamentals of Computer Science II 5 – If n is not the root of T, its level is 1 greater than the level of its parent.

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

View Full Document
Height of a Tree Height – The number of nodes on the longest path from the root to a leaf. • The height of a tree T in terms of the levels of its nodes is defined as: – If T is empty, its height is 0. – If T is not empty, its height is equal to the maximum level of its nodes. 17/10/2011 CS202 - Fundamentals of Computer Science II 6 • Or, the height of a tree T can be defined recursively as: – If T is empty, its height is 0.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/01/2011 for the course CS 202 taught by Professor Amaşlm during the Spring '11 term at Bilkent University.

### Page1 / 90

lec04-trees - What is a (General) Tree? A (general) tree is...

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

View Full Document
Ask a homework question - tutors are online