Worksheet for Trees in Algorithms and Data Structures course, Study notes of Algorithms and Programming

A worksheet for Trees in the Algorithms and Data Structures course. It includes exercises related to binary trees, minmax trees, strictly binary trees, ADTs, red-black trees, and B-trees. examples and asks questions to test the student's understanding of the topic.

Typology: Study notes

2021/2022

Uploaded on 05/11/2023

laalamani
laalamani 🇺🇸

3.7

(3)

218 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS200AlgorithmsandDataStructures
Worksheet for Trees
1. Given the binary tree operations, what tree or trees does the following sequence of
statements produce?
Public void Ex11(){
BinaryTree<Integer> t1 = new BinaryTree<Integer>(2);
t1.attachLeft(5)
BinaryTree<Integer> t2 = new BinaryTree<Integer>(8);
t2.attachLeft(6);
t2.attachRight(7);
t1.attachRightSubtree(t2);
BinaryTree<Integer> t3 = new BinaryTree<Integer>(7);
t3.atachLeft(3);
t3.attachRight(1);
BinaryTree<Integer> t4 = new BinaryTree<Integer> (1, t1, t3);
}
2. Consider a nonempty binary tree with two types of nodes: min nodes and max nodes.
Each node has an integer value initially associated with it. You can define the value of
such a minimax tree as follow;
If the root is a min node, the value of the tree is equal to the
minimum of
a. The integer stored in the root
b. The value of the left subtree, but only if it is
nonempty
c. The value of the right subtree, but only if it is
nonempty
If the root is a max node, the value of the tree is equal to the
maximum of the above three values.
An example of the minmax tree (square node: max node, circle node: min node)
pf3
pf4
pf5

Partial preview of the text

Download Worksheet for Trees in Algorithms and Data Structures course and more Study notes Algorithms and Programming in PDF only on Docsity!

Worksheet for Trees

  1. Given the binary tree operations, what tree or trees does the following sequence of statements produce?

Public void Ex11(){ BinaryTree t1 = new BinaryTree(2); t1.attachLeft(5)

BinaryTree t2 = new BinaryTree(8); t2.attachLeft(6); t2.attachRight(7);

t1.attachRightSubtree(t2);

BinaryTree t3 = new BinaryTree(7); t3.atachLeft(3); t3.attachRight(1);

BinaryTree t4 = new BinaryTree (1, t1, t3); }

  1. Consider a nonempty binary tree with two types of nodes: min nodes and max nodes. Each node has an integer value initially associated with it. You can define the value of such a minimax tree as follow;

If the root is a min node, the value of the tree is equal to the minimum of a. The integer stored in the root b. The value of the left subtree, but only if it is nonempty c. The value of the right subtree, but only if it is nonempty

If the root is a max node, the value of the tree is equal to the maximum of the above three values.

An example of the minmax tree (square node: max node, circle node: min node)

Compute the value of the minmax tree in below tree. Each node is labeled with its initial value. (square: max node, circle: min node)

  1. A binary tree is strictly binary if every non-leaf node has exactly two children. Prove by induction on the number of leaves that a strictly binary tree with n leaves has exactly 2n – 1 nodes.
  2. Execute the following sequence of operations on an initially empty ADT(abstract data type) implemented as

A binary search tree A 2-3 tree A 2-3-4 tree A red-black tree An AVL tree

And show the underlying tree after each operation.

t.treeInsert(17) t.treeInsert(78) t.treeInsert(20) t.treeInsert(57) t.treeInsert(51) t.treeDelete(17) t.treeInsert(60) t.treeInsert(70)

  1. Given the following 2-3 tree, draw the tree that results after removing r,e,g, and b from the tree.
  2. Draw the 2-3-4 tree that results from inserting p,c,m,j,v,f, and b, in the order given, into a 2-3-4 tree that contains a single node whose value is n.
  3. Draw the 2-3-4 tree that results from inserting 39, 38, 37, 36, 35, 34, 33, and 32 into it.
  1. Change the following 2-3-4 tree to red-black tree.
  2. Change the following red-black tree to a 2-3-4 tree.
  3. Why does a node in a red-black tree require less memory than a node in a 2-3- tree?
  4. Given a B-tree of degree 5 and a height of 3; a. What is the maximum number of nodes (including the foot)? b. What is the maximum number of records that can be stored?
  5. The B-tree traversal algorithm presented in this class assumes that internal memory is large enough to accommodate the recursive stack that contains up to h blocks, where h is the height of the B-tree. If you are in an environment where this assumption is not true, modify the traversal algorithm so that the recursive stack contains block numbers rather than the actual blocks. How many block accesses does your algorithm have to perform?