Exercise 3 Tree

download Exercise 3 Tree

of 7

Transcript of Exercise 3 Tree

  • 8/4/2019 Exercise 3 Tree

    1/7

    EXERCISES 3

    Tree

    1. For each tree shown below

    (A) (B)

    a. Answer these questions: What is its height? Is it a full binary tree? Is it a

    complete binary tree? Is it a binary search tree?

    b. If visiting a node displays the integer value stored, show the inorder, preorder

    and postorder traversal of each tree

    2. Show the tree that would be built by the following input string, then create this tree

    using Create method

    30

    154

    null

    null

    20

    18

    null

    19

    null

    null

    null35

    32

    null

    null

    38

    null

    4

    0

    3

    0

    3

    2

    5

    5

    4

    0

    3

    0

    3

    5

    3

    2

    5

    5

    5

    0

  • 8/4/2019 Exercise 3 Tree

    2/7

    null

    3. Show the BSTree that would be formed by adding following values to the tree:

    a. happy, depressed, manic, sad, ecstatic

    b. 45, 30, 15, 50, 60, 20, 25, 90

    4. For each tree shown below, show or explain the effect of removing the nodes 15, 30

    from the tree

    5. Build a class for Binary Search Tree of objects having parameter type

    a. Implement a recursive and non-recursive method that adds a value to the

    BSTree, and returns true if the add is sucessful, otherwise returns false

    b. Implement a recursive and non-recursive method that searches a BSTreelooking for the node having specified value and returns a reference variable to that

    node if found, otherwise returns null

    c. Implement a method to remove a specified value from the BSTree, and return

    the removed value if the remove is sucessful, otherwise return null

    d. Implement recursive and non-recursive methods to perform an inorder, preorderand postorder traversal of a BSTree and display the nodes value

    e. Implement a method that returns the number of nodes in the BST

    f. Implement a method that returns the number of nodes having odd value in the

    BST

    g. Implement a method that returns the average of nodes having even value in the

    BSTh. Implement a method that returns the height of the BST

    i. Implement a method that returns the depth or level of the node having specified

    value

    j. Implement a method that returns the number of leaf nodes in the BST

    k. Implement a method that returns the number of nodes at the first level in the

    BST

    2

    8

    1

    5

    2

    3

    11

    3

    7

    30

    42

    17

    2

    62

    5

    5

    7

    1

    63

    83

    5

    1

    52

    49

    7

    0

    6

    0

    9

    0

    4

    8

  • 8/4/2019 Exercise 3 Tree

    3/7

    l. Implement a method that returns the number of nodes at the second level in the

    BST

    m. Implement a method that returns the number of nodes at specified level in the

    BST

    n. Implement a method that returns the number of internal nodes in the BST

    o. Write a recursive and non-recursive method to check if the tree is binary searchtree

    Recursive:

    left subtree: BST

    and right subtree: BST

    and max(datas of left childs) < root.data < min(datas of right childs)

    Non-Recursive:

    inorder traversal, if node datas are increasing: BSTp. Implement a method that returns the height of the node having specified value

    q. Implement a method that displays the values of nodes at specified level in the

    BST6. Build a class for Balanced Search Tree of objects having parameter type

    a. Implement a method that adds a value to the Tree, and returns true if the add is

    sucessful, otherwise returns falseb. Write a method to check if the tree is balanced binary tree

    7. For BSTree shown below:

    a. Draw the BSTree that would be built by adding following values to the tree:

    32, 69, 18, 11, 28, 70, 23, 9, 37, 47

    b. 45, 30, 15, 50, 60, 20, 25, 90

    c. Show the inorder, preorder and postorder traversal for displaying the nodesvalue in the tree after adding above values

    8. Create following tree using Create method

    root

    3

    8

    3

    1

    T

    8

    1

    51

    1

    1

    0

    63

    0

    2 41

    2

  • 8/4/2019 Exercise 3 Tree

    4/7

    9. Draw process of stack uesed to build binary expression trees for the following infixexpressions. Your trees should enforce the Java rules for operator evaluation (higher-

    precedence operators before lower-precedence operators and left associativity)

    a. x/y + a b*cb. x*a y/b * (c+d)

    10.For expression: (A+B) * (H - (E F ^ D) / C)

    a. Build expression tree from the above expressionb. Build postfix expression from the expression

    c. Present algorism of evaluating an expression treed. Draw process of stack used to evaluate the expression

    import java.util.*;

    publicclass BSTree {

    private Node root;

    publicstaticclass Node {

    private Nodeleft;

    private Noderight;

    private E data;

    //Tree includes 1 node having the value d

    public Node(E d){

    left = null;

    right = null;

    data = d;

    }

    //Tree includes the root node d,

    //and left subtree l, right subtree rpublic Node(E d, Nodel, Noder) {

    left = l;

    right = r;

    data = d;

    }

    }

    //Initiate tree having root

    public Node Create(E d){

  • 8/4/2019 Exercise 3 Tree

    5/7

    root = new Node(d);

    returnroot;

    }

    //Initiate tree having root, and left subtree l, right subtree r

    public Node Create(E d, Node l, Node r){

    root = new Node(d, l, r);

    returnroot;

    }

    //inorder traversal

    privatevoid inorderTraversal(Node r) {

    if (r == null) return;

    inorderTraversal(r.left);

    System.out.print(r.data+" ");

    inorderTraversal(r.right);

    }

    publicvoid inorderTraversal(){

    inorderTraversal(root);

    }

    //inorder traversal return Stringprivate String inorderTraversal1(Node r) {

    if (r == null) return"";

    return inorderTraversal1(r.left) + r.data +

    inorderTraversal1(r.right);

    }

    public String innorderTraversal1(){

    return inorderTraversal1(root);

    }

    //preorder traversal return String

    privatevoid preorderTraversal1(Node r) {

    if (r == null) return;

    System.out.print( r.data+" ");

    preorderTraversal1(r.left);

    preorderTraversal1(r.right);

    }

    publicvoid prenorderTraversal1(){

    preorderTraversal1(root);

    }

    //postorder traversal return String

    privatevoid postorderTraversal1(Node r) {

    if (r == null) return;

    postorderTraversal1(r.left);

    postorderTraversal1(r.right);

    System.out.print(r.data+" ");}

    publicvoid postorderTraversal1(){

    postorderTraversal1(root);

    }

    /**********

    *Thissupportroutinesearchesaspecifiedsubtreelooking

    *fortheitemspecifiedasthetarget.

    *@paramr Therootofasubtreetobesearched

  • 8/4/2019 Exercise 3 Tree

    6/7

    *@paramtarget Thetargetofthesearch

    *@return Thenodehavingthetargetvalueofthesearchif

    found,otherwisenull

    */

    private Node searchBST(Node r, Comparable target) {

    if (r == null) returnnull;

    int result = target.compareTo(r.data);

    if (result == 0) return r;

    if (result < 0) return searchBST(r.left, target);

    return searchBST(r.right, target);

    }

    /**********

    *ThisroutinesearchestheBSTreelookingforthespecifieditem.

    *@paramtarget Thetargetofthesearch

    *@return Thetargetisreturnediffoundoranullis

    */

    public Node searchBST(E target) {

    return searchBST((Node)root,(Comparable)target);

    }

    privatebooleanreturnValue; // Used to signal if an add was successful

    /**********

    *Thismethodistheentrypointroutinesoutsideofthisclassusetoadd

    *avaluetotheBST.

    *@paramnewValueThetargetofthesearch

    *@returnThetargetisreturnediffoundoranullis

    */

    publicboolean add(Comparable newValue) {

    root = add(root, newValue);

    returnreturnValue;

    }

    /**********

    *ThisisprivatemethodisusedtoaddavaluetotheBSTviarecursion.

    *@paramnewValueThetargetofthesearch

    *@returnAreferencetotheupdatedsubtreeaftertheadd

    */

    private Node add(Node r, Comparable newValue) {

    if (r == null)

    returnnew Node(newValue);

    int compareResult = newValue.compareTo(r.data);

    if (compareResult == 0)

    return r;

    if (compareResult < 0) {

    r.left = add(r.left, newValue);

    return r;

    }r.right = add(r.right, newValue);

    return r;

    }

    publicstaticvoid main(String[] args){

    BSTree tree = new BSTree();

    tree.add(24);

    tree.add(15);

    tree.add(30);

  • 8/4/2019 Exercise 3 Tree

    7/7

    tree.add(5);

    tree.add(7);

    tree.add(25);

    tree.add(56);

    tree.add(17);

    tree.add(22);

    tree.add(20);

    tree.inorderTraversal();

    System.out.println();

    tree.preorderTraversal1();

    System.out.println();

    //Creat Binary tree

    /*BSTree tree = new BSTree();

    tree.Create(7,

    tree.Create(5, null, tree.Create(9, tree.Create(11), null)),

    tree.Create(8, tree.Create(6),

    tree.Create(3, tree.Create(2),tree.Create(4))

    )

    );

    tree.inorderTraversal();

    */}

    }