<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shambuling patil</title>
    <description>The latest articles on DEV Community by Shambuling patil (@patilshambuling).</description>
    <link>https://dev.to/patilshambuling</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1089081%2Ffd5b669d-aca5-4215-989e-8ba829aa74d3.jpg</url>
      <title>DEV Community: Shambuling patil</title>
      <link>https://dev.to/patilshambuling</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patilshambuling"/>
    <language>en</language>
    <item>
      <title>Basics of Tree Data Structure</title>
      <dc:creator>Shambuling patil</dc:creator>
      <pubDate>Thu, 25 May 2023 04:15:28 +0000</pubDate>
      <link>https://dev.to/patilshambuling/basics-of-tree-data-structure-187b</link>
      <guid>https://dev.to/patilshambuling/basics-of-tree-data-structure-187b</guid>
      <description>&lt;h2&gt;
  
  
  Basic's of TREE
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pre-Order iterative : (Root , left , right)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You have the root (print it), check for right node first (add it into the stack) and check for left node (add it into stack). &lt;br&gt;
Pop the stack (print it), continue the loop till stack is empty&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;static ArrayList &amp;lt; Integer &amp;gt; preOrderTrav(Node curr) {&lt;br&gt;
    ArrayList &amp;lt; Integer &amp;gt; preOrder = new ArrayList &amp;lt; Integer &amp;gt; ();&lt;br&gt;
        if (curr == null)&lt;br&gt;
            return preOrder;&lt;br&gt;
        Stack &amp;lt; Node &amp;gt; s = new Stack &amp;lt; &amp;gt; ();&lt;br&gt;
        s.push(curr);&lt;br&gt;
        while (!s.isEmpty()) {&lt;br&gt;
            Node topNode = s.peek();&lt;br&gt;
            preOrder.add(topNode.data);&lt;br&gt;
            s.pop();&lt;br&gt;
            if (topNode.right != null)&lt;br&gt;
                s.push(topNode.right);&lt;br&gt;
            if (topNode.left != null)&lt;br&gt;
                s.push(topNode.left);&lt;br&gt;
        }&lt;br&gt;
        return preOrder;&lt;br&gt;
    }&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In-Order Iterative : (Left , Root , Right)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Iterate all the left nodes of the tree (while adding all the left nodes into Stack) and at the end of left node (print the left node), pop the stack (root of last node) print it and look for right node if present (print it).&lt;br&gt;
 Continue the loop for infinity break if stack is empty.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;static ArrayList &amp;lt; Integer &amp;gt; inOrderTrav(Node curr) {&lt;br&gt;
        ArrayList &amp;lt; Integer &amp;gt; inOrder = new ArrayList &amp;lt; &amp;gt; ();&lt;br&gt;
        Stack &amp;lt; Node &amp;gt; s = new Stack &amp;lt; &amp;gt; ();&lt;br&gt;
        while (true) {&lt;br&gt;
            if (curr != null) {&lt;br&gt;
                s.push(curr);&lt;br&gt;
                curr = curr.left;&lt;br&gt;
            } else {&lt;br&gt;
                if (s.isEmpty()) break;&lt;br&gt;
                curr = s.peek();&lt;br&gt;
                inOrder.add(curr.data);&lt;br&gt;
                s.pop();&lt;br&gt;
                curr = curr.right;&lt;br&gt;
            }&lt;br&gt;
        }&lt;br&gt;
        return inOrder;&lt;br&gt;
    }&lt;/p&gt;

&lt;p&gt;** Post-Order Iterative: (Left , Right , Root)**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1 :using one stack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Print the value if both left and right value is null, and if the tree has only right nodes then check for (temp == st.peek().right) because to skip the duplication of iteration.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;static ArrayList &amp;lt; Integer &amp;gt; postOrderTrav(Node cur) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ArrayList &amp;lt; Integer &amp;gt; postOrder = new ArrayList &amp;lt; &amp;gt; ();
    if (cur == null) return postOrder;

    Stack &amp;lt; Node &amp;gt; st = new Stack &amp;lt; &amp;gt; ();
    while (cur != null || !st.isEmpty()) {

        if (cur != null) {
            st.push(cur);
            cur = cur.left;
        } else {
            Node temp = st.peek().right;
            if (temp == null) {
                temp = st.peek();
                st.pop();
                postOrder.add(temp.data);
                while (!st.isEmpty() &amp;amp;&amp;amp; temp == st.peek().right) {
                    temp = st.peek();
                    st.pop();
                    postOrder.add(temp.data);
                }
            } else cur = temp;
        }
    }
    return postOrder;


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Approach 2: (using 2 stacks)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As Stack2 is used to store the root&lt;br&gt;
Stack1 will store first right nodes first and left nodes&lt;br&gt;
Popping the stack1 and adding the root into stack2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;static ArrayList &amp;lt; Integer &amp;gt; postOrderTrav(Node curr) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ArrayList &amp;lt; Integer &amp;gt; postOrder = new ArrayList &amp;lt; &amp;gt; ();
    if (curr == null) return postOrder;

    Stack &amp;lt; Node &amp;gt; s1 = new Stack &amp;lt; &amp;gt; ();
    Stack &amp;lt; Node &amp;gt; s2 = new Stack &amp;lt; &amp;gt; ();
    s1.push(curr);
    while (!s1.isEmpty()) {
        curr = s1.peek();
        s1.pop();
        s2.push(curr);
        if (curr.left != null)
            s1.push(curr.left);
        if (curr.right != null)
            s1.push(curr.right);
    }
    while (!s2.isEmpty()) {
        postOrder.add(s2.peek().data);
        s2.pop();
    }
    return postOrder;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>dsa</category>
      <category>beginners</category>
      <category>backend</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
