The following algorithms (A1 and A2) solve the same problem. To do so, they receive as input argument: • root: the root
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
The following algorithms (A1 and A2) solve the same problem. To do so, they receive as input argument: • root: the root
The following algorithms (A1 and A2) solve the same problem. To do so, they receive as input argument: • root: the root of a binary search tree storing integer numbers • x: an integer number ALGORITHM 1 ALGORITHM 2 function A1(root, x) Q = new Queue() ENQUEUE (Q, root) while !ISEMPTY(Q) do t = PEEK(Q) if (t==x) return 1 else ENQUEUE (Q, left(t)) ENQUEUE (Q, right(t)) DEQUEUE (Q) end while return 0 end function function A2(root,x) if (root==NULL) return 0 else if(x == root->data) return 1 else if (x< root->data) return A2 (root->left,x) else return A2 (root->right,x) end function Note: the function ENQUEUE only inserts a new element in the queue if this element is different from NULL