0% found this document useful (0 votes)
7 views5 pages

Chapter 5

Chapter 5 discusses tree data structures, which consist of nodes connected by edges and are used to store data hierarchically. It covers basic terminologies, types of trees, properties, and methods for searching and traversing binary trees, including depth-first and breadth-first search techniques. The chapter emphasizes the importance of trees in organizing data efficiently and their various applications.

Uploaded by

lap use
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Chapter 5

Chapter 5 discusses tree data structures, which consist of nodes connected by edges and are used to store data hierarchically. It covers basic terminologies, types of trees, properties, and methods for searching and traversing binary trees, including depth-first and breadth-first search techniques. The chapter emphasizes the importance of trees in organizing data efficiently and their various applications.

Uploaded by

lap use
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 5 (Tree)

Introduction
• A tree data structure is a collection of nodes connected by edges. Each node contains a
value or data which may or may not have a child node. The first node of the tree is called
the root.
• Tree data structure is a specialized data structure to store data in hierarchical manner.
It is used to organize and store data in the computer to be used more effectively. It
consists of a central node, structural nodes, and sub-nodes, which are connected via
edges. We can also say that tree data structure has roots, branches, and leaves connected.

Basic Terminologies in Tree Data Structure


• Parent Node: The node which is a predecessor of a node is called the parent node of
that node. {B} is the parent node of {D, E}.
• Child Node: The node which is the immediate 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 must
contain exactly one root node and exactly one path from the root to all other nodes of
the tree.
• Leaf Node or External Node: The nodes which do not have any child nodes are called
leaf nodes. {I, J, K, F, G, H} 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}
• Descendant: A node x is a descendant of another node y if and only if y is an ancestor
of x.
• Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
• Level of a node: The count of edges on the path from the root node to that node. The
root node has level 0.
• Internal node: A node with at least one child is called Internal Node.
• Neighbor of a Node: Parent or child nodes of that node are called neighbors of other
node.
• Subtree: Any node of the tree along with its descendant.
Representation of Tree
A tree consists of a root node, and zero or more subtrees T1, T2, … , Tk such that there is an
edge from the root node of the tree to the root node of each subtree. Subtree of a node X
consists of all the nodes which have node X as the ancestor node.
A tree can be represented using a collection of nodes. Each of the nodes can be represented
with the help of class or structs. Below is the representation of Node in different languages:

class Node {
public:
int data;
Node* first_child;
Node* second_child;
Node* third_child;
.
.
.
Node* nth_child;
};

Importance of Tree Data Structure

One reason to use trees might be because you want to


store information that naturally forms a hierarchy. The
file system on a computer is the example.
2. Trees (with some ordering e.g., BST) provide
moderate access/search (quicker than Linked List and
slower than arrays).
3. Trees provide moderate insertion/deletion (quicker
than Arrays and slower than Unordered Linked Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on the number of
nodes as nodes are linked using pointers.
Types of Tree in Data Structure
• Tree data structure can be classified into three types based upon the number of children
each node of the tree can have. The types are:
• Binary tree: In a binary tree, each node can have a maximum of two children linked to
it. Some common types of binary trees include full binary trees, complete binary trees,
balanced binary trees, and degenerate or pathological binary trees.
• Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most
three child nodes, usually distinguished as “left”, “mid” and “right”.
• N-ary Tree or Generic Tree: Generic trees are a collection of nodes where each node
is a data structure that consists of records and a list of references to its children(duplicate
references are not allowed). Unlike the linked list, each node stores the address of
multiple nodes.

Properties of Tree Data Structure


• Number of edges: An edge can be defined as the connection between two nodes. If a
tree has N nodes then it will have (N-1) edges. There is only one path from each node
to any other node of the tree.
• Depth of a node: The depth of a node is defined as the length of the path from the root
to that node. Each edge adds 1 unit of length to the path. So, it can also be defined as
the number of edges in the path from the root of the tree to the node.
• Height of a node: The height of a node can be defined as the length of the longest path
from the node to a leaf node of the tree.
• Height of the Tree: The height of a tree is the length of the longest path from the root
of the tree to a leaf node of the tree.
• Degree of a Node: The total count of subtrees attached to that node is called the degree
of the node. The degree of a leaf node must be 0. The degree of a tree is the maximum
degree of a node among all the nodes in the tree.
Searching in a Binary Tree
• FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
1. [Tree empty]
If ROOT = NULL then Set LOC:= NULL and PAR:= Null and Return.
2. [ITEM at Root]
If ITEM = INFO[ROOT] then set LOC:= Root and PAR:= NULL and Return.
3. [Initialize pointers PTR and SAVE]
If ITEM<INFO[ROOT] then set PTR:= LEFT[ROOT] and SAVE:= Root.
else set PTR:= RIGHT[Root] and SAVE:= ROOT.
[End of If Structure]
4. Repeat steps 5 and 6 while PTR ≠ NULL.
5. [ITEM found?]
If ITEM= INFO[PTR] then set LOC:= PTR and PAR:= SAVE and Return.
6. If ITEM<INFO[PTR] then set SAVE:= PTR and PTR:= LEFT[PTR]
else set SAVE:= PTR and PTR:= LEFT[PTR]
[End of If Structure]
7. [Search unsuccessful] set LOC:= NULL and PAR:= SAVE.
8. Exit.
Traversing a Binary Tree
• There are three standard ways of traversing a binary tree T with root R. These three
algorithm called preorder, inorder and post order, as follows:
Preorder
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Inorder
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Postorder
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postorder.
3. Process the root R.
Depth- First Search Traversal
• DFS (Depth-first search) is a technique used for traversing trees or graphs. Here
backtracking is used for traversal. In this traversal first, the deepest node is visited and
then backtracks to its parent node if no sibling of that node exists.
• In the graph, there might be cycles and disconnectivity. Unlike the graph, the tree does
not contain a cycle and are always connected. So DFS of a tree is relatively easier. We
can simply begin from a node, then traverse its adjacent (or children) without caring
about cycles. And if we begin from a single node (root), and traverse this way, it is
guaranteed that we traverse the whole tree as there is no dis-connectivity,
Example: Therefore, the Depth First Traversals of this Tree will
be:
1. Inorder: 4 2 5 1 3
2. Preorder: 1 2 4 5 3
3. Postorder: 4 5 2 3 1

Breadth-First Search Traversal/ Level Order Traversal


• The breadth-first search or BFS algorithm is used to search a tree or graph data structure
for a node that meets a set of criteria. It begins at the root of the tree or graph and
investigates all nodes at the current depth level before moving on to nodes at the next
depth level.
• This technique is defined as a method to traverse a Tree such that all nodes present in
the same level are traversed completely before traversing the next level.

You might also like