{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/tree","result":{"data":{"blog":{"frontmatter":{"title":"TREE DATA STRUCTURE","thumbnail":"blog38","date":"January 10, 2021","dsaCppCodeFile":null},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is a tree data structure,\n                types, applications and basic operations on tree ADT.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  A tree is a data structure similar to a linked list but\n                  instead of each node pointing to the next node in a linear\n                  fashion, each node points to a number of nodes. Tree is an\n                  example of a non-linear data structure.\n                </p>\n                <p>\n                  A tree structure is a way of representing the hierarchical\n                  nature of a structure in a graphical form. In trees ADT\n                  (Abstract Data Type), the order of the elements is not\n                  important.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 64.1891891891892%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABJ0AAASdAHeZh94AAABZElEQVQ4y22TWZKDMAxEff8L8p0U+xIIEEjYNDxlxDhMVCVsbLnVastOdnu9XlJVpTT3u5Rluc8rGcdRzIZhkPu+t22b/p9H3xyfaZqkaWp5Pp86ByzPc2nbVoIgkOv1qknquj6AfPfB3bcsZrCKokiyLJM0S5V93/cfMWdg94e+yTzP6m8JKum6ToMej4ckSXIkganFnks/GAKSpqk6TJZl0XXGvMgVEGAMlmEYaiz6fi0Z/eI41vLQ0YIUcNezKArpu3e5y7pqHPGWxMp2RpWN2+2m7KwUbN0PA2ZMMKSgbBwJPm6Zw+hFNtOI8mAMCEyaptE9Evr/Fs8aI1iO1gAMp1QYmo4wi+NIAWily+WiyXy2aA9gscfD3KER6JS8/moDAOAEox/MMJLj/q1yhjUwmB9twyJgvn5mSILbzZ97799LMYH9dvEPAGaNDYvz8/Pd2VtFG0DPgXarURRqFd8Y+vMf8Ffzf0BAQU4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Tree DS\" title=\"Tree DS\" src=\"/static/f5855d864999581e213656c538522c0c/fcda8/trees.png\" srcset=\"/static/f5855d864999581e213656c538522c0c/12f09/trees.png 148w,\n/static/f5855d864999581e213656c538522c0c/e4a3f/trees.png 295w,\n/static/f5855d864999581e213656c538522c0c/fcda8/trees.png 590w,\n/static/f5855d864999581e213656c538522c0c/efc66/trees.png 885w,\n/static/f5855d864999581e213656c538522c0c/66465/trees.png 1097w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Terminologies</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Root: </strong>The root of a tree is the node with no\n                  parents. There can be at most one root node in a tree (node A\n                  in the above example).\n                </li>\n                <li>\n                  <strong>Edge: </strong>An edge refers to the link from parent\n                  to child (all links in the figure).\n                </li>\n                <li>\n                  <strong>Leaf Node: </strong>A node with no children (E,J,K,H\n                  and I).\n                </li>\n                <li>\n                  <strong>Siblings: </strong>Children of same parent (B,C,D are\n                  siblings of A, and E,F are the siblings of B).\n                </li>\n                <li>\n                  <strong>Ancestor: </strong>A node P is said to be the ancestor\n                  of node Q if there exists a path from node P to Q. Likewise,\n                  node Q is said to be the <strong>descendant</strong> of node P\n                  (B is an ancestor of J).\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Properties</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Height of a node: </strong>The height of a node is the\n                  length of the path from that node to the deepest node (height\n                  of G is 1, G - K).\n                </li>\n                <li>\n                  <strong>Depth of a node: </strong>The depth of a node is the\n                  length of the path from the root to the node (depth of G is 2,\n                  A &#x2013; C &#x2013; G).\n                </li>\n                <li>\n                  <strong>Height/Depth of a tree: </strong>It is the length of\n                  the path from the root to the deepest node in the tree\n                  (Height/Depth of tree for above example is 4).\n                </li>\n                <li>\n                  In a tree, if there are <strong>N</strong> nodes then there\n                  will be <strong>N-1</strong> edges.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Types</h4>\n              <ol class=\"pl-4\">\n                <li>General Tree</li>\n                <li>Binary Tree</li>\n                <li>AVL Tree</li>\n                <li>Spanning Tree</li>\n                <li>B-/B+ Tree</li>\n                <li>Skew Tree</li>\n                <li>Heap</li>\n                <p class=\"text-muted mt-0\">\n                  <em>\n                    In this series, we shall see about binary trees and heap\n                    only.\n                  </em>\n                </p>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  Store hierarchical data like folder structure, organization\n                  structure data.\n                </li>\n                <li>\n                  Heap is a tree data structure which is implemented using\n                  arrays and used to implement priority queues.\n                </li>\n                <li>\n                  B- Tree and B+ Tree are used to implement indexing in\n                  databases.\n                </li>\n                <li>Used to store router-tables in routers.</li>\n                <li>\n                  Used to implement expression parsers and expression solvers.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Basic Operations</h4>\n              <ol class=\"pl-4\">\n                <li>Inserting an element into a tree.</li>\n                <li>Deleting an element from a tree.</li>\n                <li>Searching for an element.</li>\n                <li>Traversing the tree(BFS/DFS).</li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binarySearchTree\">Binary Search Trees</a> |\n              <a href=\"/blogs/binaryHeap\">Binary Heap</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is a tree data structure,\n                types, applications and basic operations on tree ADT.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  A tree is a data structure similar to a linked list but\n                  instead of each node pointing to the next node in a linear\n                  fashion, each node points to a number of nodes. Tree is an\n                  example of a non-linear data structure.\n                </p>\n                <p>\n                  A tree structure is a way of representing the hierarchical\n                  nature of a structure in a graphical form. In trees ADT\n                  (Abstract Data Type), the order of the elements is not\n                  important.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 64.1891891891892%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABJ0AAASdAHeZh94AAABZElEQVQ4y22TWZKDMAxEff8L8p0U+xIIEEjYNDxlxDhMVCVsbLnVastOdnu9XlJVpTT3u5Rluc8rGcdRzIZhkPu+t22b/p9H3xyfaZqkaWp5Pp86ByzPc2nbVoIgkOv1qknquj6AfPfB3bcsZrCKokiyLJM0S5V93/cfMWdg94e+yTzP6m8JKum6ToMej4ckSXIkganFnks/GAKSpqk6TJZl0XXGvMgVEGAMlmEYaiz6fi0Z/eI41vLQ0YIUcNezKArpu3e5y7pqHPGWxMp2RpWN2+2m7KwUbN0PA2ZMMKSgbBwJPm6Zw+hFNtOI8mAMCEyaptE9Evr/Fs8aI1iO1gAMp1QYmo4wi+NIAWily+WiyXy2aA9gscfD3KER6JS8/moDAOAEox/MMJLj/q1yhjUwmB9twyJgvn5mSILbzZ97799LMYH9dvEPAGaNDYvz8/Pd2VtFG0DPgXarURRqFd8Y+vMf8Ffzf0BAQU4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Tree DS\" title=\"Tree DS\" src=\"/static/f5855d864999581e213656c538522c0c/fcda8/trees.png\" srcset=\"/static/f5855d864999581e213656c538522c0c/12f09/trees.png 148w,\n/static/f5855d864999581e213656c538522c0c/e4a3f/trees.png 295w,\n/static/f5855d864999581e213656c538522c0c/fcda8/trees.png 590w,\n/static/f5855d864999581e213656c538522c0c/efc66/trees.png 885w,\n/static/f5855d864999581e213656c538522c0c/66465/trees.png 1097w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Terminologies</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Root: </strong>The root of a tree is the node with no\n                  parents. There can be at most one root node in a tree (node A\n                  in the above example).\n                </li>\n                <li>\n                  <strong>Edge: </strong>An edge refers to the link from parent\n                  to child (all links in the figure).\n                </li>\n                <li>\n                  <strong>Leaf Node: </strong>A node with no children (E,J,K,H\n                  and I).\n                </li>\n                <li>\n                  <strong>Siblings: </strong>Children of same parent (B,C,D are\n                  siblings of A, and E,F are the siblings of B).\n                </li>\n                <li>\n                  <strong>Ancestor: </strong>A node P is said to be the ancestor\n                  of node Q if there exists a path from node P to Q. Likewise,\n                  node Q is said to be the <strong>descendant</strong> of node P\n                  (B is an ancestor of J).\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Properties</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Height of a node: </strong>The height of a node is the\n                  length of the path from that node to the deepest node (height\n                  of G is 1, G - K).\n                </li>\n                <li>\n                  <strong>Depth of a node: </strong>The depth of a node is the\n                  length of the path from the root to the node (depth of G is 2,\n                  A &#x2013; C &#x2013; G).\n                </li>\n                <li>\n                  <strong>Height/Depth of a tree: </strong>It is the length of\n                  the path from the root to the deepest node in the tree\n                  (Height/Depth of tree for above example is 4).\n                </li>\n                <li>\n                  In a tree, if there are <strong>N</strong> nodes then there\n                  will be <strong>N-1</strong> edges.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Types</h4>\n              <ol class=\"pl-4\">\n                <li>General Tree</li>\n                <li>Binary Tree</li>\n                <li>AVL Tree</li>\n                <li>Spanning Tree</li>\n                <li>B-/B+ Tree</li>\n                <li>Skew Tree</li>\n                <li>Heap</li>\n                <p class=\"text-muted mt-0\">\n                  <em>\n                    In this series, we shall see about binary trees and heap\n                    only.\n                  </em>\n                </p>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  Store hierarchical data like folder structure, organization\n                  structure data.\n                </li>\n                <li>\n                  Heap is a tree data structure which is implemented using\n                  arrays and used to implement priority queues.\n                </li>\n                <li>\n                  B- Tree and B+ Tree are used to implement indexing in\n                  databases.\n                </li>\n                <li>Used to store router-tables in routers.</li>\n                <li>\n                  Used to implement expression parsers and expression solvers.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Basic Operations</h4>\n              <ol class=\"pl-4\">\n                <li>Inserting an element into a tree.</li>\n                <li>Deleting an element from a tree.</li>\n                <li>Searching for an element.</li>\n                <li>Traversing the tree(BFS/DFS).</li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binarySearchTree\">Binary Search Trees</a> |\n              <a href=\"/blogs/binaryHeap\">Binary Heap</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M27%2091c-5%200-5%200-5%203%200%202%201%202%203%202s2%200%202%207c0%206%200%207%202%207s3-4%203-12c0-2%200-3%202-3l2-2c0-2-1-3-2-3l-7%201m15%200c-4%200-5%202-5%2013%200%205%200%206%202%206s2-1%202-3c0-4%202-4%204%200%201%203%201%203%203%202s2-1%201-4v-4c3-8%201-11-7-10m15%200h-5v16c-1%203%205%204%2010%203%204-1%203-5-2-5-4%200-4-2%200-3%204%200%204-4%200-4s-4-2%201-3c3%200%204-1%204-3l-2-2-6%201m14%200l-4%201-1%209v9h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2-1-3%204%200%204-4%200-4s-4-2%201-3c3%200%204-1%204-3s0-2-8-1m197%2011c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203s3-1%201-2l-1-3h4c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204l-23%201-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7M24%20124l-1%2010v9l3-1c5%200%207-2%208-5%202-4%202-9%200-12-1-2-7-3-10-1m18%202l-4%209-2%207h3l3-1c1-2%205-3%205-1s4%203%205%202l-3-17c-1-3-6-3-7%201m11%200c0%203%200%203%202%203%202-1%202-1%202%206s0%207%203%207c2%200%202%200%202-7%200-6%200-7%203-7l2-3c0-2-1-2-7-2h-7v3m20%200c-2%202-6%2015-6%2016h3l3-1c0-3%204-2%204%200%201%202%204%202%205%201s-2-16-3-18c-2-2-5-1-6%202m21%200c-3%203-3%205%202%209l4%203h-4c-3%200-3%200-3%202%200%203%208%203%2010%200s1-6-2-8l-3-4h1c1%202%205%203%205%201l3-1c2%200%202%200%202%207s0%207%202%207%203-1%203-2c0-11%200-12%202-12l2-3c0-2-1-2-6-2-6%200-7%200-7%202h-2c-3-3-7-2-9%201m27-2l-2%209v9h2c2%200%203-1%203-3l1-3%201%203c2%203%203%203%205%202v-3c-1-3-1-4%201-6v-6c-2-3-8-4-11-2m14%201v15c1%203%208%203%2010%200%203-4%203-17%200-17-2%200-3%203-3%2010%200%204-1%206-3%205v-8c0-6%200-7-2-7l-2%202m16%200c-3%204-4%2011-2%2014%203%204%208%204%2011%200%202-3%200-5-3-2s-5-1-3-7c1-3%204-4%203-1-1%202%200%202%202%202l3-1c0-1%201-2%203-2s2%200%202%207%200%207%203%207l2-1v-7c0-5%200-6%202-6l2-3c0-2-1-2-6-2-7%200-7%200-7%202v2l-2-2c-3-3-7-3-10%200m27%201v15l5%201c5%200%207-3%207-11%200-7%200-8-2-8-1%200-2%201-3%207-1%209-2%209-3%200%200-6%200-7-2-7-1%200-2%201-2%203m15-2l-2%209v9h2c2%200%203-1%203-3l1-3%201%203c2%203%203%203%205%202v-3c-1-2-1-4%201-6%201-3%201-5-1-7-1-2-8-3-10-1m14%201l-1%2010v7h7c6%200%206%200%206-2%200-3%200-3-4-3-5%200-5-2-1-2%204-1%204-5%200-5l-3-1%204-1c3%200%204%200%204-2%201-3%200-3-6-3-5%200-6%200-6%202m63%2016l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%2010-1%207-7v-5l-1-3-3-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/2d996fd6506d5b720b4489fbed18214f/ee604/blog38.png","srcSet":"/static/2d996fd6506d5b720b4489fbed18214f/69585/blog38.png 200w,\n/static/2d996fd6506d5b720b4489fbed18214f/497c6/blog38.png 400w,\n/static/2d996fd6506d5b720b4489fbed18214f/ee604/blog38.png 800w,\n/static/2d996fd6506d5b720b4489fbed18214f/f3583/blog38.png 1200w,\n/static/2d996fd6506d5b720b4489fbed18214f/e4d72/blog38.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"tree","thumbnail":"thumbnails/blog38.png"}},"staticQueryHashes":["2987289216","63159454"]}