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

C

Uploaded by

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

C

Uploaded by

Adc Adc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Trees

A class of graphs that is acyclic is termed as trees.


Let us now discuss an important class of graphs called trees and its associated
terminology.
Trees are useful in describing any structure that involves hierarchy. Familiar
examples
of such structures are family trees, the hierarchy of positions in an organization,
and so on.

A tree has following general properties:


 One node is distinguished as a root;
 Every node (exclude a root) is connected by a directed edge from exactly
one other node; A direction is: parent -> children

A is a parent of B, C, D,
B is called a child of A.
on the other hand, B is a parent of E, F, K

In the above picture, the root has 3 subtrees.

Each node can have arbitrary number of children. Nodes with no children are
called leaves, or external nodes. In the above picture, C, E, F, L, G are leaves.
Nodes, which are not leaves, are called internalnodes. Internal nodes have at
least one child.

Nodes with the same parent are called siblings. In the picture, B, C, D are called
siblings. The depth of a node is the number of edges from the root to the node.
The depth of K is 2. The height of a node is the number of edges from the
node to the deepest leaf. The height of B is 2. The height of a tree is a height of
a root.
Applications of Trees
Trees and their variants are an extremely useful data structure with lots
of practical applications.

 Binary Search Trees(BSTs) are used to quickly check whether an


element is present in a set or not.
 Heap is a kind of tree that is used for heap sort.
 A modified version of tree called Tries is used in modern routers to
store routing information.
 Most popular databases use B-Trees and T-Trees, which are
variants of the tree structure we learned above to store their data
 Compilers use a syntax tree to validate the syntax of every
program you write.

A full binary tree (sometimes proper binary tree or 2-tree) is a tree in


which every node other than the leaves has two children. A complete
binary tree is abinary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.

Properties of a Binary Tree


A tree is a connected acyclic graph. In many ways, a tree is the simplest non-
trivial type
of graph. It has several good properties such as the fact that there exists a unique
path
between every two vertices. The following theorems list some simple properties
of trees:
Let T be a tree. Then the following properties hold true:
1. There exists a unique path between every two vertices.
2. The number of vertices is one more than the number of edges in the tree.
3. A tree with two or more vertices has at least two leaves.
Property 1
Property 1 comes from the definition of a tree. As a tree is a connected graph,
there exists
at least one path between every two vertices. However, if there are two or more
paths
between a pair of vertices, there would be a circuit in the graph and so the graph
cannot
be a tree.
Property 2
Property 2 can be proved using mathematical induction. Let there be a tree T
with the total
number of edges e and the total number of vertices v.
Induction step A tree with one vertex contains no edge, and a tree with two
vertices has
one edge.

Operations on binary tree The basic operations on a binary tree can be


as listed as
follows:
1. Creation—Creating an empty binary tree to which the ‘root’ points
2. Traversal—Visiting all the nodes in a binary tree
3. Deletion—Deleting a node from a non-empty binary tree
4. Insertion—Inserting a node into an existing (may be empty) binary tree
5. Merge—Merging two binary trees
6. Copy—Copying a binary tree
7. Compare—Comparing two binary trees
8. Finding a replica or mirror of a binary tree

Array Representation
One of the ways to represent a tree using an array is to store the nodes level-by-
level, starting
from the level 0 where the root is present. Such a representation requires
sequential
numbering of the nodes, starting with the nodes on level 0, then those on level 1,
and so on.

Advantages The various merits of representing binary trees using arrays are as
follows:
1. Any node can be accessed from any other node by calculating the index.
2. Here, the data is stored without any pointers to its successor or predecessor.
3. In the programming languages, where dynamic memory allocation is not
possible
(such as BASIC, fortran ), array representation is the only means to store a tree.
Disadvantages The various demerits when representing binary trees using
arrays are
as follows:
1. Other than full binary trees, majority of the array entries may be empty.
2. It allows only static representation. The array size cannot be changed during
the execution.
3. Inserting a new node to it or deleting a node from it is inefficient with this
representation,
because it requires considerable data movement up and down the array, which
demand
excessive amount of processing time.

Linked List Representation


Binary tree has a natural implementation in a linked storage. In a linked
organization, we
wish that all the nodes should be allocated dynamically. Hence, we need each
node with
data and link fields. Each node of a binary tree has both a left and a right subtree.
Each node will have three fields—Lchild, Data, and Rchild.

Here, 0 (zero) stored at Lchild or Rchild fields represents that the respective
child is not present. Let us consider one more example as in Fig. 7.29.

In this node structure, Lchild and Rchild are the two link fields to store the
addresses of left child and right child of a node; data is the information content
of the node. With this representation, if we know the address of the root node,
then using it, any other node can be accessed.

Each node of a binary tree (as the root of some subtree) has both left and right
subtrees, which can be accessed through pointers.
Advantages The merits of representing binary trees through liked
representations are
as follows:

1. The drawbacks of the sequential representation are overcome in this


representation.
We may or may not know the tree depth in advance. In addition, for unbalanced
trees,
the memory is not wasted.

2. Insertion and deletion operations are more efficient in this representation.

3. It is useful for dynamic data.

Disadvantages The demerits of representing binary trees through linked


representation
are as follows:

1. In this representation, there is no direct access to any node. It has to be


traversed from
the root to reach to a particular node.

2. As compared to sequential representation, the memory needed per node is


more. This
is due to two link fields (left child and right child for binary trees) in the node.

3. The programming languages not supporting dynamic memory management


would not
be useful for this representation.

Binary search tree deletion


The question is which subtrees should the parent of the deleted node be linked to, what
shouldbe done with the other subtrees, and where should the remaining subtrees be linked.
Oneof the solutions is to attach the right subtree in place of the deleted node, and then attach
the left subtree onto an appropriate node of the right subtree

or

1. One can search for the largest data in the deleted node’s left subtree and replace the
deleted node with it.
2. One can search for the smallest data from the deleted node’s right sub tree and replace
the deleted node with it.

You might also like