Data Structure - Tree - Notes, Study notes of Data Structures and Algorithms

In this document topics covered which are Tree, Binary tree, Strictly binary tree, Strictly binary tree, Implementation, Array Implementation of Binary tree.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tree
Def: A tree is a finite set of one or more
nodes such that
(i) There is a specially designated node
called root.
(ii) The remaining nodes are partitioned into
n ≥ 0 disjoint sets T
1
, T
2
, … T
k
where
each of these sets is a tree.T
1
, T
2
, T
k
are called subtrees of the root.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Data Structure - Tree - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Tree

Def: A tree is a finite set of one or more

nodes such that

(i) There is a specially designated node

called root.

(ii) The remaining nodes are partitioned into

n ≥ 0 disjoint sets T 1

, T

2

, … T

k

where

each of these sets is a tree.T

1

, T

2

, T

k

are called subtrees of the root.

Binary tree

A binary tree is a finite set of elements that is

either empty or is partitioned into three

disjoint subsets.The first subset contains a

single element called root of the tree. The

other two subsets are themselves binary

trees called left subtree and right subtree.

A left or right subtree may be empty.

A binary tree

Strictly binary tree

If every non leaf node in a binary tree has

nonempty left and nonempty right subtree,

then binary tree is called

strictly binary tree.

Level – level of root is defined to be zero

level of any other node is defined as one

more than the level of its father.

Depth: maximum level of any node in a

tree is called depth of the tree.

Siblings: nodes with the same father are

called siblings.

Leaves: nodes with no children

Path: is a list of distinct vertices in which

successive vertices are connected by

edges in the tree.

Implementation

A binary tree can be implemented as an

array of nodes or a linked list.

Array implementation is done by

sequential numbering.

Start numbering from the root.

Then number the nodes of the next level

from left to right, numbering missing nodes

also

This representation can be used for all

binary trees though in most cases there

will be a lot of unutilized space.

For complete binary trees, this

representation is ideal as no space is

wasted.

  • (^) For skewed trees less than half the array

is utilized.

Linked list implementation

The most common and easiest way to

implement a binary tree is to represent a

node as a structure consisting of the data

and two pointers

These pointers point to two children of the

binary tree.

Linked List implemenation

  • (^) The binary tree can be constructed in a straightforward

manner.

  • The procedures maketree, setleft, setright are as follows:
  • (^) bt_nodeptr maketree(x)
  • (^) char x,
  • {
  • (^) bt_nodeptr p;
  • (^) p=new bt-makeptr;
  • (^) p->info=x;
  • (^) p->left =null;
  • p->right =null;
  • (^) return(p);
  • (^) }
  • (^) setleft(p,x)
  • (^) bt_nodepte p;
  • (^) char x;
  • (^) {
  • (^) If (p== NULL)
  • (^) cout<<“\n void insertion”;
  • (^) else if (p->left! = NULL)
  • (^) cout <<“\n invalid insertion”;
  • (^) else
  • (^) p->left =maketree(x);
  • (^) }

Binary tree traversal method

Traversal means visit each node exactly

once.

  • (^) A tree traversal is just the listing of the

nodes of a tree in a certain order.

This traversal is used at no. of places:

Searching,

Complier uses it in scanning , parsing,

code generation and arithmetic expression

evaluation.

Traversals

Preorder traversal

  • (^) Postorder traversal

Inorder traversal

A

B

C

D E

G

F

R

T

L

T

Preorder traversal of T process A then traverses left subtree

L

T

and finally traverses right subtree R

L.

The preorder of L

T

processes its root B and then D and E. The preorder of R

T

Recursive function

  • (^) preorder (tree)
  • (^) nodeptr tree;
  • (^) {
  • (^) If (tree!= NULL)
  • (^) {
  • cout<<“\n “;
  • tree->info;
  • preorder(tree->left);
  • (^) preorder(tree->right);
  • (^) }
  • (^) }