{"id":10508,"date":"2015-08-29T00:36:28","date_gmt":"2015-08-29T04:36:28","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10508"},"modified":"2015-08-24T10:32:54","modified_gmt":"2015-08-24T14:32:54","slug":"treetest-java-builds-a-binary-tree-and-prints-the-contents-of-the-nodes-uses-the-following-classes","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10508","title":{"rendered":"TreeTest.java Builds a binary tree and prints the contents of the nodes. Uses the following classes:"},"content":{"rendered":"<pre>Treetest.java\r\n\/** A NodeOperator that prints each node.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\nclass PrintOperator implements NodeOperator {\r\n\u00a0 public void operateOn(Node node) {\r\n\u00a0\u00a0\u00a0 System.out.println(node.getNodeValue());\r\n\u00a0 }\r\n}\r\n\r\n\/** A sample tree representing a parse tree of\r\n\u00a0*\u00a0 the sentence \"Java hackers hack Java\", using\r\n\u00a0*\u00a0 some simple context-free grammar.\r\n\u00a0*\/\r\n\r\npublic class TreeTest {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 Node adjective =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new Node(\"\u00a0 Adjective\", new Leaf(\"\u00a0\u00a0 Java\"));\r\n\u00a0\u00a0\u00a0 Node noun1 =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new Node(\"\u00a0 Noun\", new Leaf(\"\u00a0\u00a0 hackers\"));\r\n\u00a0\u00a0\u00a0 Node verb =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new Node(\"\u00a0 TransitiveVerb\", new Leaf(\"\u00a0\u00a0 hack\"));\r\n\u00a0\u00a0\u00a0 Node noun2 =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new Node(\"\u00a0 Noun\", new Leaf(\"\u00a0\u00a0 Java\"));\r\n\u00a0\u00a0\u00a0 Node np = new Node(\" NounPhrase\", adjective, noun1);\r\n\u00a0\u00a0\u00a0 Node vp = new Node(\" VerbPhrase\", verb, noun2);\r\n\u00a0\u00a0\u00a0 Node sentence = new Node(\"Sentence\", np, vp);\r\n\u00a0\u00a0\u00a0 PrintOperator printOp = new PrintOperator();\r\n\u00a0\u00a0\u00a0 System.out.println(\"Depth first traversal:\");\r\n\u00a0\u00a0\u00a0 sentence.depthFirstSearch(printOp);\r\n\u00a0\u00a0\u00a0 System.out.println(\"\\nBreadth first traversal:\");\r\n\u00a0\u00a0\u00a0 sentence.breadthFirstSearch(printOp);\r\n\u00a0 }\r\n}\r\n\r\nLeaf.java\r\nA leaf node with no children.\r\n\/** Leaf node: a node with no subtrees.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class Leaf extends Node {\r\n\u00a0 public Leaf(Object value) {\r\n\u00a0\u00a0\u00a0 super(value, null, null);\r\n\u00a0 }\r\n}\r\n\r\nNode.java A data structure representing a node in a binary tree. \r\n\r\n\r\nimport java.util.Vector;\r\n\r\n\/** A data structure representing a node in a binary tree.\r\n\u00a0*\u00a0 It contains a node value and a reference (pointer) to\r\n\u00a0*\u00a0 the left and right subtrees.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class Node {\r\n\u00a0 private Object nodeValue;\r\n\u00a0 private Node leftChild, rightChild;\r\n\r\n\u00a0\/** Build Node with specified value and subtrees. *\/\r\n\r\n\u00a0 public Node(Object nodeValue, Node leftChild,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Node rightChild) {\r\n\u00a0\u00a0\u00a0 this.nodeValue = nodeValue;\r\n\u00a0\u00a0\u00a0 this.leftChild = leftChild;\r\n\u00a0\u00a0\u00a0 this.rightChild = rightChild;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Build Node with specified value and L subtree. R child\r\n\u00a0\u00a0 *\u00a0 will be null. If you want both children to be null, use\r\n\u00a0\u00a0 *\u00a0 the Leaf constructor.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public Node(Object nodeValue, Node leftChild) {\r\n\u00a0\u00a0\u00a0 this(nodeValue, leftChild, null);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Return the value of this node. *\/\r\n\r\n\u00a0 public Object getNodeValue() {\r\n\u00a0\u00a0\u00a0 return(nodeValue);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Specify the value of this node. *\/\r\n\r\n\u00a0 public void setNodeValue(Object nodeValue) {\r\n\u00a0\u00a0\u00a0 this.nodeValue = nodeValue;\r\n\u00a0 }\r\n\r\n\u00a0\/** Return the L subtree. *\/\r\n\r\n\u00a0 public Node getLeftChild() {\r\n\u00a0\u00a0\u00a0 return(leftChild);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Specify the L subtree. *\/\r\n\r\n\u00a0 public void setLeftChild(Node leftChild) {\r\n\u00a0\u00a0\u00a0 this.leftChild = leftChild;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Return the R subtree. *\/\r\n\r\n\u00a0 public Node getRightChild() {\r\n\u00a0\u00a0\u00a0 return(rightChild);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Specify the R subtree. *\/\r\n\r\n\u00a0 public void setRightChild(Node rightChild) {\r\n\u00a0\u00a0\u00a0 this.rightChild = rightChild;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Traverse the tree in depth-first order, applying\r\n\u00a0\u00a0 *\u00a0 the specified operation to each node along the way.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void depthFirstSearch(NodeOperator op) {\r\n\u00a0\u00a0\u00a0 op.operateOn(this);\r\n\u00a0\u00a0\u00a0 if (leftChild != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 leftChild.depthFirstSearch(op);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 if (rightChild != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 rightChild.depthFirstSearch(op);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** Traverse the tree in breadth-first order, applying the\r\n\u00a0\u00a0 *\u00a0 specified operation to each node along the way.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void breadthFirstSearch(NodeOperator op) {\r\n\u00a0\u00a0\u00a0 Vector nodeQueue = new Vector();\r\n\u00a0\u00a0\u00a0 nodeQueue.addElement(this);\r\n\u00a0\u00a0\u00a0 Node node;\r\n\u00a0\u00a0\u00a0 while(!nodeQueue.isEmpty()) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 node = (Node)nodeQueue.elementAt(0);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 nodeQueue.removeElementAt(0);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 op.operateOn(node);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (node.getLeftChild() != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nodeQueue.addElement(node.getLeftChild());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (node.getRightChild() != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nodeQueue.addElement(node.getRightChild());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}\r\n\r\nNodeOperator.java An interface used in the Node class to ensure that an object has an operateOn method. \r\n\r\n\/** An interface used in the Node class to ensure that\r\n\u00a0*\u00a0 an object has an operateOn method.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic interface NodeOperator {\r\n\u00a0 void operateOn(Node node);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Treetest.java \/** A NodeOperator that prints each node. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/ class PrintOperator implements NodeOperator { \u00a0 public void operateOn(Node node) { \u00a0\u00a0\u00a0 System.out.println(node.getNodeValue()); \u00a0 &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10508\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10508","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26964,"url":"http:\/\/bangla.sitestree.com\/?p=26964","url_meta":{"origin":10508,"position":0},"title":"TreeTest.java Builds a binary tree and prints the contents of the nodes. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Basic Java","author":"Author-Check- Article-or-Video","date":"May 6, 2021","format":false,"excerpt":"Treetest.java \/** A NodeOperator that prints each node. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001 Marty Hall and Larry Brown; * may be freely used or adapted. *\/ class PrintOperator implements NodeOperator { public void operateOn(Node node)\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26856,"url":"http:\/\/bangla.sitestree.com\/?p=26856","url_meta":{"origin":10508,"position":1},"title":"DOM example that represents the basic structure of an XML document as a JTree #Programming Code Examples #Java\/J2EE\/J2ME #JavaScript","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10245,"url":"http:\/\/bangla.sitestree.com\/?p=10245","url_meta":{"origin":10508,"position":2},"title":"DOM example that represents the basic structure of an XML document as a JTree","author":"","date":"August 25, 2015","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: \u00a0\u00a0\u00a0 * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27190,"url":"http:\/\/bangla.sitestree.com\/?p=27190","url_meta":{"origin":10508,"position":3},"title":"JTree Examples #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; \/** Example tree built out of DefaultMutableTreeNodes. * *\/ public class SimpleTree extends JFrame { public static void main(String[] args)\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10267,"url":"http:\/\/bangla.sitestree.com\/?p=10267","url_meta":{"origin":10508,"position":4},"title":"JTree Examples","author":"","date":"August 26, 2015","format":false,"excerpt":"SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; \/** Example tree built out of DefaultMutableTreeNodes. \u00a0* \u00a0*\/ public class SimpleTree extends JFrame { \u00a0 public static void main(String[]\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76201,"url":"http:\/\/bangla.sitestree.com\/?p=76201","url_meta":{"origin":10508,"position":5},"title":"Steps to Create a Binary Search Tree","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"If binary tree is empty, create a node, and assign data, point to itOtherwise : point to root node Compare the new - data - to - insert with the current node dataWe maintain a pointer say current to point to the node under visit If data matched : nothing\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10508"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10508\/revisions"}],"predecessor-version":[{"id":10509,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10508\/revisions\/10509"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10508"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}