16 Lecture

CS301

Midterm & Final Term Short Notes

Deleting a node in BST

Deleting a node in a binary search tree (BST) involves rearranging the tree so that the deleted node's children are connected to its parent node. If the deleted node has no children, we simply remove it from the tree. If it has one child, we rep


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. In a BST, which node is deleted when the node to be deleted has no children? a) The root node b) The node to be deleted c) The parent of the node to be deleted d) None of the above

Answer: b) The node to be deleted

  1. When deleting a node with one child in a BST, which child of the deleted node replaces it? a) The left child b) The right child c) It depends on the node's value d) None of the above

Answer: c) It depends on the node's value

  1. When deleting a node with two children in a BST, which node is used to replace the deleted node? a) The left child of the deleted node b) The right child of the deleted node c) The smallest node in the right subtree of the deleted node d) The largest node in the left subtree of the deleted node

Answer: c) The smallest node in the right subtree of the deleted node

  1. Which traversal algorithm is commonly used to delete a node in a BST? a) Inorder traversal b) Preorder traversal c) Postorder traversal d) Level-order traversal

Answer: a) Inorder traversal

  1. In a BST, what is the time complexity of deleting a node with one child? a) O(1) b) O(log n) c) O(n) d) It depends on the height of the tree

Answer: b) O(log n)

  1. What is the time complexity of deleting a node with two children in a BST? a) O(1) b) O(log n) c) O(n) d) It depends on the height of the tree

Answer: d) It depends on the height of the tree

  1. What happens when a leaf node is deleted in a BST? a) The node is deleted and the tree is balanced b) The node is deleted and the tree is left unbalanced c) The tree becomes a binary tree d) None of the above

Answer: a) The node is deleted and the tree is balanced

  1. In a self-balancing BST, what type of rotation is performed when deleting a node with one child? a) Left rotation b) Right rotation c) Double rotation d) No rotation is performed

Answer: d) No rotation is performed

  1. When deleting a node in a BST, what is the worst-case time complexity if the tree is unbalanced? a) O(1) b) O(log n) c) O(n) d) It depends on the size of the tree

Answer: c) O(n)

  1. In a BST, what is the minimum number of children a node can have? a) 0 b) 1 c) 2 d) There is no minimum number of children

Answer: a) 0



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is your favorite book and why? Answer: My favorite book is "To Kill a Mockingbird" by Harper Lee. I love the way it explores themes of racism, justice, and morality through the eyes of a child. The characters are well-developed and the story is both heartwarming and heart-wrenching.

  2. What do you think is the most important social issue facing us today? Answer: In my opinion, the most important social issue facing us today is climate change. It is a global problem that affects everyone and everything on our planet. We need to take action to reduce greenhouse gas emissions and transition to a more sustainable way of living.

  3. What is your favorite way to relax? Answer: My favorite way to relax is to read a good book or listen to music. I find that both activities allow me to escape from the stresses of everyday life and recharge my batteries.

  4. What do you think is the biggest challenge facing young people today? Answer: I believe that the biggest challenge facing young people today is finding meaningful work in a rapidly changing economy. With automation and globalization, many traditional jobs are disappearing and it can be difficult to find a career path that offers stability and fulfillment.

  5. What is your favorite place to travel to and why? Answer: My favorite place to travel to is Japan. I love the mix of ancient traditions and modern technology, the delicious food, and the beautiful landscapes. Plus, the people are incredibly friendly and welcoming.

  6. What is your favorite hobby and why? Answer: My favorite hobby is playing music. I love the creative outlet it provides, the way it connects me with others, and the sense of accomplishment I feel when I master a new song.

  7. What is your favorite quote and why? Answer: My favorite quote is "Be the change you wish to see in the world" by Mahatma Gandhi. I love the message of personal responsibility and the idea that each of us has the power to make a difference in the world.

  8. What is the most important lesson you have learned in life so far? Answer: The most important lesson I have learned in life so far is that relationships are the most valuable thing we have. Whether it's family, friends, or coworkers, the people in our lives are what make life worth living.

  9. What is your biggest fear and how do you deal with it? Answer: My biggest fear is failure. To deal with it, I try to focus on the process rather than the outcome. I remind myself that it's okay to make mistakes and that every failure is an opportunity to learn and grow.

  10. What is your favorite memory and why? Answer: My favorite memory is the day I got married. It was a beautiful day filled with love, laughter, and joy. I felt surrounded by the people I care about most and I knew that I was embarking on a new chapter of my life with my best friend by my side.

Binary Search Tree (BST) is a data structure that is widely used for searching and sorting algorithms. It is a binary tree where the left subtree of a node contains only nodes with values smaller than the node's value, and the right subtree contains only nodes with values greater than the node's value. Deleting a node in a BST requires finding the node to be deleted and then rearranging the tree to maintain the binary search tree property. There are three cases when deleting a node in a BST:
  1. Node has no children: In this case, the node is simply deleted.
  2. Node has one child: In this case, the child node replaces the deleted node.
  3. Node has two children: In this case, the smallest node in the right subtree of the node to be deleted is used to replace the deleted node.
To delete a node in a BST, we first search for the node to be deleted using the BST property. Once we find the node, we check for the above cases and perform the necessary steps to maintain the binary search tree property. Deleting a node in a BST can cause the tree to become unbalanced, which can affect the performance of searching and sorting algorithms. To maintain the balance of the tree, we can use self-balancing BSTs like AVL trees or Red-Black trees. In conclusion, deleting a node in a BST requires rearranging the tree while maintaining the binary search tree property. It is an essential operation that is used in many applications, and understanding its working is crucial for efficient use of BSTs.