Assignment for Binary Tree Analysis and Implementation, Assignments of Data Structures and Algorithms

A university assignment on binary trees, including questions on node counting, local minimum finding, and tree printing in java. The assignment includes instructions for each question, a hint for the first question, and an example of a binary tree for the tree printing question.

Typology: Assignments

Pre 2010

Uploaded on 03/10/2009

koofers-user-n7i
koofers-user-n7i 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP 3530
Assignment 8
Due: 20
th
Oct 2008
1. Consider a binary tree T, in which every non-leaf node has non-empty
left and right sub-trees. Assuming T has n leaf nodes, how many nodes in
total does it have? Justify your answer.
Hint: If we remove two leaf nodes that are under the same parent, every
non-leaf node of the new tree will still have non-empty left and right sub-
trees. How does this change the number of leaf (and non-leaf) nodes?
2. Consider an n-node complete binary tree T. Each node v of T is labeled
with a real number x
v
. You may assume that the real numbers labeling the
nodes are all distinct. A node v of T is a local minimum if the label x
v
is less
than the label x
w
for either child of v, or v's parent.
(a) Prove that any such tree has at least one local minimum.
(b) Write an efficient (fewest probes) algorithm for finding one local
minimum of T (maybe there are many, you only need to find one).
Calculate the time complexity for your algorithm.
3. Write a Java method
treeprint(node t, int n)
that will pretty-print a binary tree in indented format, using a preorder
traversal. The argument n is the current depth in the tree, whose value at
the beginning of the recursion is assumed to be zero. For example:
a (3)
... b (1)
... ... c(0)
... d (2)
... ... e (0)
... ... f (0)
... g (0)
In this tree, a has 3 children (b,d,g), b has one child (c), and d has 2 children
(e and f).
pf2

Partial preview of the text

Download Assignment for Binary Tree Analysis and Implementation and more Assignments Data Structures and Algorithms in PDF only on Docsity!

COP 3530

Assignment 8 Due: 20th^ Oct 2008

  1. Consider a binary tree T, in which every non-leaf node has non-empty left and right sub-trees. Assuming T has n leaf nodes, how many nodes in total does it have? Justify your answer.

Hint: If we remove two leaf nodes that are under the same parent, every non-leaf node of the new tree will still have non-empty left and right sub- trees. How does this change the number of leaf (and non-leaf) nodes?

  1. Consider an n-node complete binary tree T. Each node v of T is labeled with a real number xv. You may assume that the real numbers labeling the nodes are all distinct. A node v of T is a local minimum if the label xv is less than the label xw for either child of v, or v's parent.

(a) Prove that any such tree has at least one local minimum. (b) Write an efficient (fewest probes) algorithm for finding one local minimum of T (maybe there are many, you only need to find one). Calculate the time complexity for your algorithm.

  1. Write a Java method

treeprint(node t, int n)

that will pretty-print a binary tree in indented format, using a preorder traversal. The argument n is the current depth in the tree, whose value at the beginning of the recursion is assumed to be zero. For example:

a (3) ... b (1) ... ... c(0) ... d (2) ... ... e (0) ... ... f (0) ... g (0)

In this tree, a has 3 children (b,d,g), b has one child (c), and d has 2 children (e and f).

  1. Write a Java program that will read in two traversals of a binary tree, and attempt to construct the corresponding binary tree. Assume all the keys are distinct integers. The program will first read the inorder traversal, and then read in the preorder traversal. Your program must detect the situation in which building the tree is impossible. To show the results, you must print the tree. The output format should be similar to that for problem 3.