Académique Documents
Professionnel Documents
Culture Documents
htm
ConvertaBinaryTreetoThreadedbinarytree|Set2(Efficient)GeeksforGeeks
http://www.geeksforgeeks.org/convertbinarytreethreadedbinarytreeset2efficient/
//Thisfunctionreturnsrightmostchildofroot.
Node*createThreaded(Node*root)
{
//Basecases:Treeisemptyorhassinglenode
if(root==NULL)
returnNULL
if(root>left==NULL&&
root>right==NULL)
returnroot
//Findpredecessorifitexists
if(root>left!=NULL)
{
//Findpredecessorofroot(Rightmost
//childinleftsubtree)
Node*l=createThreaded(root>left)
//Linkathreadfrompredecessortoroot.
l>right=root
l>isThreaded=true
}
//Ifcurrentnodeisrightmostchild
if(root>right==NULL)
returnroot
//Recurforrightsubtree.
returncreateThreaded(root>right)
}
PopulateInorderSuccessorforallnodesGeeksforGeeks
http://www.geeksforgeeks.org/populateinordersuccessorforallnodes/
/*SetnextofpandalldescendentsofpbytraversingtheminreverseInorder*/
voidpopulateNext(structnode*p)
{
//Thefirstvisitednodewillbetherightmostnode
/nextoftherightmostnodewillbeNULL
staticstructnode*next=NULL
if(p)
{
//Firstsetthenextpointerinrightsubtree
populateNext(p>right)
//SetthenextaspreviouslyvisitednodeinreverseInorder
p>next=next
//Changetheprevforsubsequentnode
next=p
file:///C:/Users/akash/Desktop/placement%20docs/inorder.htm 1/3
7/3/2017 inorder.htm
//Finally,setthenextpointerinleftsubtree
populateNext(p>left)
}
}
PrintcousinsofgivennodeinabinarytreeTechieDelight
http://www.techiedelight.com/printcousinsofgivennodebinarytree/
//Functiontofindlevelofgivennodex
voidLevel(Node*root,Node*x,intindex,int&level)
{
//returniftreeisemptyorlevelisalreadyfound
if(root==nullptr||level)
return
//ifgivennodeisfound,updateitslevel
if(root==x)
level=index
//recurseforleftandrightsubtree
Level(root>left,x,index+1,level)
Level(root>right,x,index+1,level)
}
voidprintLevel(Node*root,Node*node,intlevel)
{
//basecase
if(root==nullptr)
return
//printcousins
if(level==1)
{
cout<<root>key<<""
return
}
//recurseforleftandrightsubtreeifrootisnotparent
//ofgivennode
if(root>left&&root>left!=node&&
root>right&&root>right!=node)
{
printLevel(root>left,node,level1)
printLevel(root>right,node,level1)
}
}
//Functiontoprintallcousinsofgivennode
voidprintAllCousins(Node*root,Node*node)
{
intlevel=0
//findlevelofgivennode
Level(root,node,1,level)
//printallcousinsofgivennodeusingitslevelnumber
printLevel(root,node,level)
}
file:///C:/Users/akash/Desktop/placement%20docs/inorder.htm 2/3
7/3/2017 inorder.htm
DetermineifgiventwonodesarecousinsofeachotherTechieDelight
http://www.techiedelight.com/determinetwonodesarecousins/
Checktheheightofboththenodes,ifheightsaredifferentthenreturnfalse.
Checkifboththenodeshasthesameparent,ifyesthenreturnfalse.
elsereturntrue.
intisSibling(structNode*root,structNode*a,structNode*b)
{
//Basecase
if(root==NULL)return0
return((root>left==a&&root>right==b)||
(root>left==b&&root>right==a)||
isSibling(root>left,a,b)||
isSibling(root>right,a,b))
}
//RecursivefunctiontofindlevelofNode'ptr'inabinarytree
intlevel(structNode*root,structNode*ptr,intlev)
{
//basecases
if(root==NULL)return0
if(root==ptr)returnlev
//ReturnlevelifNodeispresentinleftsubtree
intl=level(root>left,ptr,lev+1)
if(l!=0)returnl
//Elsesearchinrightsubtree
returnlevel(root>right,ptr,lev+1)
}
//Returns1ifaandbarecousins,otherwise0
intisCousin(structNode*root,structNode*a,structNode*b)
{
//1.ThetwoNodesshouldbeonthesamelevelinthebinarytree.
//2.ThetwoNodesshouldnotbesiblings(meansthattheyshould
//nothavethesameparentNode).
if((level(root,a,1)==level(root,b,1))&&!(isSibling(root,a,b)))
return1
elsereturn0
}
file:///C:/Users/akash/Desktop/placement%20docs/inorder.htm 3/3