Why The Below Pseudo Code Where X Is A Value Wt Is Weight #644
Why the below pseudo code where x is a value, wt is weight factor and t is root node can’t insert?</p> <pre><code class="language-java"> WeightBalanceTreeNode insert(int x, int wt, WeightBalanceTreeNode k) : if (k == null) k = new WeightBalanceTreeNode(x, wt, null, null) else if (x < t.element) : k.left = insert (x, wt, k.left) if (k.left.weight < k.weight) k = rotateWithRightChild (k) else if (x > t.element) : k.right = insert (x, wt, k.right) if (k.right.weight < k.weight) k = rotateWithLeftChild (k) </code></pre>
This multiple choice question (MCQ) is related to the book/course gs gs121 Data Structures and Algorithms. It can also be found in gs gs121 Binary Trees - Weight Balanced Tree - Quiz No.1.
Why the below pseudo code where x is a value, wt is weight factor and t is root node can’t insert?
WeightBalanceTreeNode insert(int x, int wt, WeightBalanceTreeNode k) : if (k == null) k = new WeightBalanceTreeNode(x, wt, null, null) else if (x < t.element) : k.left = insert (x, wt, k.left) if (k.left.weight < k.weight) k = rotateWithRightChild (k) else if (x > t.element) : k.right = insert (x, wt, k.right) if (k.right.weight < k.weight) k = rotateWithLeftChild (k)
when x>t. element Rotate-with-left-child should take place and vice versa
the logic is incorrect
the condition for rotating children is wrong
insertion cannot be performed in weight balanced trees
Similar question(s) are as followings:
Online Quizzes of gs121 Data Structures and Algorithms
Binary Trees - Binary Search Tree - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Binary Search Tree - Quiz No.2
gs gs121 Data Structures and Algorithms
Online Quizzes
Binary Trees - Preorder Traversal - Quiz No.1
gs gs121 Data Structures and Algorithms
Online Quizzes