Title: Chapter 5 Trees 23
1Chapter 5 Trees (2/3)
- 5.4 Additional Binary Tree Operations
- 5.6 Heaps
- 5.7 Binary Search Trees
2Copying Binary Trees
Copy
3Copying Binary Trees
treePointer copy(treePointer original)
treePointer temp if (original)
temp(treePointer) malloc(sizeof(node))
if (IS_FULL(temp)) fprintf(stderr,
the memory is full\n) exit(1)
temp-gtdataoriginal-gtdata
temp-gtleftChildcopy(original-gtleftChild)
temp-gtrightChildcopy(original-gtrightChild)
return temp return NULL
postorder
4Equality of Binary Trees
int equal(treePointer first, treePointer
second) / function returns FALSE if the binary
trees first and second are not equal, otherwise
it returns TRUE / return ((!first
!second) (first second
(first-gtdata second-gtdata)
equal(first-gtleftChild, second-gtleftChild)
equal(first-gtrightChild, second-gtrightChild)
) )
the same topology and data
5Heap (1/2)
- Priority Queue a queue that the element to be
deleted is the one with highest (or lowest)
priority. - Heaps are frequently used to implement Priority
Queue. - Operations on heaps
- creation of an empty heap
- insertion of a new element into the heap
- deletion of the largest (smallest) element from
the heap
6Data Structures for a Priority Queue
- unordered linked list
- unordered array
- sorted linked list
- sorted array
- heap
7Priority queue representations
8Heap (1/2)
- A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree. - A min tree is a tree in which the key value in
each node is no larger than the key values in its
children. A min heap is a complete binary tree
that is also a min tree.
9Max heaps (p.225)
Property The root of max heap contains the
largest element.
10Min heaps (p.225)
Property The root of min heap contains the
smallest element.
11Application Priority Queue
- machine service
- amount of time (min heap)
- amount of payment (max heap)
- factory
- time tag
12Insertion to a Max Heap (1/2)
- insert 5 into heap
- Initial location of new node
- Compare 5 with the parent node of the new node
(5gt2) - Move 2 to the new node
- Compare 5 with the parent node of the current
node (5lt20) - Save 5 to the current node
20
15
2
5
14
10
13Insertion to a Max Heap (2/2)
- insert 21 into heap
- Initial location of new node
- Compare 21 with the parent node of the new node
(21gt2) - Move 2 to the new node
- Compare 21 with the parent node of the current
node (21gt20) - Move 20 to the current node
- Save 21 to the root
20
21
15
2
14
10
14Max Heap declaration
define MAX_ELEMENTS 200 define HEAP_FULL(n)
(nMAX_ELEMENTS-1) define HEAP_EMPTY(n)
(!n) typedef struct int key element element
heapMAX_ELEMENTS int n0
15Insertion into a Max Heap (p. 227)
void push(element item, int n) int i if
(HEAP_FULL(n)) fprintf(stderr,
the heap is full.\n) exit(EXIT_FAILURE)
i (n) while ((i!1)(item.keygth
eapi/2.key)) heapi
heapi/2 i / 2 heapi
item
The size of the heap n 2k-1 gt
k?log2(n1)?
The time complexity of the insertion is O(log2n)
16Deletion from a Max Heap
- Delete the largest element from heap
- Pop the value of the root (20)
- Delete the most bottom-right node (10)
- Compare 10 with the larger children of the root
(15gt10) - Move 15 up to the root
- Compare 10 with the larger children of the
current node (14gt10) - Move 14 up to the current note
- Save 10 to the current node
20
15
2
14
10
10
17Deletion from a Max Heap (p.229)
element pop(int n) int parent, child
element item, temp if (HEAP_EMPTY(n))
fprintf(stderr, The heap is empty\n)
exit(EXIT_FAILURE) / save value
of the element with the highest key / item
heap1 / use last element in heap to
adjust heap / temp heap(n)-- parent
1 child 2
18 while (child lt n) / find the
larger child of the current parent /
if ((child lt n)
(heapchild.keyltheapchild1.key))
child if (temp.key gt heapchild.key)
break / move to the next
lower level / heapparent
heapchild child 2
heapparent temp return item
19Properties of Heap
- Insert an element
- O(log2n)
- Delete the min (max) element
- O(log2n)
- Delete an arbitrary element
- O(n)
- Search for an arbitrary element
- O(n)
20Binary Search Tree
- Binary search tree
- Every element has a unique key.
- The keys in a nonempty left subtree are smaller
than the key in the root of subtree. - The keys in a nonempty right subtreeare larger
than the key in the root of subtree. - The left and right subtrees are also binary
search trees.
21Examples of Binary Search Trees
22Recursive Search of a Binary Search Tree
element search(treePointer root, int k) /
return a pointer to the node that contains key.
If there is no such node, return NULL
/ if (!root) return NULL if (k
root-gtdata.key) return (root-gtdata) if (k lt
root-gtdata.key) return search(root-gtleftChi
ld,k) return search(root-gtrightChild,k)
23Iterative Search of a Binary Search Tree
element iterSearch(treePointer tree, int k)
while (tree) if (key
tree-gtdata.key) return (tree-gtdata) if
(key lt tree-gtdata.key) tree
tree-gtleftChild else tree
tree-gtrightChild return NULL
O(h)
24Inserting into Binary Search Tree
25Insertion into A Binary Search Tree
void insert(treePointer node, int k)
treePointer ptr, temp modifiedSearch(node,
num) if (temp !(node)) ptr
(treePointer) malloc(sizeof(node))
ptr-gtdata.key k ptr-gtleftChild
ptr-gtrightChild NULL if (node)
if (klttemp-gtdata.key) temp-gtleftChildptr
else temp-gtrightChild ptr else
node ptr
O(h)
26Deletion from a Binary Search Tree (1/2)
Degree of the node 1
1
2
X
T1
T2
27Deletion from a Binary Search Tree (2/2)
Degree of the node 2
40
60
20
70
30
50
10
55
45
52
Before deleting 60