{"id":26926,"date":"2017-12-26T22:03:48","date_gmt":"2017-12-26T16:33:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26926"},"modified":"2017-12-26T22:03:48","modified_gmt":"2017-12-26T16:33:48","slug":"python-program-binary-tree-introduction","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-program-binary-tree-introduction\/","title":{"rendered":"Python Program &#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    <-- root\r\n     \/   \\\r\n    f      k  \r\n  \/   \\      \\\r\n a     h      z    <-- 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     \/    <-- 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=\u201dbanner\u201d]\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<div class=\"responsive-tabs-wrapper\">\n<div class=\"responsive-tabs responsive-tabs--enabled\">\n<div class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\u00a0[ad type=\u201dbanner\u201d]<\/div>\n<div id=\"tablist1-panel2\" class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">Python Program:<\/div>\n<div class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\"><\/div>\n<div class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\n<div class=\"line number1 index0 alt2\"><code class=\"python comments\"># A Python class that represents an individual node <\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"python comments\"># in a Binary Tree<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"python keyword\">class<\/code> <code class=\"python plain\">Node:<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python keyword\">def<\/code> <code class=\"python plain\">__init__(<\/code><code class=\"python color1\">self<\/code><code class=\"python plain\">,key):<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python color1\">self<\/code><code class=\"python plain\">.left <\/code><code class=\"python keyword\">=<\/code> <code class=\"python color1\">None<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python color1\">self<\/code><code class=\"python plain\">.right <\/code><code class=\"python keyword\">=<\/code> <code class=\"python color1\">None<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"python spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"python color1\">self<\/code><code class=\"python plain\">.val <\/code><code class=\"python keyword\">=<\/code> <code class=\"python plain\">key<\/code><\/div>\n<div class=\"line number7 index6 alt2\">\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    <-- root\r\n     \/   \\\r\n    2     3  \r\n   \/   \r\n  4<\/pre>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20introduce%20Binary%20Tree%0A%20%0A%23%20A%20class%20that%20represents%20an%20individual%20node%20in%20a%0A%23%20Binary%20Tree%0Aclass%20Node%3A%0A%20%20%20%20def%20__init__(self%2Ckey)%3A%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%20%20%20%20%20%20%20self.val%20%3D%20key%0A%20%0A%20%0A%23%20create%20root%0Aroot%20%3D%20Node(1)%0A\u201d\u2019%20following%20is%20the%20tree%20after%20above%20statement%0A%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20None%20%20None\u201d\u2019%0A%20%0Aroot.left%20%20%20%20%20%20%3D%20Node(2)%3B%0Aroot.right%20%20%20%20%20%3D%20Node(3)%3B%0A%20%20%20%0A\u201d\u2019%202%20and%203%20become%20left%20and%20right%20children%20of%201%0A%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20%20%20%202%20%20%20%20%20%203%0A%20%20%20%20%20%2F%20%20%20%20%5C%20%20%20%20%2F%20%20%5C%0A%20%20%20None%20None%20None%20None\u201d\u2019%0A%20%0A%20%0Aroot.left.left%20%20%3D%20Node(4)%3B%0A\u201d\u20194%20becomes%20left%20child%20of%202%0A%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%2F%20%20%20%20%20%20%20%5C%0A%20%20%20%20%20%202%20%20%20%20%20%20%20%20%20%203%0A%20%20%20%20%2F%20%20%20%5C%20%20%20%20%20%20%20%2F%20%20%5C%0A%20%20%204%20%20%20%20None%20%20None%20%20None%0A%20%20%2F%20%20%5C%0ANone%20None\u201d'\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python Program &#8211; Binary Tree (Introduction) &#8211; A tree whose elements have at most 2 children is called a binary tree. Let us create a simple tree with 4 node<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80139,4148],"tags":[70478,70289,80310,70263,80276,70489,70929,83762,83766,83764,83760,83759,83763,83765,80309,83761],"class_list":["post-26926","post","type-post","status-publish","format-standard","hentry","category-binary-tree-divide-and-conquer","category-python","tag-avl-tree","tag-balanced-binary-tree","tag-balanced-tree","tag-binary-search-tree","tag-binary-tree-java-code","tag-complete-binary-tree","tag-heap-data-structure","tag-python-code","tag-python-codecademy","tag-python-programming-examples-advanced","tag-python-programming-pdf","tag-python-programming-tutorial","tag-python-syntax","tag-python-tutorial-examples","tag-red-black-tree-java","tag-what-is-python-used-for"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26926","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=26926"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26926\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}