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

Trees

A tree is a nonlinear hierarchical data structure composed of nodes connected by edges. It represents hierarchical data. A tree has a root node that branches into child nodes, which may themselves have child nodes. Trees allow for nonlinear storage of data and can organize elements into different levels in a hierarchical manner. Common tree operations include searching, inserting, and traversing nodes in various orders.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Trees

A tree is a nonlinear hierarchical data structure composed of nodes connected by edges. It represents hierarchical data. A tree has a root node that branches into child nodes, which may themselves have child nodes. Trees allow for nonlinear storage of data and can organize elements into different levels in a hierarchical manner. Common tree operations include searching, inserting, and traversing nodes in various orders.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

TREES DATA STRUCTURE

A tree is a nonlinear hierarchical data structure that consists of nodes


connected by edges.
It is a type of data structure representing hierarchical data.

o A tree is a collection of entities called nodes.


o Nodes are connected by edges.
o Each node contains a value or data , and it may or may not have a child
node . The first node of the tree is called the root .
o A tree data structure is a non-linear data structure because it does not store
in a sequential manner.
o It is a hierarchical structure as elements in a Tree are arranged in multiple
levels.
o In the Tree data structure, the topmost node is known as a root node.
o Each node contains some data, and data can be of any type.
o Each node contains some data and the link or reference of other nodes that
can be called children.
Root
• The first node from where the tree originates is called as a root node.
• In any tree, there must be only one root node.
• We can never have multiple root nodes in a tree data structure.

Example

Here, node A is the only root node.

Parent
• The node which has a branch from it to any other node is called as a parent node.
• In other words, the node which has one or more children is called as a parent
node.
• In a tree, a parent node can have any number of child nodes.

Example-
Here,
• Node A is the parent of nodes B and C
• Node B is the parent of nodes D, E and F
• Node C is the parent of nodes G and H
• Node E is the parent of nodes I and J
• Node G is the parent of node K

Ancestors of node
• Ancestors: The nodes obtained in the path from the specified node x while
moving upwards towards the root node are called ancestors..

• Descendants:
• The nodes in the path below the parent are called descendants.
• In other words, the nodes that are all reachable from a node x while moving
downwards are all called descendants of x.

Siblings
• Nodes which are belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.
Example

Terminal and Non Terminal Nodes

• A terminal node is a node like a leaf at the edge of the tree;


• No other nodes exist beyond it (hence "terminal").

• A non-terminal node is any node that exists in the graph between other
nodes.

Edge
• The connecting link between any two nodes is called as an edge.
• In a tree with n number of nodes, there are exactly (n-1) number of edges.
Example

Child

• The node which is a descendant of some node is called as a child node.


• All the nodes except root node are child nodes.

Example

Here,
• Nodes B and C are the children of node A
• Nodes D, E and F are the children of node B
• Nodes G and H are the children of node C
• Nodes I and J are the children of node E
• Node K is the child of node G
Siblings

• Nodes which belong to the same parent are called as siblings.


• In other words, nodes with the same parent are sibling nodes.
Example

Degree

• Degree of a node is the total number of children of that node.


• Degree of a tree is the highest degree of a node among all the nodes in the tree.

Example

Here,
• Degree of node A = 2
• Degree of node B = 3
• Degree of node C = 2
• Degree of node D = 0
• Degree of node E = 2
• Degree of node F = 0
• Degree of node G = 1
• Degree of node H = 0
• Degree of node I = 0
• Degree of node J = 0
• Degree of node K = 0

Level

• In a tree, each step from top to bottom is called as level of a tree.


• The level count starts with 0 and increments by 1 at each level or step.

Example

Path
Path refers to the sequence of nodes along the edges of a tree.
Depth
The depth of the node is the distance from the root node to that
particular node.
• Total number of edges from root node to a particular node is called as depth of
that node.
• Depth of a tree is the total number of edges from root node to a leaf node in the
longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.

Example

Here,
• Depth of node A = 0
• Depth of node B = 1
• Depth of node C = 1
• Depth of node D = 2
• Depth of node E = 2
• Depth of node F = 2
• Depth of node G = 2
• Depth of node H = 2
• Depth of node I = 3
• Depth of node J = 3
• Depth of node K = 3
• Height: The height of the node is the distance from that node to the
deepest node of that subtree.

• Height of tree: The Height of the tree is the maximum height of any
node. This is same as the height of root node

• Internal Node:- Internal Node means that a node which has at least
a single child.

• External Node:- External Node means that a node which has no


children. It is also known as leaf.

Binary trees
Definition: A binary tree is a tree which has finite set of nodes that is either empty
or consist of a root and two subtrees called left subtree and right subtree.
Or
A binary tree is a tree data structure in which each node has at most two
children, which are referred to as the left child and the right child.
A binary tree can be partitioned into three subgroups namely root, left subtree and
right subtree.

• Root – If tree is not empty, the first node in the tree is called root node.
• left subtree – It is a tree which is connected to the left of root. Since this
tree
• comes towards left of root, it is called left subtree.
• right subtree – It is a tree which is connected to the right of root. Since this
tree comes towards right of root, it is called right subtree.
Note: In general, a tree in which each node has either zero, one or two subtrees is
called a binary tree.
The pictorial representation of a typical node in a binary tree is shown below:
Properties of binary trees
a) The maximum number of nodes on level i of a binary tree = 2i for i ≥ 0
b) The maximum number of nodes in a binary tree of depth k = 2k – 1
c) The number of leaf nodes is equal to number of nodes of degree 2

Types of binary trees

• Strictly binary tree (Full binary tree)


• Skewed tree
• Complete binary trees
• Binary search trees

Strictly binary tree (Full binary tree)

A full binary tree (Strictly binary tree or 2-tree) is a tree in which every node
other than the leaves has two children

or

A binary tree having 2i nodes in any given level i is called strictly binary tree.
Here, every node other than the leaf node has two children. A strictly binary
tree is also called full binary tree or proper binary tree.

Skewed tree
Definition: A skewed tree is a tree consisting of only left subtree or only right
subtree. A tree with only left subtrees is called left skewed binary tree and a
tree with only right subtree is called right skewed binary tree.
Complete binary tree

Definition: A complete binary tree is a binary tree in which every level, except possibly
the last level is completely filled. If the nodes in the last level are not completely filled,
then all the nodes in that level should be filled only from left to right.

For example, all the trees shown below are complete binary trees.

Binary Search Tree (BST)

A binary search tree is a binary tree in which for each node say x in the tree, elements
in the left-subtree are less than info(x) and elements in the right subtree are greater
than info(x). Every node in the tree should satisfy this condition.

Basic Operations

Following are the basic operations of a tree −

• Search − Searches an element in a tree.

• Insert − Inserts an element in a tree.

• Pre-order Traversal − Traverses a tree in a pre-order manner.

• In-order Traversal − Traverses a tree in an in-order manner.

• Post-order Traversal − Traverses a tree in a post-order manner.


Search Operation

• Whenever an element is to be searched, start searching from the root node.


Then if the data is less than the key value, search for the element in the left
subtree. Otherwise, search for the element in the right subtree. Follow the same
algorithm for each node.

Insert Operation

• Whenever an element is to be inserted, first locate its proper location. Start


searching from the root node, then if the data is less than the key value, search
for the empty location in the left subtree and insert the data. Otherwise, search
for the empty location in the right subtree and insert the data.

Binary tree Traversals

Definition: Traversing is a method of visiting each node of a tree exactly once


in a systematic order.
During traversal, we may print the info field of each node visited.

• various traversal techniques of a binary tree are shown below:


Inorder Traversal

Array representation of tree

You might also like