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

Introduction To Tree Data Structure and Algorithm

Tree

Uploaded by

Denden Disono
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)
34 views

Introduction To Tree Data Structure and Algorithm

Tree

Uploaded by

Denden Disono
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/ 23

TREE DATA STRUCTURE

TREE DATA STRUCTURE

A tree data structure 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.

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.
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.
{K, L, M, N, O, P, G} 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 TREE DATA
STRUCTURE:

● Descendant: Any successor node on the path from the leaf node to that node. {E,I} are
the descendants of the node {B}.
● 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.
● Neighbour of a Node: Parent or child nodes of that node are called neighbors of that
node.
● Subtree: Any node of the tree along with its descendant.
REPRESENTATION OF TREE DATA STRUCTURE:

A tree consists of a root, and zero or more subtrees T1, T2, … , Tk such that there is an
edge from the root of the tree to the root of each subtree.
REPRESENTATION OF A NODE IN
TREE DATA STRUCTURE:

struct Node
{

int data;
struct Node *first_child;
struct Node *second_child;
struct Node *third_child;
.
.
.
struct Node *nth_child;

};
EXAMPLE OF TREE DATA STRUCTURE:

● Node 1 is the root node


● 1 is the parent of 2 and 3
● 2 and 3 are the siblings
● 4, 5, 6, and 7 are the leaf nodes
● 1 and 2 are the ancestors of 5
TYPES OF TREE DATA STRUCTURES:
TYPES OF TREE DATA STRUCTURES:

● 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.
BASIC OPERATION OF TREE DATA STRUCTURE:

● Create – create a tree in the data structure.


● Insert − Inserts data in a tree.
● Search − Searches specific data in a tree to check whether it is present or not.
● Traversal:
● Preorder Traversal – perform Traveling a tree in a pre-order manner in the data
structure.
● In order Traversal – perform Traveling a tree in an in-order manner.
● Post-order Traversal –perform Traveling a tree in a post-order manner.
TRAVERSAL IN A TREE DATA STRUCTURE:
In-order traversal

● It starts with visiting all the nodes in the left subtree


● Then visits the root node
● And finally, all the nodes in the right subtree are visited

The order will be: D - B - E - A - F - C - G


TRAVERSAL IN A TREE DATA STRUCTURE:

Pre-order traversal

● First, the root node is visited


● Then all the nodes in the left subtree
● And finally visits all the nodes in the right subtree

The order will be: A - B - D - E - C - F - G


TRAVERSAL IN A TREE DATA STRUCTURE:
Post-order traversal

● Starts with the nodes in the left subtree


● Visits the nodes in the right subtree
● And then visits the root node

The order will be: D - E - B - F - G - C - A


Why Tree is considered a non-linear data structure?

The data in a tree are not stored in a sequential manner they are not stored linearly.
Instead, they are arranged on multiple levels or we can say it is a hierarchical structure.
For this reason, the tree is considered to be a non-linear data structure.
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.
NEED FOR TREE DATA STRUCTURE

1. One reason to use trees might be because you want to store information that naturally forms a
hierarchy.

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.
APPLICATION OF TREE DATA STRUCTURE:

● File System: This allows for efficient navigation and organization of files.
● Data Compression: Huffman coding is a popular technique for data compression that
involves constructing a binary tree where the leaves represent characters and their
frequency of occurrence. The resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
● Compiler Design: In compiler design, a syntax tree is used to represent the structure of a
program.
● Database Indexing: B-trees and other tree structures are used in database indexing to
efficiently search for and retrieve data.
ADVANTAGES OF TREE DATA STRUCTURE:

● Tree offer Efficient Searching Depending on the type of tree, with average
search times of O(log n) for balanced trees like AVL.
● Trees provide a hierarchical representation of data, making it easy to
organize and navigate large amounts of information.
● The recursive nature of trees makes them easy to traverse and manipulate
using recursive algorithms.
DISADVANTAGES OF TREE DATA STRUCTURE:

● Unbalanced Trees, meaning that the height of the tree is skewed towards one
side, which can lead to inefficient search times.
● Trees demand more memory space requirements than some other data
structures like arrays and linked lists, especially if the tree is very large.
● The implementation and manipulation of trees can be complex and require
a good understanding of the algorithms.
JAVA CODE
public static void main(String[]

args) {

BinaryTree tree = new

BinaryTree();

tree.root = new Node(1);

tree.root.left = new Node(12);

tree.root.right = new Node(9);

tree.root.left.left = new Node(5);

tree.root.left.right = new

Node(6);
JAVA CODE
System.out.println("Inorder

traversal");

tree.inorder(tree.root);

System.out.println("\nPreorder

traversal ");

tree.preorder(tree.root);

System.out.println("\nPostorder

traversal");

tree.postorder(tree.root);

}
THANK YOU FOR
LISTENING!!!

You might also like