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

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Example
Background image of page 4
5 Example
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
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.
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
Image of page 11
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 Right Arrow Icon
Ask a homework question - tutors are online