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

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.
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
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...
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.
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Given an array of foods, create a binary search tree. Then, make a copy of that...
Given an array of foods, create a binary search tree. Then, make a copy of that BST and balance it. Language is C++. Vectors are not allowed. The balance function definition: void balance(BST treeObj); and then when writing the function: void BST::balance(BST treeObj) where BST is a class. The function will be called in main like: originalTree.balance(balancedTree);
FIRST You are given a binary string x and an array A[1 : n] of binary...
FIRST You are given a binary string x and an array A[1 : n] of binary strings. Assume that x and the elements of A have the same length. Let ⊕ denote the xor operator on binary strings. For example 1010 ⊕ 1101 = 0111, and 1110 ⊕ 1111 = 0001. Assume that xor’ing two strings takes O(1) time. Give a divide-and-conquer algorithm to check if there’s a subarray A[i : j] of A such that A[i] ⊕ · ·...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT