1 views

Uploaded by NiaSania

d

- The Annotation of Preposition Senses in German
- Binary Trees
- Interview Success Kit
- AVL TREE
- DS Tree Questions
- Monte Carlo Tree Search and Its Applications
- 3rd Sem Data-structure
- Adjancey Matrix Graphs
- Trees 1
- Optimizing Distributed XML Queries Through Localization
- Union Find
- CDS
- datastructure-100211095005-phpapp02
- Trees
- DSF MANUAL ToPrint 27july2017 Marginupdated
- Binary Tree
- Exam2007 Blank
- DS Unit4.pdf
- D3804A15 DS 4
- 1

You are on page 1of 34

By:

Dr. Ghulam Rasool

Tree

A Tree is a non-linear data structure. A

common example of tree is the

linage of a person that is referred to

as the family tree.

Tree is used where the hierarchy

relationship among data are to be

maintained.

Insertion, deletion, searching etc. are

more efficient in trees than linear DS.

Properties of Tree

Each tree has one node called as the root of the tree.

A unique path exists from the root node to all other

nodes in the tree.

Each node , other than the root has a unique

predecessor. It is called parent.

Each node may have no, one or several successor

nodes. Successor node is also called child

Node: It is the main component of any tree structure.

Node of a tree stores the actual data and links to

other nodes

Root Node: The unique Node in the tree that has no

parent node is called root node. It is beginning of tree

Parent Node: The node that generate links for other

nodes is called parent node

Child Node: the node that is directly connected to parent is

called child node

Sub-tree: The child node of a root node that has its own

child nodes is called sub-tree.

Terminal Node: The node having no successor or child node

is called terminal node. It is also called leaf node

Depth of a Node: Each Node has a unique path connecting it

to root. The depth of a node is the total number of nodes

between the node and the root including root node itself.

Height of a Tree: max no of nodes that is possible in a path

starting from root node to a leaf node is called the height

of a tree.

Degree: Max no of children that is possible for a node is

known as the degree of a node.

Binary Trees

A binary tree T is a finite set of

nodes, such that

Each node has at most two child

nodes called left and right.

Figure page 208

A binary tree is a full binary tree, if it

contains maximum possible number

of nodes in all levels

Complete Binary Tree

A binary tree is said to be complete

binary tree, if all its level, except

possibly the last level have the

maximum possible number of nodes

and all the nodes at the last level

appear as far left as possible

In any binary tree, max number of nodes

on level l is 2^l

Maximum number of nodes possible

in a binary tree of height h is 2(h-1)

Minimum number of nodes possible

in a binary tree of height h is h

For any non-empty binary tree, if n is

the no of nodes and e is the no of

edges, then n= e+1

Height of a complete binary tree with

n number of nodes is [log2(n+1)]h

1 If Ptr!= NULL

1 Ptr.data= Item

3 If option= Y then

2 Ptr.lc= lcptr

3 Build_Tree(lcptr, NewL)

4 Else

1 lcptr=NULL

2 Ptr.lc= NULL

3 Build_Tree(lcptr, NULL)

5

Node Ptr has rightsubtree (Y/N) ?

6 If option = Y

2 ptr.rc= rcptr

3 Build_Tree(rcptr, NewR)

Continue

7 Else

1 rc.Ptr= NULL

2 Ptr.Rc=NULL

3 Build_Tree(Ptr, NULL)

Exit

Insertion

Deletion

Traversal

Merging

1

2

If(ptr= Null) then

1 Print Search is unsuccessful

2 Exit

3 If(ptr.LC=NULL) or (ptr.RC=NULL)

1 Read option to insert as left/right child

2 If(option=L) then

1 If(ptr.LC= NULL) then

1 p= new node

2 p.data= Item

3 p.LC=p.RC=NULL

4 ptr.LC=P

2 else

1 Print Insertion is not possible as left child

2 Exit

3 else

1 If(ptr.RC= NULL) then

1 p= new node

2 p.data= Item

3 p.LC=p.RC=NULL

4 ptr.RC=P

2 else

1 Print Insertion is not possible as left child

2 Exit

4 else

Print Key node already has child

5 Exit

Search_Link(PTR0, Key)

1 ptr= PTR0

2 If(ptr.Data!= Key)

1 If(ptr.LC!=NULL)

1 Search_Link(ptr.LC)

2 Else

1 Retrun(0)

3 1 If(ptr.RC!=NULL)

1 Search_Link(ptr.RC)

2 Else

1 Retrun(0)

3 Else

1 Return(ptr)

4 Exit

Following are tree traversal methods

Pre-order

In-order

Post-order

Algorithm Pre-Order(Root)

1

Ptr= Root

2 If(Ptr != NULL)

1 Print Ptr.i

2 Pre-order(Ptr.Lc)

3 Pre-order(Ptr.Rc)

3 Exit

Algorithm IN-Order(Root)

1

Ptr= Root

4 If(Ptr != NULL)

1 In-order(Ptr.Lc)

2 Print Ptr.i

3 In-order(Ptr.Rc)

3 Exit

Algorithm Post-Order(Root)

1

2

Ptr= Root

If(Ptr != NULL)

1 Post-order(Ptr.Lc)

2 Post-order(Ptr.Rc)

3 Print Ptr.i

3 Exit

A binary tree T is called binary search tree if

each node N of T satisfies the following

property:

The value at N is greater than every value in

left sub-tree of N and is less than every

value in the right sub-tree

Following operations are performed:

Searching for data

Inserting any data into tree

Deleting any data from tree

Traversing the tree

1

2

3

4

Root.data=item

Root.Lchild=Null

Root.Rchild=Null

Repeat step 3-5 until user want to create nodes

P= Root and get value of new node from user

While (P!=Null)

If P.data=item

print data already exists

Exit

ElseIf(P.data<item)

Parent=P

P=P.RChild

BST Creation

else Parent= P

P= P.LChild

5 Q= new node

Q. data= item

Q.LChild= Q.Rchild=Null

If (Parent= Null)

Parent=Q

ElseIf( Parent.data>item)

Parent.LChild=Q

Else

Parent.RChild=Q

6

Exit

Algorithm BST_Insert(Item)

1 Ptr= Root and falg= False

2 While(Ptr!= NIL) and (flag =False)

1

Case Item< ptr.Data

1 ptr1=ptr

2 ptr= ptr.Child

2

Case Item > ptr.Data

1 ptr1=ptr

2 ptr= ptr.Child

3

Case Item = ptr.Data

1 flag= True

3 exit

4 End Case 5

3. End While

Insertion in BST

4 If(Ptr = NIL)

1 P= New Node

2 P.data= item

3. P.Lchild=P.Rchild= NIL

4 If(ptr1.Data < Item)

Ptr1.Rchild= p

else

Ptr1.Lchild= P

5 Exit

Expression Tree

A Binary tree that is used to store arithmetic expressions is

called expression tree.

a+b*c*d+e

1 Scan PSN expression from left to right until end of string

2

If (symbol = operand)

P= new node

P.data= symbol

Push(P)

Else

Ptr1= Pop()

Ptr2= Pop()

P= new node

P.data= symbol

P.Lchild= Ptr2

P.Rchild=Ptr1

Push(P)

3

Exit

Expression Tree

Write an algorithm/program that

should evaluate expression tree

Write an algorithm/program that

should search value from BST

Heap Tree

A complete binary tree is called heap

tree if it satisfies:

For each node N in H, the value at N

is greater than or equal to the value

of each of children of N

Such a tree is called Max heap tree

In Min heap any node N has the

value less than or equal to the value

of any successor of N.

Building Heap

Start with the last internal node

Swap the current internal node with

its larger child, if necessary

Then follow the swapped node down

Continue until all internal nodes are

done

Heap Tree

Insertion in a heap

Insert value 6

Insertion

AVL Tree

A self balancing binary search tree

The heights of the two child subtrees

of any node differ by at most one

Re-balancing is required if required

BST

1

4

2

3

1

4

4

12/26/03

6

3

Is this balanced?

2

1

6

7

AVL Trees - Lecture 8

7

32

Heap Tree

Write a program that should insert

and delete nodes from Heap Tree

Write a program that should sort the

data using heap tree

Quiz

Insert the following numbers into a

binary search tree in the order that

they are given and draw the resulting

tree.

87; 36; 22; 15; 56; 85; 48; 90; 72; 6

Delete 48 and draw the resulting

tree.

Delete 15 and draw the resulting tree

- The Annotation of Preposition Senses in GermanUploaded bydeboradom3891
- Binary TreesUploaded byFurqan Shaikh
- Interview Success KitUploaded byRajesh Patwardhan
- AVL TREEUploaded byAnand Yadav
- DS Tree QuestionsUploaded bynimmapreetu
- Monte Carlo Tree Search and Its ApplicationsUploaded byMihai Nan
- 3rd Sem Data-structureUploaded bySanjeev Surad
- Adjancey Matrix GraphsUploaded bynothingelse420
- Trees 1Uploaded byAnusha Gontla
- Optimizing Distributed XML Queries Through LocalizationUploaded bygeorgiua
- Union FindUploaded byGokul Guru
- CDSUploaded byapi-19908948
- datastructure-100211095005-phpapp02Uploaded byamritamurali
- TreesUploaded byGobara Dhan
- DSF MANUAL ToPrint 27july2017 MarginupdatedUploaded byNithin YM
- Binary TreeUploaded byOk Nope
- DS Unit4.pdfUploaded byprasath_676303
- Exam2007 BlankUploaded bytanmaya1991
- D3804A15 DS 4Uploaded bySurendra Singh Chauhan
- 1Uploaded bySriram Ganesh
- pset0Uploaded bysssbulbul
- Lecture16-BSTrees.pptUploaded bycatarac19
- Data Structure Questions Set 2 and Set 3Uploaded byShaaahid
- AVL treeUploaded bySureshSawant
- avlUploaded byben_sundar
- Lecture 7.pdfUploaded byAvanakshSingh
- a2.pdfUploaded byGabito Essien
- Binary Search TreeUploaded byvsamr
- 10.1080@13658816.2013.801578Uploaded byJohn Justine Villar
- Brute ForceUploaded byvenu4disc

- Data StructuresUploaded byHenryHai Nguyen
- Binarysearch TreeUploaded bySaurabh Kaushik
- DS(Data Structure)-MCQsUploaded byrkeranga
- 1Uploaded bySupriya Kumari
- Computer Science Engineering Sample PapersUploaded byapi-20012397
- Data Structures - Abstractions and Design Using Java (Third Edition, 2016)Uploaded byfuckyuo
- Assignment IC Binary TreesUploaded byAditya Pawar
- r059211201 Advanced Data Structures and AlgorithmsUploaded bySRINIVASA RAO GANTA
- obstUploaded byAashima Gupta
- Lecture12-4Uploaded byJohnathan Hok
- csc263Uploaded byhitman047
- Jwasham_google-Interview-university_ a Complete Daily Plan for Studying to Become a Google Software EngineerUploaded byHarssh S Shrivastava
- - binary search treeUploaded byapi-237335979
- BCA SyllabusUploaded byamarkartu
- Ds Lab ManualUploaded byPrakash Palanisamy
- Algorithm Gym __ Data Structures - CodeforcesUploaded bySanjeev Singh
- Data Structures and AlgorithmsUploaded byAlaukikDoc
- CS 202-EE 202-Data Structures-Shafay ShamailUploaded byAzeem Iqbal
- Data Structure Question Bank (2)Uploaded byBabu Giri
- Tree pptUploaded bySheelnidhi Pandey
- Ads a Lab ManualUploaded byVivekananda Ganjigunta Narayana
- data struct anna univ ques paperUploaded byvijimaha
- QuestionsUploaded bySaravana Priya
- Data Structures Algorithms EditedUploaded byishandeep
- Sd SummaryUploaded byGabriel M Bero
- Ds&Oops Lab Manual-eceUploaded byHari Krishnan
- basic conceptUploaded byPraveen Kumar
- Binary Tree (Part 1)-Chapter 6Uploaded byYudin Gituloh
- ADS Question PapersUploaded byapi-3849895
- Qbasic for SLCUploaded bylahansanjeev