{"id":26964,"date":"2021-05-06T23:10:05","date_gmt":"2021-05-07T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/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\/"},"modified":"2021-05-06T23:10:05","modified_gmt":"2021-05-07T03:10:05","slug":"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","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26964","title":{"rendered":"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"},"content":{"rendered":"<pre>\nTreetest.java\n\/** A NodeOperator that prints each node.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\nclass PrintOperator implements NodeOperator {\n  public void operateOn(Node node) {\n    System.out.println(node.getNodeValue());\n  }\n}\n\n\/** A sample tree representing a parse tree of\n *  the sentence &quot;Java hackers hack Java&quot;, using\n *  some simple context-free grammar.\n *\/\n\npublic class TreeTest {\n  public static void main(String[] args) {\n    Node adjective =\n      new Node(&quot;  Adjective&quot;, new Leaf(&quot;   Java&quot;));\n    Node noun1 =\n      new Node(&quot;  Noun&quot;, new Leaf(&quot;   hackers&quot;));\n    Node verb =\n      new Node(&quot;  TransitiveVerb&quot;, new Leaf(&quot;   hack&quot;));\n    Node noun2 =\n      new Node(&quot;  Noun&quot;, new Leaf(&quot;   Java&quot;));\n    Node np = new Node(&quot; NounPhrase&quot;, adjective, noun1);\n    Node vp = new Node(&quot; VerbPhrase&quot;, verb, noun2);\n    Node sentence = new Node(&quot;Sentence&quot;, np, vp);\n    PrintOperator printOp = new PrintOperator();\n    System.out.println(&quot;Depth first traversal:&quot;);\n    sentence.depthFirstSearch(printOp);\n    System.out.println(&quot;nBreadth first traversal:&quot;);\n    sentence.breadthFirstSearch(printOp);\n  }\n}\n\nLeaf.java\nA leaf node with no children.\n\/** Leaf node: a node with no subtrees.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic class Leaf extends Node {\n  public Leaf(Object value) {\n    super(value, null, null);\n  }\n}\n\nNode.java A data structure representing a node in a binary tree. \n\n\nimport java.util.Vector;\n\n\/** A data structure representing a node in a binary tree.\n *  It contains a node value and a reference (pointer) to\n *  the left and right subtrees.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic class Node {\n  private Object nodeValue;\n  private Node leftChild, rightChild;\n\n \/** Build Node with specified value and subtrees. *\/\n\n  public Node(Object nodeValue, Node leftChild,\n              Node rightChild) {\n    this.nodeValue = nodeValue;\n    this.leftChild = leftChild;\n    this.rightChild = rightChild;\n  }\n\n  \/** Build Node with specified value and L subtree. R child\n   *  will be null. If you want both children to be null, use\n   *  the Leaf constructor.\n   *\/\n\n  public Node(Object nodeValue, Node leftChild) {\n    this(nodeValue, leftChild, null);\n  }\n\n  \/** Return the value of this node. *\/\n\n  public Object getNodeValue() {\n    return(nodeValue);\n  }\n\n  \/** Specify the value of this node. *\/\n\n  public void setNodeValue(Object nodeValue) {\n    this.nodeValue = nodeValue;\n  }\n\n \/** Return the L subtree. *\/\n\n  public Node getLeftChild() {\n    return(leftChild);\n  }\n\n  \/** Specify the L subtree. *\/\n\n  public void setLeftChild(Node leftChild) {\n    this.leftChild = leftChild;\n  }\n\n  \/** Return the R subtree. *\/\n\n  public Node getRightChild() {\n    return(rightChild);\n  }\n\n  \/** Specify the R subtree. *\/\n\n  public void setRightChild(Node rightChild) {\n    this.rightChild = rightChild;\n  }\n\n  \/** Traverse the tree in depth-first order, applying\n   *  the specified operation to each node along the way.\n   *\/\n\n  public void depthFirstSearch(NodeOperator op) {\n    op.operateOn(this);\n    if (leftChild != null) {\n      leftChild.depthFirstSearch(op);\n    }\n    if (rightChild != null) {\n      rightChild.depthFirstSearch(op);\n    }\n  }\n\n  \/** Traverse the tree in breadth-first order, applying the\n   *  specified operation to each node along the way.\n   *\/\n\n  public void breadthFirstSearch(NodeOperator op) {\n    Vector nodeQueue = new Vector();\n    nodeQueue.addElement(this);\n    Node node;\n    while(!nodeQueue.isEmpty()) {\n      node = (Node)nodeQueue.elementAt(0);\n      nodeQueue.removeElementAt(0);\n      op.operateOn(node);\n      if (node.getLeftChild() != null) {\n        nodeQueue.addElement(node.getLeftChild());\n      }\n      if (node.getRightChild() != null) {\n        nodeQueue.addElement(node.getRightChild());\n      }\n    }\n  }\n}\n\nNodeOperator.java An interface used in the Node class to ensure that an object has an operateOn method. \n\n\/** An interface used in the Node class to ensure that\n *  an object has an operateOn method.\n *\n *  Taken from Core Web Programming from\n *  Prentice Hall and Sun Microsystems Press,\n *  .\n *  &copy; 2001 Marty Hall and Larry Brown;\n *  may be freely used or adapted.\n *\/\n\npublic interface NodeOperator {\n  void operateOn(Node node);\n}\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10221<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Basic Java<br \/>Tags:Java\/J2EE\/J2MEBasic Java<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Treetest.java \/** A NodeOperator that prints each node. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * &copy; 2001 Marty Hall and Larry Brown; * may be freely used or adapted. *\/ class PrintOperator implements NodeOperator { public void operateOn(Node node) { System.out.println(node.getNodeValue()); } } \/** &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26964\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26964","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10508,"url":"http:\/\/bangla.sitestree.com\/?p=10508","url_meta":{"origin":26964,"position":0},"title":"TreeTest.java Builds a binary tree and prints the contents of the nodes. Uses the following classes:","author":"","date":"August 29, 2015","format":false,"excerpt":"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\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":26964,"position":1},"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":26856,"url":"http:\/\/bangla.sitestree.com\/?p=26856","url_meta":{"origin":26964,"position":2},"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":26964,"position":3},"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":10267,"url":"http:\/\/bangla.sitestree.com\/?p=10267","url_meta":{"origin":26964,"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":26964,"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\/26964","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26964"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26964\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26964"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}