Question

Write a (void) method to traverse a binary tree in Breadth-First order, where you start from...

Write a (void) method to traverse a binary tree in Breadth-First order, where you start from the root, print the node values of its children, (left and then right) (these are at level 1), then print all node values at level 2 from left to right, and then level 3 etc. For the example binary tree below, you should get the result A, B, E, C, F, D, G, H. Notice that this type of traversal is not the same as any of the three methods we discussed in class (inOrder, preOrder, and postOrder). Hint: No recursion needed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.LinkedList;

public class BinaryTree {

TreeNode root = null;

TreeNode current, parent;

private int size = 0, counter = 0;

public int getSize() {

return this.size;

}

public boolean insert(char el) {

current = root;

if (current == null) {

root = new TreeNode(el);

} else {

parent = null;

current = root;

while (current != null) {

if (el < current.element) {

parent = current;

current = current.left;

} else if (el > current.element) {

parent = current;

current = current.right;

} else {

return false;

}

}

if (el < parent.element) {

parent.left = new TreeNode(el);

}

if (el > parent.element) {

parent.right = new TreeNode(el);

}

}

this.size++;

return true;

}

public void levelOrderTraversal(TreeNode node) {

if (node == null)

return ;

LinkedList<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(node);

while (!queue.isEmpty()) {

TreeNode tempNode = queue.poll();

System.out.println(tempNode.element + " ");

if (tempNode.left != null)

queue.add(tempNode.left);

if (tempNode.right != null)

queue.add(tempNode.right);

}

}

public void levelOrderTraversal() {

levelOrderTraversal(root);

}

}

------------------------------------------------------------------------------------------------------------------------------------------------

// start Driver class

public class TreeDriver {

public static void main(String[] args) {

BinaryTree tree = new BinaryTree();

tree.insert('A');

tree.insert('B');

tree.insert('E');

tree.insert('C');

tree.insert('F');

tree.insert('D');

tree.insert('G');

tree.insert('H');

System.out.println("Breadth first Traversal: ");

tree.levelOrderTraversal();

}

}

-----------------------------------------------------------------------------------------------------------------

// start TreeNode class

public class TreeNode {

char element;

TreeNode left;

TreeNode right;

public TreeNode(char el) {

this.element = el;

}

}

------------------------------------------------------------------------------------------------------------------------------------------------

SEE OUTPUT

PLEASE COMMENT if there is any concern.

========================================================================

Add a comment
Know the answer?
Add Answer to:
Write a (void) method to traverse a binary tree in Breadth-First order, where you start from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first...

    QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first preorder postorder inorder 0.10000 points    QUESTION 9 What kind of traversal does the following algorithm (in pseudo-code) describe? Algorithm traversal (root) if (root is not null)      traversal (leftSubTree)      process (root)      traversal (rightSubTree) end if end traversal breadth first preorder inorder postorder 0.10000 points    QUESTION 10 What kind of traversal does the following algorithm (in pseudo-code)  describe? Algorithm traversal (root) if...

  • Please help me with me. I did the first part to write the operations but in...

    Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • (Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order...

    (Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order traversal. To "process" or "visit" a node means to print the data at that node. Pre-order traversal output: 10 6 4 8 18 15 21 Post-order traversal output: 4 8 6 15 21 18 10 Additionally, create and traverse the following trees: Algorithm preorder(p) perform the "visit" action for position p this happens before any recursion for each child c in children(p) do recursively...

  • Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from...

    Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT