Tree and Binary Tree Data Structure
Tree and Binary Tree Data Structure
Tree and Binary Tree Data Structure
NON-LINEAR
It is a hierarchical structure that is used to
represent and organize data in a way that is easy to
navigate and search. It is a collection of nodes that are
connected by edges and has a hierarchical relationship
between the nodes.
Tree
Data Structure The topmost node of the tree is called the root,
and the nodes below it are called the child nodes. Each
node can have multiple child nodes, and these child
nodes can also have their own child nodes, forming a
recursive structure.
This data structure is a specialized method to
Tree organize and store data in the computer to be used more
effectively. We can also say that tree data structure has
Data Structure roots, branches, and leaves connected with one another.
Parent Node: The node which is a predecessor of a
node is called the parent node of that node. {B} is the
Basic parent node of {D, E}.
Terminologies in
Tree Data Child Node: The node which is the immediate
Structure successor of a node is called the child node of that node.
Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node
which does not have any parent node is called the root
node. {A} is the root node of the tree. A non-empty tree
Basic must contain exactly one root node and exactly one path
Terminologies in from the root to all other nodes of the tree.
Tree Data
Structure Leaf Node or External Node: The nodes which do not
have any child nodes are called leaf nodes. {K, L, M, N,
O, P} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path
of the root to that node are called Ancestors of that node.
{A,B} are the ancestor nodes of the node {E}
Basic
Terminologies in Descendant: Any successor node on the path from the
Tree Data leaf node to that node. {E,I} are the descendants of the
Structure node {B}.
Uses of Preorder:
Preorder traversal is used to create a copy of the
tree. Preorder traversal is also used to get prefix
expressions on an expression tree.
Postorder(Tree):
Uses of Postorder:
Postorder traversal is used to delete the tree.
For each node, first, the node is visited and then
it’s child nodes are put in a FIFO queue. Then again the
first node is popped out and then it’s child nodes are put
in a FIFO queue and repeat until queue becomes empty.
Level-Order
Traversal
Output:
1
23
45
Boundary Traversal of a Tree includes:
Diagonal
Traversal
Output:
8 10 14
3 6 7 13
14
One reason to use trees might be because you
want to store information that naturally forms a
hierarchy. For example, the file system on a computer:
Disadvantages
Trees demand more memory space requirements than
of some other data structures like arrays and linked lists,
Tree Data especially if the tree is very large.
Structure
The implementation and manipulation of trees can be
complex and require a good understanding of the
algorithms.
BINARY TREE
In a binary tree, each node can have a maximum of two
children linked to it. Some common types of binary trees
Binary Tree include full binary trees, complete binary trees, balanced
binary trees, and degenerate or pathological binary trees.
Full Binary Tree
Full
Binary Tree
A Tree where every internal node has one child. Such
trees are performance-wise same as linked list. A
degenerate or pathological tree is a tree having a single
child either left or right.
Degenerate
Binary Tree
A skewed binary tree is a pathological/degenerate tree in
which the tree is either dominated by the left nodes or the
right nodes. Thus, there are two types of skewed binary tree:
left-skewed binary tree and right-skewed binary tree.
Skewed
Binary Tree
Complete Binary Tree
Types of Binary
Tree
On the basis of the Perfect Binary Tree
completion of
levels
Balanced Binary Tree
Complete Binary Tree if all the levels are completely
filled except possibly the last level and the last level has
all keys as left as possible.
Representation Data
Binary Tree Pointer to the left child
Pointer to the right child
Inserting an element.
Removing an element.
Basic
Operation on Searching for an element.
Binary Tree Deletion for an element.
Traversing an element.
Used to find elements in less time (binary search tree)
Used to enable fast memory allocation in computers.
Used to perform encoding and decoding operations.
Binary trees can be used to implement sorting
Applications of algorithms, such as in heap sort which uses a binary
Binary Tree heap to sort elements efficiently.
Binary trees can be used to represent the decision-
making process of computer-controlled characters in
games, such as in decision trees.
Tree Traversal algorithms can be classified broadly
into two categories:
Traversals of
Binary Tree Depth-First Search (DFS) Algorithms
Breadth-First Search (BFS) Algorithms
Tree is a hierarchical data structure. Main
uses of trees include maintaining hierarchical data,
providing moderate access and insert/delete
operations.
Conclusion
Binary trees are special cases of tree where
every node has at most two children.