0% found this document useful (0 votes)
94 views

A Binary Tree Either Empty or Consists of A Node Called The Root Together With Two Binary Trees Called The Left Subtree and The Right Subtree

A binary tree is a data structure where each node has at most two children. There are three types of binary tree traversals: inorder, preorder, and postorder. Inorder traversal visits the left subtree, then the node, then the right subtree. Preorder visits the node, then the left subtree, then the right subtree. Postorder visits the left subtree, then the right subtree, then the node. A stack operates by last in, first out (LIFO) while a queue operates by first in, first out (FIFO).

Uploaded by

Rafraf Gayatao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

A Binary Tree Either Empty or Consists of A Node Called The Root Together With Two Binary Trees Called The Left Subtree and The Right Subtree

A binary tree is a data structure where each node has at most two children. There are three types of binary tree traversals: inorder, preorder, and postorder. Inorder traversal visits the left subtree, then the node, then the right subtree. Preorder visits the node, then the left subtree, then the right subtree. Postorder visits the left subtree, then the right subtree, then the node. A stack operates by last in, first out (LIFO) while a queue operates by first in, first out (FIFO).

Uploaded by

Rafraf Gayatao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

) Binary Trees
-  A binary tree is a tree data structure in which each node has at most
two children, which are referred to as the left child and the right child, either empty
or consists of a node called the root together with two binary trees called the left
subtree and the right subtree.
2.) Binary Trees Traversal
- is a common operation performed on data structures. It is the process in
which each and every element present in a data structure is "visited" (or accessed)
at least once. This may be done to display all of the elements or to perform an
operation on all of the elements. That is, we cannot randomly access a node in a
tree.
A.) Inorder Traversal
- In this traversal method, the left subtree is visited first, then the root and
later the right sub-tree. we traverse one subtree of a node, then "visit" the node, and
then traverse its other subtree. Usually, we traverse the node's left subtree first and
then traverse the node's right subtree.We should always remember that every node
may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in
an ascending order.
We start from A, and following in-order traversal, we move to its left
subtree B. B is also traversed in-order. The process goes on until all the nodes are
visited. The output of inorder traversal of this tree will be ;
D→B→E→A→F→C→G

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

B.) Pre-Order Traversal


- In this traversal method, the root node is visited first, then the left subtree
and finally the right subtree.

We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order traversal of this tree will be ;
A→B→D→E→C→F→G
Algorithm

Until all nodes are traversed −


Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

C.) Postorder Traversal


- In this traversal method, the root node is visited last, hence the name. First,
we traverse the left subtree, then the right subtree and finally the root node.

We start from A, and


following Post-order traversal, we first visit the left subtree B. B is also traversed
post-order. The process goes on until all the nodes are visited. The output of post-
order traversal of this tree will be ;
D→E→B→F→G→C→A

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
3. Difference of Stack in Queues
- A stack is an ordered list of elements where all insertions and deletions are
made at the same end, whereas a queue is exactly the opposite of a stack which is
open at both the ends meaning one end is used to insert data while the other to
remove data. The stack is known as lifo and queue is known as fifo rule.

You might also like