0% found this document useful (0 votes)
12 views13 pages

Trees

A document that provides the explanation of how trees work in data structures

Uploaded by

niko ferns
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)
12 views13 pages

Trees

A document that provides the explanation of how trees work in data structures

Uploaded by

niko ferns
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/ 13

Trees

Sayan Sikder
SCOPE, VIT University, Vellore
2023
Introduction

• 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.
Representation
Recursive Definition

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.

;
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} are the leaf nodes of
the tree.
Basic Terminologies In Tree Data Structure (contd.)

• 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: 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 neighbours of
that node.

• Subtree: Any node of the tree along with its descendant.


Properties of a Tree

• 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.
Syntax in C
Basic Operation Of Tree

1. Create – create a tree in data structure.

2. Insert − Inserts data in a tree.

3. Search − Searches specific data in a tree to check it is present or not.

4. Preorder Traversal – perform Traveling a tree in a pre-order manner in data


structure.

5. Inorder Traversal – perform Traveling a tree in an in-order manner.

6. Postorder Traversal –perform Traveling a tree in a post-order manner.


Inorder Traversal

Algorithm Inorder (tree):

▪ Traverse the left subtree, i.e., call Inorder (left->subtree)

▪ Visit the root.

▪ Traverse the right subtree, i.e., call Inorder (right->subtree)


Preorder Traversal

Algorithm Preorder (tree):

1.Visit the root.

2.Traverse the left subtree, i.e., call Preorder (left->subtree)

3.Traverse the right subtree, i.e., call Preorder (right->subtree)


Postorder Traversal

Algorithm Postorder (tree):

1.Traverse the left subtree, i.e., call Postorder (left->subtree)

2.Traverse the right subtree, i.e., call Postorder (right->subtree)

3.Visit the root


Expression tree

You might also like