Unit 3-DS
Unit 3-DS
Important Terms
Path − Path refers to the sequence of nodes along the edges of a tree.
Root − The node at the top of the tree is called root. There is only one root
per tree and one path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node
called parent.
Child − The node below a given node connected by its edge downward is
called its child node.
Leaf − The node which does not have any child node is called the leaf node.
Subtree − Subtree represents the descendants of a node.
Visiting − Visiting refers to checking the value of a node when control is on
the node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If the root
node is at level 0, then its next child node is at level 1, its grandchild is at
level 2, and so on.
Keys − Key represents a value of a node based on which a search operation
is to be carried out for a node.
TYPES OF TREES
General Trees
Binary Trees
Binary Search Trees
General Trees
General trees are unordered tree data structures where the root node has minimum
0 or maximum ‘n’ subtrees.
The General trees have no constraint placed on their hierarchy. The root node thus
acts like the superset of all the other subtrees.
Binary Trees
Binary Trees are general trees in which the root node can only hold up to
maximum 2 subtrees: left subtree and right subtree. Based on the number of
children, binary trees are divided into three types.
A full binary tree is a binary tree type where every node has either 0 or 2
child nodes.
Complete Binary Tree
A complete binary tree is a binary tree type where all the leaf nodes must be
on the same level. However, root and internal nodes in a complete binary
tree can either have 0, 1 or 2 child nodes.
A perfect binary tree is a binary tree type where all the leaf nodes are on the
same level and every node except leaf nodes have 2 children.
TREE TRAVERSAL
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a sub
tree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in
an ascending order.
D→B→E→A→F→C→G
Algorithm
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
Post-order 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 pre-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
Binary Tree
A binary tree is a tree data structure in which each parent node can have at most
two children. Each node of a binary tree consists of three items:
data item
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
A complete binary tree is just like a full binary tree, but with two major differences
3. The last leaf element might not have a right sibling i.e. a complete binary
tree doesn't have to be a full binary tree.
A degenerate or pathological tree is the tree having a single child either left or
right.
Degenerate Binary Tree
It is a type of binary tree in which the difference between the height of the left and
the right subtree for each node is either 0 or 1.
Balanced Binary Tree
A node of a binary tree is represented by a structure containing a data part and two
pointers to other structures of the same type.
struct node
int data;
};
The expression tree is a tree used to represent the various expressions. The tree
data structure is used to represent the expressional statements. In this tree, the
internal node always denotes the operators. The leaf nodes always denote the
operands.
Expression trees play a very important role in representing the language-level code
in the form of the data, which is mainly stored in the tree-like structure. It is also
used in the memory representation of the lambda expression. Using the tree data
structure, we can express the lambda expression more transparently and explicitly.
It is first created to convert the code segment onto the data segment so that the
expression can easily be evaluated.
The expression tree is a binary tree in which each external or leaf node corresponds
to the operand and each internal or parent node corresponds to the operators so for
example expression tree for 7 + ((1+8)*3) would be:
Return S.value
x = solve(S.left)
y = solve(S.right)
o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored
on the disc drive, the file and folder are in the form of the naturally
hierarchical data and stored in the form of trees.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast
and efficient way for dynamic spell checking.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in
routing tables in the routers.