Question

In: Computer Science

Let T be a binary tree with n positions that is realized with an array representation...

Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.

Solutions

Expert Solution

//Pseudo code for each methods

Lets assume Tree 'T' is global declared as an array 'A'.
and 'N' is size of Array.

//1.
    {
        int root()
        {
            return A[0];
        }
      
    }

//2.
    {
        int parent(int val)
        {
            int i;
            for(i=0;i<(N/2);i++)
            {
                if((((2*i)+1)<N)&&A[(2*i)+1]==val)
                    return A[i];
                else if((((2*i)+2)<N)&&A[(2*val)+2]==val)
                    return A[i];
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }
  
//3.
    {
        int left(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(((2*i)+1)<N)
                {
                    if(A[i]==val)
                        return A[((2*i)+1)];
                }
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }

//4.
    {
        int right(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(((2*i)+2)<N)
                {
                    if(A[i]==val)
                        return A[((2*i)+2)];
                }
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }
  
//5.
    {
        bool IsExternal(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(A[i]==val)
                return true;
            }
            return false;
        }
    }
  
//6.
    {
        int IsRoot(int val)
        {
            if(A[0]==val)
            return true;
            return false;
        }
    }


Related Solutions

a.)  Let T be a binary tree with n nodes. Define the lowest common ancestor (LCA) between...
a.)  Let T be a binary tree with n nodes. Define the lowest common ancestor (LCA) between two nodes v and w as the lowest node in T that has both v and w as descendants. Given two nodes v and w, write an efficient algorithm, LCA(v, w), for finding the LCA of v and w. Note: A node is a descendant of itself and v.depth gives a depth of a node v. b.) What is the running time of your...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element.""" def __init__(self, container, node): def element(self): def __eq__(self, other): # ------------------------------- utility methods ------------------------------- def _validate(self, p): """Return associated node, if position is valid.""" def _make_position(self, node): """Return Position...
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3...
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3 ) * ( 3 - 2 ) ) / ( ( 3 + 10 ) + ( ( 8 - 3 ) - 2 ) ) * 9 )" -Give an O(n)-time algorithm for computing the depth of all positions of a tree T, where n is the number of nodes of T.
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Given a binary search tree T with n elements. For any two keys k1 and k2...
Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.
Given a binary search tree T with n elements. For any two keys k1 and k2...
Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT