SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Trees
Exercise:
Exercises
Question:45 | ISBN:9781118771334 | Edition: 6

Question

Design algorithms for the following operations for a binary tree T:
• preorderNext(p): Return the position visited after p in a preorder traversal of T (or null if p is the last node visited).
• inorderNext(p): Return the position visited after p in an inorder traversal of T (or null if p is the last node visited).
• postorderNext(p): Return the position visited after p in a postorder traversal of T (or null if p is the last node visited).
What are the worst-case running times of your algorithms?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

To design algorithms for the operations preorderNext(p), inorderNext(p), and postorderNext(p) for a binary tree T, we can use the following approaches:

  1. Preorder Traversal:

    • In a preorder traversal, the order of visiting nodes is root, left, right.
    • To find the position visited after p in a preorder traversal:
      • If p has a left child, return the position of the left child.
      • If p has a right child, return the position of the right child.
      • Otherwise, go up the tree until finding a node that has a right child different from p's parent. Return the position of that right child.
      • If no such node is found, return null.
  2. Inorder Traversal:

    • In an inorder traversal, the order of visiting nodes is left, root, right.
    • To find the position visited after p in an inorder traversal:
      • If p has a right child, return the position of the leftmost node in the right subtree (i.e., the leftmost descendant of the right child).
      • Otherwise, go up the tree until finding a node that is a left child. Return the position of the parent node.
      • If no such node is found, return null.
  3. Postorder Traversal:

    • In a postorder traversal, the order of visiting nodes is left, right, root.
    • To find the position visited after p in a postorder traversal:
      • If p is the root of the tree, return null since there is no node visited after the root.
      • If p is the left child of its parent, return the position of the right sibling of its parent.
      • Otherwise, go up the tree until finding a node that is a left child. Return the position of the right sibling of that parent node.
      • If no such node is found, return null.

The worst-case running time of these algorithms depends on the height of the tree. If the tree is balanced, the worst-case running time would be O(log n) for a tree with n nodes. However, if the tree is skewed (e.g., a degenerate tree), the worst-case running time would be O(n) where n is the number of nodes in the tree. This is because we may need to traverse the entire height of the tree to find the next position.

It's important to note that these algorithms assume the availability of methods to traverse the tree and access the left and right children of a node. The time complexity of these operations depends on the specific tree implementation used.

0 0

Discussions

Post the discussion to improve the above solution.