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 Either Empty or Consists of A Node Called The Root Together With Two Binary Trees Called The Left Subtree and The Right Subtree
) 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
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
Algorithm