traversals

Ace your homework & exams now with Quizwiz!

How to delete from binary tree

-Find the node to be deleted. -Remove it and replace it with its successor/predecessor and update the BST.

BFS Algorithm Steps

-Take the data for the graph's adjacency matrix or adjacency list. -Create a queue and fill it with items. Activate the root node (meaning that get the root node at the beginning of the queue). -Dequeue the queue's head (or initial element), then enqueue all of the queue's nearby nodes from left to right. Simply dequeue the head and resume the operation if a node has no nearby nodes that need to be investigated. (Note: If a neighbor emerges that has previously been investigated or is in the queue, don't enqueue it; instead, skip it.) -Continue in this manner until the queue is empty.

BST Properties

-The left subtree of a node contains only nodes with keys lesser than the node's key. -The right subtree of a node contains only nodes with keys greater than the node's key. -The left and right subtree each must also be a binary search tree.

How to insert data into binary tree search

1. Create a new BST node and assign values to it. 2. insert(node, key) i) If root == NULL, return the new node to the calling function. ii) if root=>data < key call the insert function with root=>right and assign the return value in root=>right. root->right = insert(root=>right,key) iii) if root=>data > key call the insert function with root->left and assign the return value in root=>left. root=>left = insert(root=>left,key) 3. Finally, return the original root pointer to the calling function.

What is the maximum number of children that a binary tree node can have? a) 0 b) 1 c) 2 d) 3

2 Explanation: In a binary tree, a node can have atmost 2 nodes (i.e.) 0,1 or 2 left and right child.

If complete binary trees are represented in arrays, Using what formula can a parent node be located in an array? a) (i+1)/2 b) (i-1)/2 c) i/2 d) 2i/2

Answer: (i-1)/2 Explanation: If a complete binary tree is represented in an array, parent nodes are found at indices (i-1)/2.

If complete binary trees are represented in arrays, what formula can be used to locate a left child, if the node has an index i? a) 2i+1 b) 2i+2 c) 2i d) 4i

Answer: 2i+1 Explanation: If complete binary trees are represented in arrays, left children are located at indices 2i+1 and right children at 2i+2.

Which of the following is an infix expression? a) (a+b)*(c+d) b) ab+c* c) +ab d) abc+*

Answer: a Explanation: (a+b)*(c+d) is an infix expression. +ab is a prefix expression and ab+c* is a postfix expression.

An expression tree is created using? a) postfix expression b) prefix expression c) infix expression d) paranthesized expression

Answer: a Explanation: A postfix expression is converted into an expression tree by reading one symbol at a time and constructing a tree respectively.

It is easier for a computer to process a postfix expression than an infix expression. a) True b) False

Answer: a Explanation: Computers can easily process a postfix expression because a postfix expression keeps track of precedence of operators

Which of the following properties are obeyed by all three DFS tree - traversals? a) Left subtrees are visited before right subtrees b) Right subtrees are visited before left subtrees c) Root node is visited before left subtree d) Root node is visited before right subtree

Answer: a Explanation: In preorder, inorder and postorder traversal the left subtrees are visited before the right subtrees. In Inorder traversal, the Left subtree is visited first then the Root node then the Right subtree. In postorder traversal, the Left subtree is visited first, then Right subtree and then the Root node is visited.

A node can have a single child. a) true b) false

Answer: a Explanation: It is possible for a node to have at least one child, as is the case with the unary minus operator.

When an operand is read in the infix to postfix algorithm, which of the following is done? a) It is placed on to the output b) It is placed in operator stack c) It is ignored d) Operator stack is emptied

Answer: a Explanation: While converting an infix expression to a postfix expression, when an operand is read, it is placed on to the output. When an operator is read, it is placed in the operator stack.

Only infix expression can be made into an expression tree. a) true b) false

Answer: b Explanation: All infix, prefix and postfix expressions can be made into an expression tree using appropriate algorithms.

What is the specialty about the in order traversal of a binary search tree? a) It traverses in a non increasing order b) It traverses in an increasing order c) It traverses in a random fashion d) It traverses based on priority of the node

Answer: b Explanation: As a binary search tree consists of elements lesser than the node to the left and the ones greater than the node to the right, an inorder traversal will give the elements in an increasing order.

In infix to postfix conversion algorithm, the operators are associated from? a) right to left b) left to right c) center to left d) center to right

Answer: b Explanation: In infix, prefix and postfix expressions, the operators are associated from left to right and not right to left.

What is the postfix expression for the infix expression? a-b-c ⦁ -ab-c ⦁ ab - c - ⦁ --abc ⦁ -ab-c

Answer: b Explanation: The corresponding postfix expression for the given infix expression is found to be ab-c- and not abc-

The leaves of an expression tree always contain? a) operators b) operands c) null d) expression

Answer: b Explanation: The leaves of an expression tree always contain the result of a given expression (i.e.) operands.

What does the other nodes of an expression tree(except leaves) contain? a) only operands b) only operators c) both operands and operators d) expression

Answer: b Explanation: The nodes other than leaves always contain only operators. There cannot be any operand in those nodes.

What is the time complexity of an infix to postfix conversion algorithm? a) O(N log N) b) O(N) c) O(N2) d) O(M log N)

Answer: b Explanation: The time complexity of an infix to postfix expression conversion algorithm is mathematically found to be O(N).

Parentheses are simply ignored in the conversion of infix to postfix expression. a) True b) False

Answer: b Explanation: When a parenthesis is encountered, it is placed on the operator stack. When the corresponding parenthesis is encountered, the stack is popped until the other parenthesis is reached and they are discarded.

What does the following piece of code do? public void func(Tree root) { func(root.left()); func(root.right()); System.out.println(root.data()); } a) Preorder traversal b) Inorder traversal c) Postorder traversal d) Level order traversal

Answer: c Explanation: In a postorder traversal, first the left child is visited, then the right child and finally the parent.

An expression tree is a kind of? a) Binary search tree b) Fibonacci tree c) Binary tree d) Heap

Answer: c Explanation: The expression tree is a binary tree. It contains operands at leaf nodes and remaining nodes are filled with operators. The operands and the operators can be arranged in any order (ascending, descending).

When an operand is read in the infix to postfix algorithm, what should be done when a left parenthesis '(' is encountered? a) It is ignored b) It is placed in the output c) It is placed in the operator stack d) The contents of the operator stack is emptied

Answer: c Explanation: When a left parenthesis is encountered, it is placed on to the operator stack. When the corresponding right parenthesis is encountered, the stack is popped until the left parenthesis and remove both the parenthesis.

Which of the following is false about a binary search tree? a) The left child is always lesser than its parent b) The right child is always greater than its parent c) The left and right sub-trees should also be binary search trees d) In order sequence gives decreasing order of elements

Answer: d Explanation: In order sequence of binary search trees will always give ascending order of elements. Remaining all are true regarding binary search trees.

The average depth of a binary tree is given as? a) O(N) b) O(log N) c) O(M log N) d) O(√N)

Answer: d Explanation: The average depth of a binary expression tree is mathematically found to be O(√N).

(t/f) Iterator allows us to add an element to collection while traversing it.

Flase

What is the difference between Iterator and Iterable?

Iterator: ⦁ Is its own Class ⦁ Used to iterate over the Iterable object ⦁ Implemented as a separate file ⦁ Need one per Iterable class (generally) Iterable: ⦁ Is the Collection (Stack, Queue, List) ⦁ Can provide an Iterator on request

(t/f) An Iterator traverses the elements in one direction only.

True

(t/f) An existing element's value cannot be replaced by using Iterator.

True

(t/f) Iterator cannot provide us index of an element in the Data Structure.

True

Postorder

left, right, root

Inorder

left, root, right

How to search for a key in a binary search tree?

public Tree search(Tree root, int key) { if( root == null || root.key == key ) { return root; } if( root.key < key ) { return search(root.right,key); } else return search(root.left,key); }

Preorder

root, left, right

What 3 methods are in the Iterable interface?

⦁ hasNext() ⦁ next() ⦁ remove() (optional)


Related study sets

Chapter 3 fill in the blank notes

View Set

Determining Premise & Conclusion

View Set

Systems-Based Emergency Medicine Course Objectives: Psychiatric/Behavioral Health Emergencies PA 605 ER

View Set

InQuizitive: Chapter 10. Poverty SYG2000

View Set

CH 4. Life insurance policies- types of policies

View Set

Biostatistics: Correlations and Regression Analysis as Related to GI Cases + Q(Jung)

View Set

Temporal Fossa, Infratemporal Fossa & TMJ

View Set

Chapter 08: Security Management Models

View Set