I already have some code. Can you implement preorder and postorder traversal of
ID: 3761732 • Letter: I
Question
I already have some code. Can you implement preorder and postorder traversal of tree and show output. public class BinaryTreeExample { public static void main (String[] args) { new BinaryTreeExample().run(); } static class Node { Node left; Node right; int value; public Node(int value) { this.value = value; } } public void run() { Node rootnode = new Node (25); System.out.println("Building tree with rootvaule" + rootnode.value); System.out.println("======================="); insert(rootnode, 11); insert(rootnode, 15); insert(rootnode, 16); insert(rootnode, 23); insert(rootnode, 79); System.out.println("Traveling tree in order"); System.out.println("======================="); printInOrder(rootnode); } public void insert(Node node, int value) { if(value < node.value) { if(node.left != null) { insert(node.left, value); } else{ System.out.println(" Inserted " + value + " to left of node" + node.value); node.left = new Node (value); } } else if (value > node.value){ if (node.right != null) { insert(node.right, value); } else { System.out.println (" Inserted " + value + " to right of node " + node.value); node.right = new Node(value); } } } public void printInOrder (Node node) { if (node != null) { printInOrder(node.left); System.out.println(" Traveled " + node.value); printInOrder(node.right); } } }
Explanation / Answer
To traverse a non-empty binary search tree in inorder (symmetric), perform the following operations recursively at each node: (LNR) 1. Traverse the left subtree. 2. go to root. 3. Traverse the right subtree. To traverse a non-empty binary search tree in preorder, the following operations are done recursively at each node, starting with the root node: (NLR) 1. go to root. 2. Traverse the left subtree. 3. Traverse the right subtree. To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node: (LRN) 1. Traverse the left subtree. 2. Traverse the right subtree. 3. go to root. import java.io.*; import java.util.*; class Node { public int item; public Node leftChild; public Node rightChild; public void displayNode() { System.out.print("["); System.out.print(item); System.out.print("]"); } } class StackNode { public Node item; public StackNode next; public StackNode(Node val) { item = val; } } class LinkedListStack { private StackNode first; public LinkedListStack() { first = null; } public boolean isEmpty() { return (first==null); } public void insert(Node key)//inserts at beginning of list { StackNode newLLNode = new StackNode(key); newLLNode.next = first; first = newLLNode; } public Node delete()//deletes at beginning of list { StackNode temp = first; first = first.next; return temp.item; } } class Stack { private LinkedListStack listObj; public Stack() { listObj = new LinkedListStack(); } public void push(Node num) { listObj.insert(num); } public Node pop() { return listObj.delete(); } public boolean isEmpty() { return listObj.isEmpty(); } } class Tree { public Node root; public Tree() { root = null; } public Node returnRoot() { return root; } public void insert(int id) { Node newNode = new Node(); newNode.item = id; if(root==null) root = newNode; else { Node current = root; Node parent; while(true) { parent = current; if(id < current.item) { current = current.leftChild; if(current == null) { parent.leftChild = newNode; return; } } else { current = current.rightChild; if(current == null) { parent.rightChild = newNode; return; } } } } } public void preOrder(Node Root) { if(Root != null) { System.out.print(Root.item + " "); preOrder(Root.leftChild); preOrder(Root.rightChild); } } public void inOrder(Node Root) { if(Root != null) { inOrder(Root.leftChild); System.out.print(Root.item + " "); inOrder(Root.rightChild); } } public void postOrder(Node Root) { if(Root != null) { postOrder(Root.leftChild); postOrder(Root.rightChild); System.out.print(Root.item + " "); } } public void displayTree() { Stack globalStack = new Stack(); globalStack.push(root); int emptyLeaf = 32; boolean isRowEmpty = false; System.out.println("****......................................................****"); while(isRowEmpty==false) { Stack localStack = new Stack(); isRowEmpty = true; for(int j=0; j<emptyLeaf; j++) System.out.print(' '); while(globalStack.isEmpty()==false) { Node temp = globalStack.pop(); if(temp != null) { System.out.print(temp.item); localStack.push(temp.leftChild); localStack.push(temp.rightChild); if(temp.leftChild != null ||temp.rightChild != null) isRowEmpty = false; } else { System.out.print("--"); localStack.push(null); localStack.push(null); } for(int j=0; j<emptyLeaf*2-2; j++) System.out.print(' '); } System.out.println(); emptyLeaf /= 2; while(localStack.isEmpty()==false) globalStack.push( localStack.pop() ); } System.out.println("****......................................................****"); } } class TreeTraversal { public static void main(String[] args) throws IOException { int value; Tree theTree = new Tree(); theTree.insert(42); theTree.insert(25); theTree.insert(65); theTree.insert(12); theTree.insert(37); theTree.insert(13); theTree.insert(30); theTree.insert(43); theTree.insert(87); theTree.insert(99); theTree.insert(9); System.out.println("Displaying the tree"); theTree.displayTree(); System.out.println("Inorder traversal"); theTree.inOrder(theTree.returnRoot()); System.out.println(" "); System.out.println("Preorder traversal"); theTree.preOrder(theTree.returnRoot()); System.out.println(" "); System.out.println("Postorder traversal"); theTree.postOrder(theTree.returnRoot()); System.out.println(" "); } } OUTPUT: ~/Tree$ java TreeTraversal Displaying the tree ****......................................................**** 42 25 65 12 37 43 87 9 13 30 -- -- -- -- 99 ****......................................................**** Inorder traversal 9 12 13 25 30 37 42 43 65 87 99 Preorder traversal 42 25 12 9 13 37 30 65 43 87 99 Postorder traversal 9 13 12 30 37 25 43 99 87 65 42
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.