Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced B Tree in Data Structure Convert Sorted List to Binary Search Tree Flattening a Linked List Given a Perfect Binary Tree, Reverse Alternate Levels Left View of Binary Tree What are Forest Trees in Data Structure Compare Balanced Binary Tree and Complete Binary Tree Diameter of a Binary Tree Given a Binary Tree Check the Zig Zag Traversal Given a Binary Tree Print the Shortest Path Given a Binary Tree Return All Root To Leaf Paths Given a Binary Tree Swap Nodes at K Height Given a Binary Tree Find Its Minimum Depth Given a Binary Tree Print the Pre Order Traversal in Recursive Given a Generate all Structurally Unique Binary Search Trees Perfect Binary Tree Threaded Binary Trees Function to Create a Copy of Binary Search Tree Function to Delete a Leaf Node from a Binary Tree Function to Insert a Node in a Binary Search Tree Given Two Binary Trees, Check if it is Symmetric A Full Binary Tree with n Nodes Applications of Different Linked Lists in Data Structure B+ Tree in Data Structure Construction of B tree in Data Structure Difference between B-tree and Binary Tree Finding Rank in a Binary Search Tree Finding the Maximum Element in a Binary Tree Finding the Minimum and Maximum Value of a Binary Tree Finding the Sum of All Paths in a Binary Tree Time Complexity of Selection Sort in Data Structure How to get Better in Data Structures and Algorithms Binary Tree Leaf Nodes Classification of Data Structure Difference between Static and Dynamic Data Structure Find the Union and Intersection of the Binary Search Tree Find the Vertical Next in a Binary Tree Finding a Deadlock in a Binary Search Tree Finding all Node of k Distance in a Binary Tree Finding Diagonal Sum in a Binary Tree Finding Diagonal Traversal of The Binary Tree Finding In-Order Successor Binary Tree Finding the gcd of Each Sibling of the Binary Tree Greedy Algorithm in Data Structure How to Calculate Space Complexity in Data Structure How to find missing numbers in an Array Kth Ancestor Node of Binary Tree Minimum Depth Binary Tree Mirror Binary Tree in Data Structure Red-Black Tree Insertion Binary Tree to Mirror Image in Data Structure Calculating the Height of a Binary Search Tree in Data Structure Characteristics of Binary Tree in Data Structure Create a Complete Binary Tree from its Linked List Field in Tree Data Structure Find a Specified Element in a binary Search Tree Find Descendant in Tree Data Structure Find Siblings in a Binary Tree Given as an Array Find the Height of a Node in a Binary Tree Find the Second-Largest Element in a Binary Tree Find the Successor Predecessor of a Binary Search Tree Forest of a Tree in Data Structure In Order Traversal of Threaded Binary Tree Introduction to Huffman Coding Limitations of a Binary Search Tree Link State Routing Algorithm in Data Structure Map Reduce Algorithm for Binary Search Tree in Data Structure Non-Binary Tree in Data Structure Quadratic Probing Example in Hashing Scope and Lifetime of Variables in Data Structure Separate Chaining in Data Structure What is Dynamic Data Structure Separate Chaining vs Open Addressing Time and Space Complexity of Linear Data Structures Abstract Data Types in Data Structures Binary Tree to Single Linked List Count the Number of Nodes in the Binary Tree Count Total No. of Ancestors in a Binary Search Tree Elements of Dynamic Programming in Data Structures Find cost of tree with prims algorithm in data structures Find Preorder Successor in a Threaded Binary Tree Find Prime Nodes Sum Count in Non-Binary Tree Find the Right Sibling of a Binary Tree with Parent Pointers Find the Width of the Binary Search Tree Forest trees in Data Structures Free Tree in Data Structures Frequently asked questions in Tree Data Structures Infix, Postfix and Prefix Conversion Time Complexity of Fibonacci Series What is Weighted Graph in Data Structure What is the Advantage of Linear Search?

Infix, Postfix and Prefix Conversion

What is Infix Notation

Infix notation is a method of writing mathematical expressions that are commonly used in everyday calculations, as well as in computer programming languages. In this notation, operators such as addition (+), subtraction (-), multiplication (*), and division (/) are written between the operands or the numbers on which the operations are performed.

For example, the expression 2 + 3 is written in infix notation as "2 plus 3", with the plus symbol between the two operands. Similarly, the expression 4 *5 is written as "4 times 5", with the multiplication symbol placed between the two operands.

Infix, Postfix, and Prefix Conversion

Infix notation is widely used because it is intuitive and easy to read and understand for most people. It follows the same order of operations as traditional arithmetic, meaning multiplication and division are performed before addition and subtraction. This order can be overridden using parentheses to group operations, just as in traditional arithmetic.

For example, the expression 2 + 3 * 4 can be ambiguous if we don't use parentheses toindicate which operation should be performed first. In this case, it could meaneither "2 plus 3, then multiplied by 4" or "3 multiplied by 4, then added to 2".  So, to remove ambiguity, we can write the expression as (2 + 3) * 4 or 2 + (3 * 4),  depending on the desired order of operations.

In addition to the standard arithmetic operators, infix notation can also include other mathematical and logical symbols, such as exponents (^) and square roots (v), and logical operators, such as AND (&&) and OR (||). These symbols are placed between the operands, just like the arithmetic operators.

Infix notation is used in many programming languages, including popular ones like

C++, Java, and Python. However, in programming languages, infix notation may be limited to a smaller set of operators and symbols, depending on the language syntax and rules.

Infix notation can be converted to other notations, such as postfix (also known as reverse Polish notation) and prefix (also known as Polish notation), which are used in some programming languages and calculators. These notations have advantages in certain contexts, such as easier parsing by computers or more efficient use of memory in calculators.

In conclusion, infix notation is a widely used and intuitive method of writing mathematical expressions that include operators placed between the operands.

It follows the same order of operations as traditional arithmetic and can include a variety of mathematical and logical symbols. Infix notation is used in many programming languages and can be converted to other notations for specific purposes.

What is Postfix notation

Postfix notation, also known as reverse Polish notation (RPN), is a mathematical expression method that places operators after the operands. In this notation, the expressions are written in a way that makes it easy for computers to evaluate them. Postfix notation was developed by the Polish mathematician Jan Lukasiewicz in the 1920s and has since been used in many programming languages and calculators.

In postfix spaces separate notation, operands, and operators, and the order of operations is determined by the position of the operators. For example, the expression 2 + 3 is written as "2 3 +" in postfix notation. In this expression, the operator "+" follows the two operands "2" and "3". Similarly, the expression 4 * 5 is written as "4 5 ", with the operator " following the two operands.

The advantage of postfix notation is that it eliminates the need for parentheses and the order of operations becomes unambiguous. The expressions can be evaluated by a simple stack-based algorithm, which makes it easy to implement in programming languages and calculators.

To evaluate an expression in postfix notation, a stack data structure is used to keep track of the operands and operators. The operands are pushed onto the stack, and when an operator is encountered, the top two operands are popped from the stack, the operator is applied to them, and the result is pushed back onto the stack.

For example, to evaluate the postfix expression "2 3 + 4 ", we would start by pushing the operands 2 and 3 onto the stack. When we encounter the "+" operator, we pop 3 and 2 from the stack, add them together, and push the result, 5, back onto the stack. We then push the operand Four onto the stack. When we encounter the " operator, we pop 4 and 5 from the stack, multiply them together, and push the result, 20, back onto the stack. The final result, 20, is the only value left on the stack.

Postfix notation can also include other operators and symbols, such as exponents (^) and square roots (v), and logical operators, such as AND (&&) and OR (||).

These symbols are placed after the operands, just like the arithmetic operators.

Postfix notation can be converted to infix notation or prefix notation, which can be useful for displaying expressions in a more human-readable format. However, the the conversion process can be complex, especially for expressions that include parentheses or nested operators.

Postfix notation is used in many programming languages, including Forth, Factor, and the HP calculators. It is also used in some computer architectures, where it can be used to simplify the design of hardware circuits for arithmetic and logical operations.

What is Prefix notation

Prefix notation, also known as Polish notation, is a method of writing mathematical expressions in which operators are placed before their operands. This notation was invented by the Polish mathematician Jan Lukasiewicz in the 1920s and has since been used in many programming languages and calculators.

In prefix notation, the expressions are written in a way that makes it easy for computers to evaluate them. Spaces separate operators and operands, and the position of the operators determines the order of operations. For example, the expression 2 + 3 is written in prefix notation as "+ 2 3". In this expression, the operator "+" comes before the two operands

"2" and "3". Similarly, the expression 4 * 5 is written as "* 4 5", with the operator "*" preceding the two operands.

The advantage of prefix notation is that it eliminates the need for parentheses and the order of operations becomes unambiguous. The expressions can be evaluated by a simple stack-based algorithm, which makes it easy to implement in programming languages and calculators.

To evaluate an expression in prefix notation, a stack data structure is used to keep track of the operands and operators. The operators are pushed onto the stack, and when an operand is encountered, it is pushed onto the stack. When there are enough operands on the stack to satisfy an operator, the top operands are popped off the stack, the operator is applied to them, and the result is pushed back onto the stack.

For example, we would start by pushing the operator to evaluate the prefix expression "* + 2 3 4"." onto the stack. When we encounter the "+" operator, we push it onto the stack, followed by operands 2 and 3. The next operator we encounter is * "so we push it onto the stack, followed by the operand 4. We now have enough operands on the stack to satisfy the "*." operator, so we pop four and the result of "+" (which is 5) from the stack, multiply them together and push the result, 20, back onto the stack. The final result, 20, is the only value left on the stack.

Prefix notation can also include other operators and symbols, such as exponents (^) and square roots (v), and logical operators, such as AND (&&) and OR (||).

These symbols are placed before the operands, just like the arithmetic operators.

Prefix notation can be converted to infix notation or postfix notation, which can be useful for displaying expressions in a more human-readable format. However, the conversion process can be complex, especially for expressions that include parentheses or nested operators.

Prefix notation is used in many programming languages, including Lisp, Scheme, and the early versions of the HP calculators. It is also used in some computer architectures, where it can be used to simplify the design of hardware circuits for arithmetic and logical operations.

In conclusion, prefix notation is a method of writing mathematical expressions in which operators are placed before their operands. It eliminates the need for parentheses and makes the order of operations unambiguous. Prefix notation can be evaluated by a simple stack-based algorithm and is used in many programming languages and calculators. It can also include a variety of mathematical and logical symbols and can be converted to infix or postfix notation for display purposes.

What is Infix to Postfix conversion?

Infix notation is a common way of writing mathematical expressions where the operators are placed between the operands, such as 2 + 3 or 4 * 5. However, postfix notation (also known as reverse Polish notation) is another way of writing expressions where the operators come after the operands, such as 2 3 + or 4 5 *. Converting infix notation to postfix notation can be useful in certain contexts, such as when implementing calculators or compilers.

  • The conversion process involves scanning the infix expression from left to right and using a stack to keep track of the operators.
  • When an operator is encountered, it is placed onto the stack.
  • However, before placing the operator onto the stack, any operators of higher or equal precedence already on the stack must be popped off and appended to the postfix expression. This ensures that operators of higher precedence are evaluated first.
  • When an opening parenthesis is encountered, it is placed onto the stack.
  • When a closing parenthesis is encountered, all operators on the stack are popped off and appended to the postfix expression until the opening parenthesis is reached, which is then popped off and discarded.

Steps for Infix to Postfix Conversion:

For example, Take the infix expression "3 + 4 * 2 / (1 - 5 ) ^ 2 ^ 3" as an example. To convert this expression to postfix notation, we follow these steps:

1. Create an empty stack and an empty postfix expression.

2. Scan the infix expression from left to right.

3. If the current character is an operand (in this case, a number), append it to the postfix expression.

4. If the current character is an operator (+, -, *, /, ^), then:

a. While there are operators of higher or equal precedence on the stack, pop them off and append them to the postfix expression.

b. Push the current operator onto the stack.

5. If the current character is an opening parenthesis, push it onto the stack.

6. If the current character is a closing parenthesis, pop operators off the stack and append them to the postfix expression until the opening parenthesis is reached, which is then popped off and discarded.

7. After scanning the entire expression, pop any remaining operators off the stack and append them to the postfix expression.

Using the above steps, we can convert the infix expression "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3" to postfix notation as follows:

1. Empty stack:

2. Process "3": Postfix expression: 3

3. Process "+": Stack: + Postfix expression: 3

4. Process "4": Postfix expression: 3 4

5. Process "*": Stack: * Postfix expression: 3 4

6. Process "2": Postfix expression: 3 4 2

7. Process "/": Stack: / Postfix expression: 3 4 2 *

8. Process "(": Stack: ( / Postfix expression: 3 4 2 *

9. Process "1": Postfix expression: 3 4 2 * 1

10. Process "-": Stack: - ( / Postfix expression: 3 4 2 * 1

11. Process "5": Postfix expression: 3 4 2 * 1 5

12. Process ")": Stack: / Postfix expression: 3 4 2 * 1 5 - ^

13. Process "^": Stack: ^ / Postfix expression: 3 4 2 * 1 5 - ^ ^

14. Process "2": Postfix expression: 3 4 2 * 1 5 - ^ ^ 2

15. Process "^": Stack: ^ ^ / Postfix expression: 3 4 2 * 1 5 - ^ ^ 2 ^

16. Final postfix expression: 3 4 2 * 1 5 - ^ ^ 2 ^ +

Therefore, the infix expression "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3" can be converted to postfix notation as "3 4 2 * 1 5 - ^ ^ 2 ^ +".

What is Infix to Prefix Conversion?

Infix-to-prefix conversion involves converting an infix expression to its prefix notation. This can be done using steps similar to the infix to postfix conversion, but with a few modifications. The steps are as follows:

1. Reverse the infix expression

2. Use the same algorithm as an infix to postfix conversion to obtain the postfix expression of the reversed infix expression

3. Reverse the postfix expression to obtain the prefix expression

Let's take the same example infix expression we used before: "3 + 4 * 2 / (1 - 5 ) ^ 2 ^ 3".

  • Reverse the infix expression: "3 ^ 2 ^ (5 - 1) / 2 * 4 + 3"
  • Use the infix to postfix conversion algorithm to obtain the postfix expression of the reversed infix expression: "3 4 2 * 1 5 - ^ ^ 2 / +"
  • Reverse the postfix expression to obtain the prefix expression: "+ / ^ ^ 2 - 5 1 * 2 4 3."

Therefore, the infix expression "3 + 4 * 2 / (1 - 5) ^ 2 ^ 3" can be converted to prefix notation as "+ / ^ ^ 2 - 5 1 * 2 4 3".

What is Postfix to Infix conversion?

Postfix to infix conversion converts an expression written in postfix notation to its equivalent infix notation. The operator is written after its operands in postfix notation, also known as Reverse Polish notation. On the other hand, infix notation is the conventional notation where the operator is written between the operands.

Here is an example of a postfix notation expression:

6 3 2 - *

To convert this expression to infix notation, we follow these steps:

Step 1: Start from the left and scan each element of the expression.

Step 2: If the current element is an operand, push it onto a stack.

Step 3: If the current element is an operator, pop two operands from the stack, place them on either side of the operator in infix notation, and enclose the entire expression in parentheses. Then push the resulting expression back onto the stack.

Applying these steps to our example:

  1. We start scanning the expression from the left. The first element is 6, an operand, so we push it onto the stack.

Stack: 6

  • The next element is 3, also an operand, so we push it onto the stack.

Stack: 6 3

  • The next element is 2, also an operand, so we push it onto the stack.

Stack: 6 3 2

  • The next element is -, which is an operator. In that order, we pop the last two operands from the stack, which are 3 and 2. We place them on either side of the operator in infix notation, enclosed in parentheses. We get (3 - 2). We then push this resulting expression back onto the stack.

Stack: 6 (3 - 2)

  • The next element is *, which is an operator. In that order, we pop the last two operands from the stack, which are 6 and (3 - 2). We place them on either side of the operator in infix notation, enclosed in parentheses. We get 6 * (3 - 2). We then push this resulting expression back onto the stack.

Stack: (6 * (3 - 2))

  • We have reached the end of the expression, and the final result is the expression on the top of the stack, which is (6 * (3 - 2)). This is the equivalent infix notation of the original postfix expression 6 3 2 - *.

What is Prefix-to-Infix conversion?

Prefix-to-infix conversion converts an expression written in prefix notation to its equivalent infix notation. The operator is written before its operands in prefix notation, also known as Polish notation. On the other hand, infix notation is the conventional notation where the operator is written between the operands.

Here is an example of a prefix notation expression:

* - 6 3 2

To convert this expression to infix notation, we follow below steps:

Step 1: Start from the right and scan each element of the expression.

Step 2: If the current element is an operand, push it onto a stack.

Step 3: If the current element is an operator, pop two operands from the stack, place them on either side of the operator in infix notation, and enclose the entire expression in parentheses. Then push the resulting expression back onto the stack.

Applying these steps to our example:

  1. We start scanning the expression from the right. The first element is 2, an operand, so we push it onto the stack.

Stack: 2

  • The next element is 3, also an operand, so we push it onto the stack.

Stack: 2 3

  • The next element is 6, also an operand, so we push it onto the stack.

Stack: 2 3 6

  • The next element is -, which is an operator. In that order, we pop the last two operands from the stack, which are 3 and 6. We place them on either side of the operator in infix notation, enclosed in parentheses. We get (3 - 6). We then push this resulting expression back onto the stack.

Stack: 2 (3 - 6)

  • The next element is *, which is an operator. In that order, we pop the last two operands from the stack, which are 2 and (3 - 6). We place them on either side of the operator in infix notation, enclosed in parentheses. We get 2 * (3 - 6). We then push this resulting expression back onto the stack.

Stack: (2 * (3 - 6))

  • We have reached the end of the expression, and the final result is the expression on the top of the stack, which is (2 * (3 - 6)). This is the equivalent infix notation of the original prefix expression * - 6 3 2.