CS301- Asignment Solution
Q. No.1a) Consider a binary search tree (BST) that is initially empty. Draw the tree that will result if the following numbers are inserted in the order given: 7, 3, 8, 1, 6, 2, 9, 5.b) What basic operations we can perform on the binary tree of part (a)Q.No.2 Answer the Following Questions:(a)Suppose we want a function to change the state of an object. But we don’t want to send a copy to the function. The object could be large and copying it costly in terms of time. Which of the following is the best solution to do this and why?1) We will use pointers 2) We will use pass by reference (b) what is dangling reference? Explain with help of an example.
Some Hints first question7, 3, 8, 1, 6, 2, 9, 5the root node is 7in the next step 3 will be compare to root node it is samller then 7 so it will be left side of the root node and then 8 comes it will it is greatrer then root node it will be pl;acec right side then 1 compare with root node is smaller it will be moved left whereed thereis node 3 it will also compare with that it is also smaller then then 3 so it will placed left side of 3 and so on 2nd questionwe will use pointers as they provide high performance and/or compact memory footprint.3rdDangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data.exmaple#include {char *dp = malloc ( A_CONST );/* ... */free ( dp ); dp = NULL; /* ... */}'dp' first becomes a dangling pointerthen again dp is no longer dangling
No comments:
Post a Comment