Trees - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Trees

Description:

Tree Traversal. . : LVR, LRV, VLR, VRL, RVL, RLV. L : , V : , R ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 22
Provided by: skk1
Learn more at: http://comnet.skku.edu
Category:

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • Ikjun Yeom

2
Tree Traversal
  • ??? ?? ?? ??? ? ??? ??
  • ?? ?? LVR, LRV, VLR, VRL, RVL, RLV
  • L ?? ??, V ????, R ??? ??
  • ??? ????? ?? ??(LR)
  • LVR ??(inorder) ??
  • VLR ??(preorder) ??
  • LRV ??(postorder) ??
  • ???? ???? ??

3
?? ?? (1)
  • LVR A/BCDE

void inorder(treePointer ptr) / ?? ?? ??
/ if (ptr) inorder(ptr-gtleftChild) prin
tf(d, ptr-gtdata) inorder(ptr-gtrightChild)

4
?? ?? (2)
?? A/BCDE
5
?? ??
  • VLR / A B C D E

void preorder(treePointer ptr) / ?? ?? ??
/ if (ptr) printf(d,
ptr-gtdata) preorder(ptr-gtleftChild) preorder
(ptr-gtrightChild)
6
?? ??(Postorder traversal)
  • LRV A B / C D E

void postorder(treePointer ptr) / ?? ?? ??
/ if (ptr) postorder(ptr-gtleftChild) po
storder(ptr-gtrightChild) printf(d,
ptr-gtdata)
7
??? ?? ??
  • ?? ??? ?? ??? ????

void iterInorder(treePointer node) int top
-1 / ?? ??? / treePointer stackMAX_STACK_SIZE
for () for ( node node
node-gtleftChild) add(top, node) /??? ??
/ node delete(top) /???? ?? / if
(!node) break / ?? ?? / printf(d,
node-gtdata) node node-gtrightChild
8
???? ?
  • ????? ?? ??(??) ??? ?? ??
  • ??? ????? ?? ?? ?? ??
  • ?? ??? ?? MaxPriorityQueue

ADT MaxPriorityQueue is objects a
collection of ngt0 elements, each element has a
key functions for all q ?
MaxPriorityQueue, item ? Element, n ?
integer MaxPriorityQueue create(max_size)
create an empty priority queue. Boolean
isEmpty(q, n) if(ngt0) return TRUE
else return FALSE Element top(q,n)
if(!isEmpty(q,n)) return an instance
of the largest element in q else
return error. Element pop(q,n)
if(!isEmpty(q,n)) return an instance
of the largest element in q and remove
it from the heap else return error. MaxPriorityQu
eue push(q, item, n) insert item into pq and
return the resulting priority queue.
9
???? ?
  • ?? ??
  • ??? ?? ???
  • IsEmpty() O(1)
  • Push() O(1)
  • Top() T(n)
  • Pop() T(n)
  • ?? ??
  • IsEmpty() O(1)
  • Top() O(1)
  • Push() O(log n)
  • Pop() O(log n)

10
?? ??? ?? (1)
  • ??(??)?? ? ??? ? ?? ? ??? ? ??? ??(??) ?? ??.
  • ???? ?? ????? ?? ?? ??.
  • ???? ?? ????? ?? ?? ??.

?? ??
?? ??
11
?? ????? ??(1)
  • ?? ??? ?? ?? ??
  • ?? ??? ??? ?? ??? ????? ?? ??? ?? ?? ??? ??? ??
    ???

12
?? ????? ??
void push(element item, int n) / insert item
into a max heap of current size n / int i if
(HEAP_FULL(n)) fprintf(stderr, The heap is
full. \n) exit(EXIT_FAILURE) i
(n) while ((i ! 1) (item.key gt
heapi/2.key)) heapi heapi/2 i /
2 heapi item
  • ??? O(log2 n)

13
?? ????? ??(1)
  • ?? ??
  • ??? ??? ??? ?? ?? ? ?? ? -1
  • ??? ?? ??, ??? ?? ??
  • ?? ? ?? ???.

14
?? ????? ??(2)
element pop(int n) int parent, child element
item, temp if(HEAP_EMPTY(n))
fprintf(stderr, The heap is
empty\n) exit(EXIT_FAILURE) item
hea1 temp heap(n)-- parent 1 child
2 while(childlt n) if (childlt n)
(heapchild.key lt heapchild1.key) child
if(temp.key gt heapchild.key)
break heapparent heapchild parent
child child 2 heapparent
temp return item
  • ??? O(log n)

15
?? ?? ??
  • ??
  • ?????? ??????, ?? ??? ????
  • (1) ?? ??? ?? ??? ?? ???.
  • (2) ?? ????? ??? ? ??? ??? ??.
  • (3) ??? ????? ??? ? ??? ??? ??
  • (4) ??? ??? ????? ?? ?? ????.
  • ?? ?? ??

16
?? ?? ??? ??
  • k ??? ? ??? ??
  • k lt ??? ? ?? ???? ??
  • k gt ??? ? ??? ???? ??

element search(treePointer tree, int key) /
??? key? ??? ?? ???? ???. ?? ??? ?? ???? 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)
17
?? ?? ??? ??? ??
element iterSearch(treePointer tree, int
key) / ??? key? ??? ?? ???? ???. ?? ??? ?? ???
NULL? ?? / while (tree) if (key
tree-gtdata) return tree if (key lt
tree-gtdata) tree tree-gtleft_child else tree
tree-gtright_child return NULL
18
?? ?? ????? ?? (1)
  • x? key?? ?? ??? ??
  • ??? ???? ? ?? ??? ??? ??
  • ??? ???? ??? ?? ??? ?? ??

30
30
30
80 ??
35 ??
5
40
5
40
5
40
2
80
35
2
80
2
19
?? ?? ????? ?? (2)
void insert (treePointer node, int k, iType
theItem) / ???? ??? k? ???? ??? ?? ?? ?? ??
??? ?? ??? data(k, theItem)? ? ??? ??
/ treePointer ptr, temp modifiedSearch(node,
k) if(temp !(node)) / k is not in the
tree / MALLOC(ptr, sizeof(ptr)) ptr-gtdata.k
ey k ptr-gtdata.item theItem ptr-gtleftChi
ld ptr-gtrightChild NULL if(node) /
insert as child of temp / if(klttemp-gtdata.key)
temp-gtleftChild ptr else temp-gtrightChild
ptr else node ptr
20
?? ?? ????? ??
  • ?? ??? ??
  • ??? ?? ??? 0? ??. ??? ?? ??
  • ??? ??? ?? ??? ??? ??
  • ??? ??? ??? ??? ??? ??? ??
  • ??? ??? ?? ??? ??? ??
  • ?? ?????? ?? ? ??? ??? ?????? ?? ?? ??? ??
  • ??? ?????? ??? ??? ?? ?? ??
  • ?? ??? O(h)

5
30
30? ??
2
40
5
40
80
2
80
21
?? ?? ??(n)? ??
  • ?? ?? ??? ?? ? n
  • ??? ??
  • ?? ?? ??? ?? n
  • ? 1,2,3,...,n? ???? ??
  • ??
  • ?? ?? ??? ?? O(log n )
  • ?? ?? ??(balanced search tree)
  • ??? ???? height O(log n )? ?? ??
  • ??, ??, ??? ?? ??? O(h)
  • AVL, 2-3, 2-3-4, ??-??(red-black), B ??, B ?? ?
Write a Comment
User Comments (0)
About PowerShow.com