{"id":26903,"date":"2017-12-26T21:12:21","date_gmt":"2017-12-26T15:42:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26903"},"modified":"2017-12-26T21:12:21","modified_gmt":"2017-12-26T15:42:21","slug":"java-programming-binary-tree-introduction","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-binary-tree-introduction\/","title":{"rendered":"Java Programming &#8211; Binary Tree | Set 1 (Introduction)"},"content":{"rendered":"<p><strong>Trees:<\/strong> Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures.<span id=\"more-142770\"><\/span><\/p>\n<p><strong>Tree Vocabulary: <\/strong>The topmost node is called root of the tree. The elements that are directly under an element are called its children. The element directly above something is called its parent. For example, a is a child of f and f is the parent of a. Finally, elements with no children are called leaves.<\/p>\n<pre>      tree\r\n      ----\r\n       j    &lt;-- root\r\n     \/   \\\r\n    f      k  \r\n  \/   \\      \\\r\n a     h      z    &lt;-- leaves<\/pre>\n<p><strong>Why Trees?<\/strong><br \/>\n<strong>1.<\/strong> One reason to use trees might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer:<\/p>\n<pre>file system\r\n-----------\r\n     \/    &lt;-- root\r\n  \/      \\\r\n...       home\r\n      \/          \\\r\n   ugrad        course\r\n    \/       \/      |     \\\r\n  ...      cs101  cs112  cs113<\/pre>\n<p><strong>2.<\/strong> Trees (with some ordering e.g., BST) provide moderate access\/search (quicker than Linked List and slower than arrays).<br \/>\n<strong>3.<\/strong> Trees provide moderate insertion\/deletion (quicker than Arrays and slower than Unordered Linked Lists).<br \/>\n<strong>4.<\/strong> Like Linked Lists and unlike Arrays, Trees don\u2019t have an upper limit on number of nodes as nodes are linked using pointers.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Main applications of trees include:<\/strong><br \/>\n<strong>1.<\/strong> Manipulate hierarchical data.<br \/>\n<strong>2.<\/strong> Make information easy to search (see tree traversal).<br \/>\n<strong>3.<\/strong> Manipulate sorted lists of data.<br \/>\n<strong>4.<\/strong> As a workflow for compositing digital images for visual effects.<br \/>\n<strong>5. <\/strong>Router algorithms<br \/>\n<strong>6. <\/strong>Form of a multi-stage decision-making (see business chess).<\/p>\n<p><strong>Binary Tree:<\/strong> A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.<\/p>\n<p><strong>Binary Tree Representation in C: <\/strong>A tree is represented by a pointer to the topmost node in tree. If the tree is empty, then value of root is NULL.<br \/>\nA Tree node contains following parts.<br \/>\n1. Data<br \/>\n2. Pointer to left child<br \/>\n3. Pointer to right child<\/p>\n<p>In C, we can represent a tree node using structures. Below is an example of a tree node with an integer data.<\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java comments\">\/* Class containing left and right child of current<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"java comments\">node and key value*\/<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java keyword\">class<\/code> <code class=\"java plain\">Node<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java plain\">{<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java keyword\">int<\/code> <code class=\"java plain\">key;<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java plain\">Node left, right;<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java keyword\">public<\/code> <code class=\"java plain\">Node(<\/code><code class=\"java keyword\">int<\/code> <code class=\"java plain\">item)<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java plain\">{<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java plain\">key = item;<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java plain\">left = right = <\/code><code class=\"java keyword\">null<\/code><code class=\"java plain\">;<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"java spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[ad type=&#8221;banner&#8221;]\n<p><strong>First Simple Tree in C<\/strong><br \/>\nLet us create a simple tree with 4 nodes in C. The created tree would be as following.<\/p>\n<pre>      tree\r\n      ----\r\n       1    &lt;-- root\r\n     \/   \\\r\n    2     3  \r\n   \/   \r\n  4\r\n<strong>Java Program:\r\n<\/strong><\/pre>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/* Class containing left and right child of current<br\/>   node and key value*\/<br\/>class Node<br\/>{<br\/>    int key;<br\/>    Node left, right;<br\/> <br\/>    public Node(int item)<br\/>    {<br\/>        key = item;<br\/>        left = right = null;<br\/>    }<br\/>}<br\/> <br\/>\/\/ A Java program to introduce Binary Tree<br\/>class BinaryTree<br\/>{<br\/>    \/\/ Root of Binary Tree<br\/>    Node root;<br\/> <br\/>    \/\/ Constructors<br\/>    BinaryTree(int key)<br\/>    {<br\/>        root = new Node(key);<br\/>    }<br\/> <br\/>    BinaryTree()<br\/>    {<br\/>        root = null;<br\/>    }<br\/> <br\/>    public static void main(String[] args)<br\/>    {<br\/>        BinaryTree tree = new BinaryTree();<br\/> <br\/>        \/*create root*\/<br\/>        tree.root = new Node(1);<br\/> <br\/>        \/* following is the tree after above statement<br\/> <br\/>              1<br\/>            \/   \\<br\/>          null  null     *\/<br\/> <br\/>        tree.root.left = new Node(2);<br\/>        tree.root.right = new Node(3);<br\/> <br\/>        \/* 2 and 3 become left and right children of 1<br\/>               1<br\/>             \/   \\<br\/>            2      3<br\/>          \/    \\    \/  \\<br\/>        null null null null  *\/<br\/> <br\/> <br\/>        tree.root.left.left = new Node(4);<br\/>        \/* 4 becomes left child of 2<br\/>                    1<br\/>                \/       \\<br\/>               2          3<br\/>             \/   \\       \/  \\<br\/>            4    null  null  null<br\/>           \/   \\<br\/>          null null<br\/>         *\/<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Binary Tree (Introduction) &#8211; A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80139,1,2139],"tags":[72683,80274,80278,80276,80275,80280,80277,83754,83752,83751,83757,83755,83758,83753,83756,80279],"class_list":["post-26903","post","type-post","status-publish","format-standard","hentry","category-binary-tree-divide-and-conquer","category-coding","category-java","tag-binary-tree-c","tag-binary-tree-implementation-in-java","tag-binary-tree-java-api","tag-binary-tree-java-code","tag-binary-tree-java-example","tag-create-binary-search-tree-java","tag-how-to-implement-a-simple-binary-search-tree-in-java","tag-java-programming-books","tag-java-programming-examples-pdf","tag-java-programming-for-beginners","tag-java-programming-pdf","tag-java-programming-software","tag-java-programming-tutorial","tag-java-programming-w3schools","tag-java-programming-youtube","tag-write-a-program-to-create-a-binary-tree-in-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26903"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26903\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}