Title: TREES Fundamentals of
1Chapter 5 TREESFundamentals of Data
Structure in C Horowitz, Sahni and
Anderson-Freed Computer Science Press July, 1997
2Trees
3Trees
- Introduction
- Def) a tree is finite set of one or
- more nodes such that
- 1) there is a special node (root)
- 2) remaining nodes are partitioned
- into n ? 0 disjoint trees
- T1,T2,,Tn where each of these
- is a tree we call each Ti
- subtree of the root
- - acyclic graph contain no cycle
- - hierarchical structure
4Trees
Level
1
2
3
4
5Trees
- terminology
- - degree of a node
- the number of subtrees of node
- - degree of a tree the maximum
- degree of the nodes in the tree
- - leaf(terminal) node
- a node with degree zero
- (K, L, F, G, M, I, J)
- - internal(non-terminal) node
- node with degree one or more
- (A, B, C, D, E, H)
6Trees
- - parent a node that has subtrees
- - child a root of the subtrees
- - sibling child nodes of the same
- parent
- - ancestor all the nodes along the
- path from the root to the node
- - descendant all the nodes that are
- in its subtrees
- - level level of nodes parent 1
- (level of the root node is 1)
- - depth (or height) of a tree
- the maximum level of any node in
- the tree
7Trees
- Representation of trees
- list representation
- - the information in the root node
- comes first
- - followed by a list of the subtrees
- of that node
- (eg) (A(B(E(K,L),F),C(G),D(H(M),I,J)))
- representation of a tree in memory
- where n is the degree of the tree
data
link 1
link 2
link n
8Trees
- Representation of trees
- left-child right-sibling representation
- nodes of a fixed size
- - easier to work
- - two link / pointer fields per node
- left-child right-sibling node structure
9Trees
- left-child right-sibling representation of a tree
10Trees
- Representation of trees
- representation as a degree two tree
- simply rotate the left-child
- right-sibling tree clockwise
- by 45 degrees
- two children
- - left child and right child
- - degree 2
- - binary tree
11Trees
- left-child right-child tree representation of a
tree
12Trees
tree
left child-right sibling tree
binary tree
tree
left child-right sibling tree
binary tree
13Binary Trees
14Binary Trees
- Def)a binary tree is a finite set of
- nodes such that
- 1) empty or
- 2) consists of root node and two
- disjoint binary trees, called,
- left subtree and right subtree
- two different binary trees
15Binary Trees
- Properties of binary trees
- difference between a binary tree and
- a tree
- - may have empty node
- - the order of subtree are important
- - a binary tree is not a subset of a
- tree
- - maximum number of nodes in a BT
- 2k-1 where k depth of the tree
- - relationship between the number of
- leaf nodes(n0) and the number of
- nodes with degree 2(n2)
- n0 n2 1
16Binary Trees
- special types of binary trees
- - skewed binary tree
- - full binary tree (of depth k)
- def) a binary tree of depth k(³0)
- having 2k - 1 nodes
- - complete binary tree
- def) a binary tree with n nodes
- that correspond to the nodes
- numbered from 1 to n in the
- full binary tree of depth k
17Binary Trees
- full binary tree of depth 4 with
- sequential node numbers
18Binary Trees
- skewed and complete binary trees
skewed binary tree
complete binary tree
19Binary Trees
- binary tree representation
- array representation
- - sequential representations
- - determine the locations of the
- parent, left child, and right
- child of any node i in the binary
- tree
- 1) parent(i) is at i/2 if i ¹ 1
- if i 1, no parent
- 2) left_child(i) is at 2i if 2i?n
- 3) right_child(i) is at 2i 1
- if 2i 1 ? n
20Binary Trees
- array representation of binary trees
skew binary tree
complete binary tree
21Binary Trees
- Problems of array representation
- - inefficient storage utilization
- S(n) 2k-1 where
- k depth of binary tree
- ideal for complete binary trees
- - hard to insert/delete
22Binary Trees
- linked representation
- - each node has three fields
- left_child, data, and right_child
- typedef struct node tree_ptr
- typedef struct node
- int data
- tree_ptr left_child, right_child
-
- node representation for binary trees
23Binary Trees
- linked representation for the binary
- trees
skewed binary tree
complete binary tree
24Binary Trees
- leaf nodes link fields contain null
- pointer
- add a fourth field, parent, to know
- the parent of random nodes
leaf node
25Binary Trees
- tree representation
- - each node(in a tree) has variable
- sized nodes
- - hard to represent it by using
- array
- - use linked list
- need k link fields per node where
- k degree of tree
- two types of link
- non-null links and null links
-
26Binary Tree Traversal and Other Operations
27Binary Tree Traversals
- visit each node in the tree exactly
- once
- - produce a linear order for the
- information in a tree
- - what order?
- inorder LVR (Left Visit Right)
- preorder VLR (Visit Left Right)
- postorder LRV (Left Right Visit)
- - note)
- correspondence between these
- traversals and producing the
- infix, postfix, and prefix forms
- of expressions
28Binary Tree Traversals
- binary tree with arithmetic
- expression
A / B C D E (infix form)
29Binary Tree Traversals
- Inorder traversal
- void inorder(tree_ptr ptr)
- if(ptr)
- inorder(ptr-gtleft_child)
- printf(d,ptr-gtdata)
- inorder(ptr-gtright_child)
-
-
- inorder is invoked 19 times for the
- complete traversal 19 nodes
- output A/BCDE
- - corresponds to the infix form
30Binary Tree Traversals
31Binary Tree Traversals
- Preorder traversal
- void preorder(tree_ptr ptr)
- if(ptr)
- printf(d, ptr-gtdata)
- preorder(ptr-gtleft_child)
- preorder(ptr-gtright_child)
-
-
- output in the order /ABCDE
- - correspond to the prefix form
32Binary Tree Traversals
- Postorder traversal
- void postorder(tree_ptr ptr)
- if (ptr)
- postorder(ptr-gtleft_child)
- postorder(ptr-gtright_child)
- printf(d, ptr-gtdata)
-
-
- output in the order AB/CDE
- - correspond to the postfix form
33Binary Tree Traversals
- Iterative inorder traversal
- recursion
- - call itself directly or indirectly
- - simple, compact expression good
- readability
- - dont need to know implementation
- details
- - much storage multiple activations
- exist internally
- - slow execution speed
- - application factorial, fibonacci
- number, tree traversal, binary
- search, tower of Hanoi, quick
- sort, LISP structure
34Binary Tree Traversals
- void iter_inorder(tree_ptr node)
- int top -1
- tree_ptr stackMAX_STACK_SIZE
- while (1)
- while (node)
- push(top, node)
- node node-gtleft_child
-
- node pop(top)
- if (!node) break
- printf(d, node-gtdata)
- node node-gtright_child
-
-
- iterative inorder traversal
35Binary Tree Traversals
- every node of the tree is placed on
- and removed from the stack exactly
- once
- - time complexity O(n) where
- n the number of nodes in the tree
- - space complexity stack size
- O(n) where
- n worst case depth of the tree
- (case of skewed binary tree)
36Binary Tree Traversals
- Level order traversal
- traversal by using queue(FIFO)
- output in the order 12345678915
- for previous example ED/CAB
37Binary Tree Traversals
- void level_order(tree_ptr ptr)
- int front rear 0
- tree_ptr queueMAX_QUEUE_SIZE
- if (!ptr) return
- addq(front,rear,ptr)
- while (1)
- ptr deleteq(front, rear)
- if (ptr)
- printf(d, ptr-gtdata)
- if (ptr-gtleft_child)
- addq(front,rear,ptr-gtleft_child)
- if (ptr-gtright_child)
- addq(front,rear,ptr-gtright_child)
-
- else break
-
38Additional Binary Tree Operations
- copying binary trees
- - modified version of postorder
- tree_ptr copy(tree_ptr original)
- tree_ptr temp
- if (original)
- temp (tree_ptr)malloc(sizeof(node))
- if (IS_FULL(temp)) exit(1)
- temp-gtleft_child
- copy(original-gtleft_child)
- temp-gtright_child
- copy(original-gtright_child)
- temp-gtdata original-gtdata
- return temp
-
- return NULL
39Additional Binary Tree Operations
- testing for equality of binary trees
- - modified version of preorder
- int equal(tree_ptr first,tree_ptr second)
-
- return ((!first !second)
- (first second
- (first-gtdata second-gtdata)
- equal(first-gtleft_child,
- second-gtleft_child)
- equal(first-gtright_child,
- second-gtright_child))
40Threaded Binary Trees
41Threaded Binary Trees
- there are more null links than
- actual pointers in representation
- of any binary tree
- n1 null links out of 2n total links
- how to use the null links?
- - replace the null links by
- pointers, called threads, to other
- nodes in the tree
42Threaded Binary Trees
- construct the thread
- - assume ptr represents a node
- - rules
- 1) if ptr-gtleft_child is null, then
- replace the null link with a
- pointer to the inorder
- predecessor of ptr
- 2) if ptr-gtright_child is null, then
- replace the null link with a
- pointer to the inorder successor
- of ptr
43Threaded Binary Trees
44Threaded Binary Trees
- how to distinguish actual pointers
- and threads?
- - add two additional field to the
- node structure
- left_thread and right_thread
- - if ptr-gtleft_thread TRUE
- ptr-gtleft_child contains thread
- - if ptr-gtleft_thread FALSE
- ptr-gtleft_child contains a pointer
- to the left child
- - same for right_thread
45Threaded Binary Trees
- typedef struct threaded_tree threaded_ptr
- typedef struct threaded_tree
- short int left_thread
- threaded_ptr left_child
- char data
- threaded_ptr right_child
- short int right_thread
-
- threaded node structure
left_thread
right_thread
right_child
data
left_child
46Threaded Binary Trees
- two threads have been left dangling
- - the left child of H
- - the right child of G
- how to use two dangling links?
- - add a head(dummy) node
- an empty threaded tree
left_thread
right_thread
right_child
data
left_child
TRUE
FALSE
47Threaded Binary Trees
root
- memory representation of a threaded
- tree
f FALSE t TRUE
48Threaded Binary Trees
- inorder traversal of a threaded binary tree
- find the inorder successor of ptr
- - if ptr-gtright_threadTRUE,
- then ptr-gtright_child
- - otherwise follow a path of
- left-child links from the
- right-child of ptr until we reach
- a node with left_threadTRUE
- - find inorder successor without
- using a stack
49Threaded Binary Trees
- threaded_ptr insucc(threaded_ptr tree)
- threaded_ptr temp
- temp tree-gtright_child
- if (!tree-gtright_thread)
- while (!temp-gtleft_thread)
- temp temp-gtleft_child
- return temp
-
- finding the inorder successor of a
- node
50Threaded Binary Trees
- inorder traversal
- - repeated calls to insucc
- - time O(n) where
- n number of nodes in a threaded
- binary tree
- void tinorder(threaded_ptr tree)
- threaded_ptr temp tree
- for ()
- temp insucc(temp)
- if (temp tree) break
- printf(3c, temp-gtdata)
-
-
- inorder traversal of a threaded
- binary tree
51Threaded Binary Trees
- Inserting a node into a threaded binary tree
- insert child as the right child of
- parent
- 1)change parent-gtright_thread to
- FALSE
- 2)set child-gtleft_thread and
- child-gtright_thread to TRUE
- 3)set child-gtleft_child to point to
- parent
- 4)set child-gtright_child to point to
- parent-gtright_child
- 5)change parent-gtright_child to
- point to child
- 6)(if parent has nonempty right child)
- child becomes the inorder
- predecessor of the node that was
- previously parents inorder
- successor
52Threaded Binary Trees
- void insert_right(threaded_ptr parent,
threaded_ptr child) - threaded_ptr temp
- child-gtright_childparent-gtright_child
- child-gtright_threadparent-gtright_thread
- child-gtleft_childparent
- child-gtleft_threadTRUE
- parent-gtright_childchild
- parent-gtright_threadFALSE
- if(!child-gtright_thread)
- tempinsucc(child)
- temp-gtleft_childchild
-
-
- right insertion in a threaded binary
- tree
53Threaded Binary Trees
before
after
54Threaded Binary Trees
before
after
55Heaps
56Heaps
- def) max(min) tree a tree in which
- the key value in each node is no
- smaller(larger) than the key value
- in its children (if any)
- def) max(min) heap a complete
- binary tree that is also a
- max(min) tree
- - the root of a max(min) tree
- contains the largest(smallest) key
- in the tree
57Heaps
- Representation of max(min) heaps
- - array representation
- because heap is a complete binary
- tree
- - simple addressing scheme
- for parent, left(right) child
58Heaps
- Heap structure
- define MAX_ELEMENTS 200
- define HEAP_FULL(n) (n MaX_ELEMENTS - 1)
- define HEAP_EMPTY(n) (!n)
- typedef struct
- int key
- / other field /
- element
- element heapMAX_ELEMENTS
- int n 0
59Heaps
60Heaps
61priority queues
- Priority queue
- - deletion
- deletes the element with the
- highest(or the lowest) priority
- - insertion
- insert an element with arbitrary
- priority into a priority queue at
- any time
- (ex) job scheduling of OS
62priority queues
- We an use max(min) heap to implement
- the priority queues
-
- possible priority queue representations
63Heaps
- Insertion into a max heap
- - need to go from a node to its parent
- ? linked representation
- add a parent field to each node
- ? array representation
- a heap is a complete binary tree
- simple addressing scheme
64Heaps
(a) heap before insertion
(b) initial location of new node
(d) inset 21 into heap (a)
(c) insert 5 into heap (a)
65Heaps
- - select the initial location for
- the node to be inserted
- ? bottommost-rightmost leaf node
- - insert a new key value
- - adjust key value
- from leaf to root
- parent position i/2
- - time complexity O(depth of tree)
- ? O(log2n)
66Heaps
- void insert_max_heap(element item, int n)
- int i
- if (HEAP_FULL(n))
- fprintf(stderr,The heap is full. \n)
- exit(1)
-
- i (n)
- while ((i!1 ) (item.key gt heapi/2.key))
- heapi heapi/2
- i / 2
-
- heapi item
-
- insertion into a max heap
67Heaps
- deletion from a max heap
- - always delete an element from the root
- of the heap
- - restructure the tree so that it
- corresponds to a complete binary tree
- - place the last node to the root and
- from the root compare the parent node
- with its children and exchanging
- out-of-order elements until the heap
- is reestablished
68Heaps
(a) heap structure
(b) 10 inserted at the root
(c) final heap
69Heaps
- - select the removed node
- bottommost-rightmost leaf node
- - place the nodes element in the
- root node
- - adjust key value from root to leaf
- compare the parent node with its
- children and exchange out-of-order
- elements until the heap is
- reestablished
- - time complexity O(depth of tree)
- ? O(log2n)
70Heaps
- element delete_max_heap(int n)
- element item, temp
- if (HEAP_EMPTY(n))
- fprintf(stderr,The heap is empty\n)
- exit(1)
-
- item heap1
- temp heap(n)--
- parent 1 child 2
- while (child lt n)
- / compare left and right childs key
values / - if ((child lt n) (heapchild.key lt
- heapchild1.key))
- child
- if (temp.key gt heapchild.key) break
- / move to the next lower level /
- heapparent heapchild
- parent child
- child 2
71Binary Search Trees
72Binary Search Tree(BST)
- Binary search tree(BST) is a binary
- tree that is empty or each
- node satisfies the following
- properties
- 1)every element has a key, and no
- two elements have the same key
- 2)the keys in a nonempty left
- subtree must be smaller than the
- key in the root of the subtree
- 3)the keys in a nonempty right
- subtree must be larger than the
- key in the root of the subtree
- 4)the left and right subtrees are
- also BST
73Binary Search Tree(BST)
(a) not a BST
(b) BST
(c) BST
74Binary Search Tree(BST)
- searching, insertion, deletion is
- bounded by O(h) where h is the
- height of the BST
- can perform these operations both
- - by key value and
- eg) delete the element whith key x
- - by rank
- eg) delete the fifth smallest
- element
- inorder traversal of BST
- - generate a sorted list
75Binary Search Tree(BST)
- Searching a BST
- tree_ptr search(tree_ptr root, int key)
- /
- return a pointer to the node that contains
- key. If there is no such node, return NULL
- /
- if (!root) return NULL
- if (key root-gtdata) return root
- if (key lt root-gtdata)
- return search(root-gtleft_child, key)
- return search(root-gtright_child, key)
-
- recursive search of a BST
76Binary Search Tree(BST)
- tree_ptr iter_search(tree_ptr tree, int key)
- while (tree)
- if (key tree-gtdata) return tree
- if (key lt tree-gtdata)
- tree tree-gtleft_child
- else
- tree tree-gtright_child
-
- return NULL
-
- iterative search of a BST
77Binary Search Tree(BST)
- time complexity for searching
- - average case O(h) where
- h is the height of BST
- - worst case O(n)
- for skewed binary tree
78Binary Search Tree(BST)
- Inserting into a BST
- void insert_node(tree_ptr node, int num)
- tree_ptr ptr, temp modified_search(node,
num) - if (temp !(node))
- ptr (tree_ptr)malloc(sizeof(node))
- if (IS_FULL(ptr))
- fprintf(stderr,The momory is full\n)
- exit(1)
-
- ptr-gtdata num
- ptr-gtleft_child ptr-gtright_child NULL
- if (node)
- if (num lt temp-gtdata)
- temp-gtleft_child ptr
- else
- temp-gtright_child ptr
- else nodeptr
-
79Binary Search Tree(BST)
- modified_search is slightly modified
- version of function iter_search
- - return NULL, if the tree is empty
- or num is present
- - otherwise, return a pointer to the
- last node of the tree that was
- encountered during the search
- time complexity for inserting
- - O(h) where h is the height of the tree
80Binary Search Tree(BST)
(a) insert 80
(b) insert 35
81Binary Search Tree(BST)
- Deleting from a BST
- - deletion of a leaf node
- - deletion of a node with 1 child
- - deletion of a node with 2 children
- time complexity for deleting
- - O(h) where h is the height of the tree
82Binary Search Tree(BST)
- Deleting a leaf or a node with 1 child
35
40
(a) delete 35 (leaf)
(b) delete 40 (node with single child)
83Binary Search Tree(BST)
- deletion of a node with two children
(a) tree before deletion of 60
(b) tree after deletion of 60
84Binary Search Tree(BST)
- Height of a BST
- the height of a BST with n elements
- - average case O(log2n)
- - worst case O(n)
- eg) use insert_node to insert the
- keys 1, 2, 3, ..., n into an
- initially empty BST
85Binary Search Tree(BST)
- Balanced search trees
- - worst case height O(log2n)
- - searching, insertion, deletion is
- bounded by O(h) where
- h is the height of a binary tree
- - AVL tree, 2-3 tree, red-black tree
86Selection Trees
87Selection Trees
- merge k ordered sequence(run) into
- a single ordered sequence
- - run ordered sequence
- consists of some number of records
- in nondecreasing order of a key
- field
- selection tree
- - each node represent the smaller of
- its two children
- - the root node represents the
- smallest node in the tree
- - leaf node represents the first
- record in the corresponding run
- - compared to the playing of a
- tournament in which the winner is
- the record with the smaller key
88Selection Trees
- selection tree for k 8
- showing the first three keys
- in each of the eight runs
89Selection Trees
node that is changed
- selection tree after one record has
- been output and the tree
- restructured
90Selection Trees
- - time to restructure the tree is
- O(log2k)
- where k is the number of runs
- - time to merge all n records is
- O(nlog2k)
- - time to set up the selection tree
- the first time is
- O(k)
- ? total time O(nlog2k)
91Selection Trees
- tree of losers a tournament tree in
- which each non-leaf node retains a
- pointer to the loser
- - slightly faster merging is possible
overall winner
92Forests
93Forest
- Def) A forest is a set of n ³ 0
- disjoint trees
- removing the root of any tree
- - produce a forest
- forest with three trees
94Forest
- Transforming a forest into a binary tree
- 1)obtain the binary tree
- representation for each of the
- trees in the forest
- 2)link all the binary trees together
- through sibling field of the root
- node
95Forest
- Def) If T1,,Tn is a forest of trees,
- then the binary tree corresponding to
- this forest, denoted by B(T1,,Tn)
- 1)is empty, if n 0
- 2)has root equal to root (T1)
- has left subtree equal to
- B(T11,T12,,T1m), where T11,T12,,T1m
- are the subtrees of root(T1)
- and has right subtree B(T2, , Tn)
96Forest
- binary tree representation of forest
97Forest
- Forest traversals
- preorder traversal
- 1)if F is empty, then return
- 2)visit the root of the first tree of F
- 3)traverse the subtrees of the first
- tree in tree preorder
- 4)traverse the remaining trees of F in
- preorder
- ? equivalent to the preorder traversal
- of the corresponding binary tree
98Forest
- Forest traversals
- inorder traversal
- 1)if F is empty, then return
- 2)traverse the subtrees of the first
- tree in tree inorder
- 3)visit the root of the first tree of F
- 4)traverse the remaining trees of F in
- inorder
- ? equivalent to the inorder traversal
- of the corresponding binary tree
99Forest
- Forest traversals
- postorder traversal
- 1)if F is empty, then return
- 2)traverse the subtrees of the first
- tree in tree postorder
- 3)traverse the remaining trees of F in
- postorder
- 4)visit the root of the first tree of F
- ? not equivalent to the inorder
- traversal of the corresponding
- binary tree
100Set Representation
101Set Representation
- represent sets using trees
- assumptions
- - the elements of the sets are the
- numbers 0, 1, 2, , n-1
- - the sets being represented are
- pairwise disjoint
- eg) partition 10 elements numbered
- 0 through 9 into three
- disjoint sets
- S10,6,7,8, S21,4,9, and
- S32,3,5
- - link the nodes from the children
- to the parent
102Set Representation
- possible forest representation of
- sets
S1
S3
S2
103Set Representation
- disjoint set union
- - if Si and Sj are two disjoint sets,
- then their union
- Si È Sj
- all elements, x, such that x is
- in Si or Sj
- find(i)
- - find the set containing
- the element, i
104Set Representation
- union and find operations
- the set union operation
- - set the parent field of one of the
- roots to the other root
- - with each set name, it is necessary
- to keep a pointer to the root of the
- tree representing that set
- the set find operation
- - each root has a pointer to the set
- name
- - find which set an element is in
- 1)follow the parent links to the root
- of its tree
- 2)return the pointer to the set name
105Set Representation
- possible representation of S1 È S2
S1 È S2
S2 È S1
106Set Representation
- data representation of S1,S2,and S3
107Set Representation
- array representation of S1,S2,and S3
- int find1(int i)
- for( parenti gt 0 i parenti)
- return i
-
- void union1(int i, int j)
- parenti j / or parentj i /
-
- initial attempt at union-find
- functions
108Set Representation
- Previous array representation
- - easy to implement, but not good in
- performance characteristics
- Ex) Si i, 0 ilt p
- - initially, forest with p nodes
- parenti -1, 0 ilt p
- - sequence of union-find operations
- union(0,1),find(0)
- union(1,2),find(0)
-
- union(n-2,n-1),find(0)
- - produces degenerate tree
- - time complexity for the above
- sequence O(n) O(n2)
109Set Representation
110Set Representation
- For avoiding the creation of
- degenerate trees
- - weighting rule for union(i,j)
- Def) weighting rule for union(i,j)
- if the number of nodes in tree i
- is less than the number in tree j
- then make j the parent of i
- otherwise make i the parent of j.
- need to know how many node there are
- in every tree
- - maintain a count field in the root
- of every tree counti for root i
- ? parenti -counti
111Set Representation
- trees obtained using the weighting
- rule
initial
union(0,1), 0find(0)
union(0,2), 0find(0)
union(0,3), , 0find(0)
union(0,n-1)
112Set Representation
- void union2(int i, int j)
- / union the sets with roots i and j, i ! j,
- using the weighting rule.
- parenti -counti and
- parentj -countj /
- int temp
- temp parenti parentj
- if (parenti gt parentj)
- parenti j / make j the new root/
- parentj temp
-
- else
- parentj i / make i the new root/
- parenti temp
-
-
- union function
113Set Representation
- lemma
- let T be a tree with n nodes created
- as a result of union2
- ? no node in T has level greater
- than log2n 1
- ? time complexity for find in an n
- element tree O(log2n)
- ? time complexity for n-1 union and
- m find operations O(n mlog2n)
114Set Representation
- Example consider behavior of union2
- union(0,1) union(2,3) union(4,5)
- union(6,7) union(0,2) union(4,6)
- union(0,4)
level
command
1
initial
1
union(0,1) union(2,3)
2
union(4,5) union(6,7)
115Set Representation
level
command
1
union(0,2) union(4,6)
2
3
1
union(0,4)
2
3
4
116Set Representation
- Further improvement is possible
- if we add a collapsing rule to the
- find operation
- Def)collapsing rule If j is a
- node on the path from i to its
- root then make j a child of the
- root
117Set Representation
- int find2(int i)
- / find the root of the tree containing element
i. - use the collapsing rule to collapse all nodes
- from i to root /
- int root, trail, lead
- for (root i parentroot gt 0 root
parentroot) -
- for (traili trail ! root traillead)
- lead parenttrail
- parenttrail root
-
- return root
118Set Representation
- Lemma (Tarjan)
- Let T(m,n) be the maximum time
- required to process an intermixed
- sequence of m?n finds and n-1
- unions. Then
- k1ma(m,n) ? T(m,n) ? k2ma(m,n)
- for some positive constants k1 and k2.
119Set Representation
- Ackermanns function
- A(p,q)
- 2q if p 0
- 0 if p ? 1 and q 0
- 2 if p ? 1 and q 1
- A(p-1,A(p,q-1)) if p ? 1 and q ? 2
- a(m,n) min(z?1 A(z,4 m/n ) gt log2n
120CountingBinary Trees