Unit-03
Unit-03
Inorder --- B A C
Preorder --- A B C
Postorder --- B C A
Inorder Traversal
Steps :
Traverse left subtree in inorder
Process root node
Traverse right subtree in inorder
Algorithm
Algorithm inorder traversal (BinTree T)
Begin
If ( not empty (T) ) then
Begin
Inorder_traversal ( left subtree ( T ) )
Print ( info ( T ) ) / * process node */
Inorder_traversal ( right subtree ( T ) )
End
End
Preorder Traversal
Steps :
Process root node
Traverse left subtree in preorder
Traverse right subtree in preorder
Algorithm
Algorithm inorder traversal (BinTree T)
Begin
If ( not empty (T) ) then
Begin
Print ( info ( T ) ) / * process node */
Preorder_traversal ( left subtree ( T ) )
Preorder_traversal ( right subtree ( T ) )
End
End
Postorder Traversal
Steps :
Traverse left subtree in postorder
Traverse right subtree in postorder
process root node
Algorithm
Algorithm postorder traversal (BinTree T)
Begin
If ( not empty (T) ) then
Begin
Postorder_traversal ( left subtree ( T ) )
Postorder_traversal ( right subtree( T))
Print ( Info ( T ) ) / * process node */
End
End
Example: 1
Find the Preorder, Inorder and Postorder traversal of the following Tree
Example: 2
A Binary Tree Has 8 Nodes. The Inorder And Postorder Traversal Of The Tree Are Given
Below. Draw The Tree And Find Preorder.
APPLICATIONS
1. Some applications of preorder traversal are the evaluation of expressions in prefix
notation and the processing of abstract syntax trees by compilers.
2. Binary search trees (a special type of BT) use inorder traversal to print all of their
data in alphanumeric order.
3. A popular application for the use of postorder traversal is the evaluating of
expressions in postfix notation.
Expression Tree
The expression tree is a binary tree in which each internal node corresponds to the
operator and each leaf node corresponds to the operand so for example expression tree for
3 + ((5+9)*2) would be:
Examples: 1
Input: Root node of the below tree
Output: 100
Examples: 2
Input: Root node of the below tree
Output: 110
As an example, suppose the input is:
ab+cde+**
The first two symbols are operands, so we create one-node trees and push pointers to
them onto a stack.*
*For convenience, we will have the stack grow from left to right in the diagrams.
Next, a '+' is read, so two pointers to trees are popped, a new tree is formed, and a pointer to it is
pushed onto the stack.*
Next, c, d, and e are read, and for each a one-node tree is created and a pointer to the
corresponding tree is pushed onto the stack.
Applications of Tree:
File Systems: The file system of a computer is often represented as a tree. Each folder or
directory is a node in the tree, and files are the leaves.
XML Parsing: Trees are used to parse and process XML documents. An XML document
can be thought of as a tree, with elements as nodes and attributes as properties of the nodes.
Database Indexing: Many databases use trees to index their data. The B-tree and its
variations are commonly used for this purpose.
Compiler Design: The syntax of programming languages is often defined using a tree
structure called a parse tree. This is used by compilers to understand the structure of the
code and generate machine code from it.
Artificial Intelligence: Decision trees are often used in artificial intelligence to make
decisions based on a series of criteria.
1 5
7
OPERATIONS
Operations on a binary tree require comparisons between nodes. These comparisons are
made with calls to a comparator, which is a subroutine that computes the total order
(linear order) on any two values. This comparator can be explicitly or implicitly defined,
depending on the language in which the BST is implemented.
The following are the operations that are being done in Binary Tree
Searching.
Sorting.
Deletion.
Insertion.
Find
This operation generally requires returning a pointer to the node in tree T that has
key x, or NULL if there is no such node. The structure of the tree makes this simple. If T
is, then we can just return. Otherwise, if the key stored at T is x, we can return T.
Otherwise, we make a recursive call on a subtree of T, either left or right, depending on
the relationship of x to the key stored in T.
Insert
The insertion routine is conceptually simple. To insert x into tree T, proceed down
the tree as you would with a find. If x is found, do nothing (or "update" something).
Otherwise, insert x at the last spot on the path traversed. Figure below shows what
happens. To insert 5, we traverse the tree as though a find were occurring. At the node
with key 4, we need to go right, but there is no subtree, so 5 is not in the tree, and this is
the correct spot.
Duplicates can be handled by keeping an extra field in the node record indicating
the frequency of occurrence. This adds some extra space to the entire tree, but is better
than putting duplicates in the tree (which tends to make the tree very deep). Of course
this strategy does not work if the key is only part of a larger record. If that is the case,
then we can keep all of the records that have the same key in an auxiliary data structure,
such as a list or another search tree.
EXAMPLE Insert node 5 in given tree
Delete
As is common with many data structures, the hardest operation is deletion. Once we have
found the node to be deleted, we need to consider several possibilities. If the node is a
leaf, it can be deleted immediately. If the node has one child, the node can be deleted
after its parent adjusts a pointer to bypass the node (we will draw the pointer directions
explicitly for clarity).. Notice that the deleted node is now unreferenced and can be
disposed of only if a pointer to it has been saved. The complicated case deals with a node
with two children. The general strategy is to replace the key of this node with the smallest
key of the right subtree (which is easily found) and recursively delete that node (which is
now empty). Because the smallest node in the right subtree cannot have a left child, the
second delete is an easy one. To delete an element, consider the following three
possibilities :
Case 1: Node to be deleted is a leaf node.
Case 2: Node with only one child.
Case 3: Node with two children.