0% found this document useful (0 votes)
38 views8 pages

Unit 3a

The document discusses trees and binary trees. It defines key tree concepts like nodes, roots, branches, leaves, subtrees, degrees, siblings, ancestors, levels, heights, and traversals. It then defines binary trees and their specific properties and representations. Binary trees can be represented using arrays or linked lists, with the array representation storing nodes level-by-level and the linked representation using pointers between nodes.

Uploaded by

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

Unit 3a

The document discusses trees and binary trees. It defines key tree concepts like nodes, roots, branches, leaves, subtrees, degrees, siblings, ancestors, levels, heights, and traversals. It then defines binary trees and their specific properties and representations. Binary trees can be represented using arrays or linked lists, with the array representation storing nodes level-by-level and the linked representation using pointers between nodes.

Uploaded by

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

UNIT – 3 TREES

Tree:
 A tree is a finite set of one or more nodes such that, there is a specially designated
node called root.

 The remaining nodes are partitioned into n>=0 disjoint sets T1, T2,..Tn,

 where each of these set is a tree T1,…Tn are called the subtrees of the root.

Branch: Branch is the link between the parent and its child.

Leaf: A node with no children is called a leaf.

Subtree: A Sub tree is a subset of a tree that is itself a tree.

Degree:

 The number of sub trees of a node is called the degree of the node.

 Hence nodes that have degree zero are called leaf or terminal nodes.

 The other nodes are referred as non-terminal nodes.

Siblings: Children of the same parent are said to be siblings.

Degree of tree: Degree of the tree is the maximum of the degree of the nodes in the tree.

Ancestors: Ancestors of a node are all the nodes along the path from root to that node.
Hence root is ancestor of all the nodes in the tree.

Level:s Level of a node is defined by letting root at level one. If a node is at level L,
then its children are at level L + 1.

Height or depth: The height or depth of a tree is defined to be the maximum level of
any node in the tree.

Climbing: The process of traversing the tree from the leaf to the root is called climbing
the tree.

Descending: The process of traversing the tree from the root to the leaf is called
descending the tree.
BINARY TREE

Binary tree

 A binary tree is a finite set of nodes that either is empty or consists of a root

 And two disjoint binary trees called the left subtree and right subtree.

 Binary tree has nodes each of which has no more than two child nodes.

Left child: The node present to the left of the parent node is called the left child.

Right child: The node present to the right of the parent node is called the right child.
Skewed Binary tree:

If the new nodes in the tree are added only to one side of the binary tree then it is
a skewed binary tree.

Strictly binary tree:

If the binary tree has each node consisting of either two nodes or no nodes at all,
then it is called a strictly binary tree.

Complete binary tree:

If all the nodes of a binary tree consist of two nodes each and the nodes at the last
level does not consist any nodes, then that type of binary tree is called a complete
binary tree.

It can be observed that the maximum number of nodes on level i of a binary tree
is 2i - 1, where i ≥ 1. The maximum number of nodes in a binary tree of depth k is 2k – 1,
where k ≥ 1.
REPRESENTATION OF BINARY TREES

There are two ways in which a binary tree can be represented. They are

(i) Array representation of binary trees.( Linear or Sequential )


(ii) Linked representation of binary trees.

(i) ARRAY REPRESENTATION OF BINARY TREES

 The nodes are started level by Level.

 Starting from the Level 0 where only ROOT NODE is present.

 Root Node is stored in the first memory location.

 When arrays are used to represent the binary trees then

An array of size 2k is declared

Where, k is the depth of the tree.

Rules

The root node is at location 1.

For any node with index i 1< i < n

 Parent[i]=[i/2]
Where i=1 there is no parent.

 Lchild(i)=2*i
If 2*i>n then I has no left child.

 Rchild(i)=2*i+1
If 2*i+1>n then I has no left child.

For example:

 If the depth of the binary tree is 3 then maximum 2 3 - 1 = 7 elements will be


present in the node and hence the array size will be 8.

 This is because the elements are stored from position one leaving the position 0
vacant.

 But generally an array of bigger size is declared so that later new nodes can be
added to the existing tree.
The following binary tree can be represented using arrays as shown.
Array representation

The root element is always stored in position 1.

 The left child of node i is stored in position 2i and

 The right child of node is stored in position 2i + 1.

 Hence the following formulae can be used to identify the parent, left child
and right child of a particular node.

Parent( i ) = i / 2, if i ≠ 1. If i = 1 then i is the root node and root does not has parent.

Left child( i ) = 2i, if 2i ≤ n, where n is the maximum number of elements in


the tree. If 2i > n, then i has no left child.

Right child( i ) = 2i + 1, if 2i + 1 ≤ n. If 2i + 1 > n, then i has no right child.

 The empty positions in the tree where no node is connected are represented in the
array using -1, indicating absence of a node.
LINKED REPRESENTATION OF BINARY TREES

In linked representation of binary trees pointers are used to connect the various
nodes of the tree.

Hence each node of the binary tree consists of three parts namely
 Info:- stores the data,
 Left :- stores the address of the left child
 Right:-stores the address of the right child

LC Data RC

Logically the binary tree in linked form can be represented as shown.

 The pointers storing NULL value indicates that there is no node attached to it.
Traversing through this type of representation is very easy.

 The left child of a particular node can be accessed by following the left link of
that node and the right child of a particular node can be accessed by following the
right link of that node.

Position Parent Lchild Rchild


Node
1 50 30 40
2 30 10 NULL
3 40 NULL 20
4 10 NULL NULL
5 20 NULL NULL
Example : 2

- *

N A N N B N N C N N D N

You might also like