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

Tree-Data-Structures

A tree data structure is a hierarchical model consisting of nodes connected by directed edges, with no cycles, ensuring each node has one parent. Types of trees include binary trees, N-ary trees, and general trees, with various traversal techniques such as pre-order, in-order, and post-order. Applications of tree structures span database indexing, network routing, search engines, and compiler design, offering advantages like efficient searching and flexible hierarchical representation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tree-Data-Structures

A tree data structure is a hierarchical model consisting of nodes connected by directed edges, with no cycles, ensuring each node has one parent. Types of trees include binary trees, N-ary trees, and general trees, with various traversal techniques such as pre-order, in-order, and post-order. Applications of tree structures span database indexing, network routing, search engines, and compiler design, offering advantages like efficient searching and flexible hierarchical representation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Tree Data Structures

A tree data structure is a hierarchical data structure that resembles a real-world tree. The elements, called nodes,
are connected by edges, creating a parent-child relationship.

rp
by ravi prajapati
Definition and Properties
1 Hierarchical 2 Directed Edges
Structure
Edges connect nodes,
A tree organizes data in a indicating the parent-
hierarchical manner, with child relationship, and the
a root node at the top direction of the
and branches leading to relationship is defined.
child nodes.

3 No Cycles
There are no circular paths in a tree, ensuring that each
node has only one parent.
Types of Trees by Definition
1 Binary Tree 2 N-ary Tree
Each node has a maximum of two children. Each node can have up to N children.

3 General Tree 4
No restrictions on the number of children a node can have.

5 6
Terminology and Concepts
Root The topmost node in the tree.

Node A data element in the tree structure.

Edge A connection between two nodes.

Parent The node directly above a given node.

Child The nodes directly below a given node.

Leaf A node with no children.


Binary Tree Fundamentals
Structure Types Operations

A binary tree is a tree data There are different types of binary Common operations on binary
structure where each node can trees, such as full, complete, and trees include insertion, deletion,
have at most two children. perfect binary trees. search, and traversal.
Tree Traversal Techniques
Pre-order Traversal
Visit the root node, then the left subtree, then the
right subtree.

In-order Traversal
Visit the left subtree, then the root node, then the
right subtree.

Post-order Traversal
Visit the left subtree, then the right subtree, then the
root node.
C++ Code to Create Binary Tree
#include <iostream>

using namespace std;

struct Node {
int data;
Node* left;
Node* right;
};

Node* newNode(int data) {


Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}

int main() {
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
return 0;
}
Algorithm for Inserting and Deleting Elements
Insertion 1
1. Start at the root node. 2. Compare the new
data with the current node's data. 3. If the new
data is less than the current node's data, move 2 Deletion
to the left child. 4. If the new data is greater than 1. Find the node to be deleted. 2. If the node is a
the current node's data, move to the right child. leaf node, simply delete it. 3. If the node has one
5. Repeat steps 2-4 until you reach a null pointer. child, replace the node with its child. 4. If the
6. Insert the new node at the null pointer node has two children, find the inorder successor
location. (the smallest node in the right subtree) and
replace the node with the successor. 5.
Recursively delete the successor from its original
position.
Applications of Tree Data Structures

Database Indexing Network Routing Search Engines Compiler Design


Trees are used in Tree structures are used to Trees are used to store Trees are used to
databases for efficient represent network and organize web pages represent the syntax of
data retrieval and topologies and optimize and search results for programming languages
indexing. routing algorithms. efficient retrieval. and optimize code
generation.
Advantages of Tree Data Structures
1 Efficient Searching 2 Hierarchical 3 Flexibility
Representation
Trees allow for quick searching Trees can be easily modified
of data, especially when they Trees provide a natural way to by adding or deleting nodes
are balanced. represent hierarchical without disrupting the overall
relationships in data. structure.

You might also like