Professional Documents
Culture Documents
Trees
Rooted tree: collection of elements called nodes, one of which is distinguished as root, along with a relation ("parenthood") that imposes a hierarchical structure on the nodes Formal definiton:
Terminology. Rooted Trees. Traversals. Labeled Trees and Expression Trees. Tree ADT. Tree Implementations. Binary Search Trees. Optimal Search Trees
A single node by itself = tree. This node is also the root of the tree Assume n = node and T1, T2, ..., Tk = trees with roots n1, n2, ..., nk: construct a new tree by making n be the parent of nodes n1, n2, ..., nk
i k. length(path): no. of nodes -1 The depth of a vertex v V is depth(v) = the length of the path from r to v The height of a vertex v V is height(v) = the length of the longest path from v to a leaf The height of the tree T is height(T) = height(r) The level of a vertex v V is level(v) = height(T) depth(v) The subtree generated by a vertex v V is a tree consisting of root v and all its descendants in T.
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 4
m = 2 binary; m = 3 ternary
full m-ary if each internal vertex has exactly m children. complete m-ary if it is full and all leaves are at level 0. Maximum height for a tree with n vertices is n 1. Maximum height for a full binary tree with n vertices is (n 1)/2 The minimum height for a binary tree with n vertices is log2 n
Some limits:
1
Traversals
Ancestral information
Traversals in preorder and postorder are useful for obtaining ancestral information. Suppose
post(n) is the position of node n in a postorder listing of the nodes of a tree, and desc(n) is the number of proper descendants of node n. Then nodes in the subtree with root n are numbered consecutively from post(n)-desc(n) to post(n)
To test if a node x is a descendant of node y: post(y)desc(y) post(x) post(y) Similar relationship for preorder
Traversal example
Labelled tree:Label (value) associated with each node Expression tree: The internal vertices represent operators, and the leaves represent operands.
preorder: 1, 2, 5, 3, 6, 10, 7, 11, 12, 4, 8, 9. postorder: 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1. inorder: 5, 2, 1, 10, 6, 3, 11, 7, 12, 8, 4, 9.
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 9
binary operator : 1st operand on the left leaf 2nd operand on the right leaf unary operator : single operand on the right leaf
10
infix form:
ADT Tree
ADT that supports the folowing operations:
parent(n, T): returns parent of node n in tree T. For root returns null tree (denoted ).
leftmostChild(n, T): returns the leftmost child of node n in tree T or for a leaf
rightSibling(n, T): returns the right sibling of node n in tree T or for the rightmost sibling
Input: node, tree; Output: node or Input: node, tree; Output: label
label(n, T): returns the label (associated value) of node n in tree T root(T): returns the root of T
15
16
17
18
BST implementation
typedef struct t_node { void *item; struct t_node *left; struct t_node *right } NodeT;
Binary search tree property: for every node, say x, in the tree,
Typical representation:linked representation of binary tree, with key, left, right, [and p (parent)] fields.
19
20
return n->item;
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 21 T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 22
Find performance
Height, h
Number of nodes, n
n = 1 + 21 + 22 + + 2h h = log2 n
= 2h+1 - 1
Complete Tree Since we need at most h+1 comparisons, find in O(h+1) or O(log n) Same as binary search
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 23
24
Addition implementation
static void AddToTree( NodeT **t, NodeT *new ) { NodeT *base = *t; /* If it's a null tree, just add it here */ if ( base == NULL ) { *t = new; return; } else if( KeyLess(ItemKey(new->item), ItemKey(base->item)) ) AddToTree( &(base->left), new ); else AddToTree( &(base->right), new ); }
Example:
1. Delete 8, 13, 21 or 37 2. Delete 22 3. Delete 12, 20 or 29
25
26
27
28
BST performance
Find c log n Add c log n Delete c log n Apparently efficient in every respect! Take this list of characters and form a tree A B C D E F unbalanced
Performance comparison
Arrays
Add Delete Find Simple, fast Inflexible O(1) O(n) inc sort O(n) O(n) O(logn) binary search
Linked List
Simple Flexible O(1) sort -> no adv O(1) - any O(n) - specific O(n) (no bin search)
Trees
Still Simple Flexible
O(log n)
29
30
Static BSTs
Reduce time of search:
Optimal trees
Optimal BST: minimizes expected search time Problem: given frequency if each key, build an optimal tree Solutions:
More frequently accessed keys kept closer to root Keys a, b, c f(a)=0.35, f(b)=0.3, f(c)=0.35. Five possible BSTs
Example:
Brute force: too many trees to check because the number of binary trees of n nodes is
Here symmetric case (c) is best. Number of comparisons: 2 0.35 + 1 0.3 + 2 0.35 = 1.7 f(a)=0.45, f(b)=0.1, f(c)=0.45 => (b) and (d) are better than (c)
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 31
32
Optimal trees
Let
Optimal trees
k1< k2< < kn be the keys in order, and n pi be the probability of accessing ki , then pi = 1 i =1 we omit consideration of unsuccessful searches 1jkn T be any tree constructed from the keys k1, k2, , kn DepthT (ki ) j i k the depth in T of the node holding ki Cost of tree T
C (T ) = pi ( DepthT (ki ) + 1)
i= j k
Objective: find a tree T on all n keys that minimizes C(T) Every subtree of an optimal tree is itself optimal:
If T is an optimal tree for kj, , kk and its root is kl Then its left subtree must be optimal for kj, , kl-1,
33
Optimal tree: its cost is as small as the cost of any other tree with the same keys
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos
and its right subtree must be optimal for kl+1, , kk If left and right subtrees of T are TLand TR Then the depth of each node increases by 1 a when it is viewed as a node of T
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 34
Optimal trees
It follows that if kl is root of T
Optimal trees
Therefore, replacing TLand TR by any tree on the same nodes with lower cost => a tree of lower cost than T
T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 35
kl
T(j, l-1)
T(l+1, k)
36
Optimal trees
Reading
AHU, chapter 3 CLR, chapters 11.3, 11.4 Preiss, chapter: Trees. Knuth, vol. 1, 2.3 Notes
MinIndex returns an index l such that j l k and C[j, l-1]+C[l+1, k] is minimized. This function can be implemented simply by searching through all the possibilities j, i+1, , k.
37
38