Tree 1

# Tree 1 - Trees Introduction General Tree A tree is a...

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

1 Trees - Introduction z General Tree A tree is a collection of nodes linked in a tree- like structure. (root, parent, child, edge, depth, height) z Binary Search Tree – File directory in operating systems. – To evaluate arithmetic expressions. – Show how to use trees to support searching operations in O (log n ) average time.

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

View Full Document
2 Trees - Introduction z AVL Trees – It is a tree that has a balance condition so that the search of the tree is bounded. z B-Trees – This is a general m -ary tree for searching.
3 Preliminaries z A tree is a collection of nodes. z The collection can be empty, which is sometimes denoted as A . z Otherwise, a tree consists of a distinguished node r , called the root, and zero or more (sub)trees T 1 , T 2 , . . . , T k , each of whose roots are connected by a directed edge to r . z The root of each subtree is said to be a child of r , and r is the parent of each subtree root.

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

View Full Document
4 Example
5 Example

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

View Full Document
6 Definition z The root is A . Node F has A as a parent and K , L , and M as children. z Each node may have an arbitrary number of children , possibly zero. z Nodes with no children are known as leaves ; the leaves in the tree above are B , C , H , I , P , Q , K , L , M , and N . z Nodes with the same parent are siblings ; thus K , L , and M are all siblings. z Grandparent and grandchild relations can be defined in a similar manner
7 Path Definition z 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 for 1 i < k . z The length of this path is the number of edges on the path, namely k -1. z There is a path of length zero from every node to itself. z Notice that in a tree there is exactly one path from the root to each node.

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

View Full Document
8 Depth Definition z For any node n i , the depth of n i is the length of the unique path from the root to n i . Thus, the root is at depth 0. z The height of n i is the longest path from n i to a leaf. Thus all leaves are at height 0. z The height of a tree is equal to the height of the root. z The depth of a tree is equal to the depth of the deepest leaf; this is always equal to the height of the tree.
9 Example E is at depth 1 and height 2. F is at depth 1 and height 1. • The height of the tree is 3.

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

View Full Document
Implementation of Trees z One way to implement a tree would be to have in each node, besides its data, a pointer to each child of the node. z What if the node has many children? z How do you declare it in advance? z
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

### Page1 / 48

Tree 1 - Trees Introduction General Tree A tree is a...

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

View Full Document
Ask a homework question - tutors are online