10B Tree Data Structure and Intro to Recursion
10B Tree Data Structure and Intro to Recursion
Cosmin E. Oancea
[email protected]
1 The tree has one node called root. The tree originates from
this, and hence the root does not have any parent.
2 Each node other than the root has one parent only but can
have multiple children.
It follows that for any node, there is a unique path (in the tree)
from the root to that node.
Tree Nomenclature
Ken
Lis Palle
c l a s s B i n Tr e e {
data ; // t h e d a t a s t o r e d i n t h i s node
left ; // the l e f t subtree Ken
right ; // the r i g h t subtree
Lis Palle
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ;
Sarah Jens Else Christian
this . right = r ;
}
}
Building a Binary Tree
c l a s s B i n Tr e e {
data ; // t h e d a t a s t o r e d i n t h i s node
left ; // the l e f t subtree Ken
right ; // the r i g h t subtree
Lis Palle
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ;
Sarah Jens Else Christian
this . right = r ;
}
}
ken . r i g h t = new B i n Tr e e ( ” Pa l e ” , n u l l , n u l l ) ;
ken . r i g h t . r i g h t = new B i n Tr e e ( ” C h r i s t i a n ” , n u l l , n u l l ) ;
ken . r i g h t . l e f t = new B i n Tr e e ( ” E l s e ” , n u l l , n u l l ) ;
l e t mum = new B i n Tr e e ( ” L i s ”
, new B i n Tr e e ( ” Sarah ” , n u l l , n u l l )
, new B i n Tr e e ( ” J e n s ” , n u l l , n u l l )
);
ken . l e f t = mum;
What is a Recursive Function
A recursive function is a function that calls itself once or in
multiple places.
Divide-and-Conquer Reasoning:
f u n c t i o n countTreeNodes ( node ) {
i f ( node === n u l l ) { / / base case
return 0;
}
l e t n u m l e f t = countTreeNodes ( node . l e f t ) ;
l e t n u m r i g h t = countTreeNodes ( node . r i g h t ) ;
return (1 + num left + num right ) ;
}
countTreeNodes ( ken ) ; / / 7
countTreeNodes (mum ) ; / / 3
Finding a Value in a Binary Tree
c l a s s B i n Tr e e {
data ; // t h e d a t a s t o r e d i n t h i s node
Ken
left ; // the l e f t subtree
right ; // the r i g h t subtree
Lis Palle
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ; Sarah Jens Else Christian
this . right = r ;
} }
Takes time proportional with the size of the tree in the worst case.
f u n c t i o n findValInTree ( v , t r e e ) {
i f ( t r e e === n u l l ) r e t u r n false ;
i f ( t r e e . d a t a === v ) {
r e t u r n true ;
} else {
r e t u r n findValInTree ( v , t r e e . l e f t ) | |
findValInTree ( v , t r e e . r i g h t ) ;
}
}
f i n d V a l I n T r e e ( ” C h r i s t i a n ” , ken ) ; // true
f i n d V a l I n T r e e ( ” John ” , ken ) ; // false
What is a Binary Search Tree?
A binary search tree is a binary tree with the additional properties:
1 The left subtree of any node contains only nodes with keys
lesser than that node’s key.
2 The right subtree of any node contains only nodes with keys
greater than that node’s key.
3 The left and right subtrees of any node must also be binary
search trees.
How to Build a Binary Search Tree?
Building a Binary Search Tree
c l a s s B i n Tr e e {
data ; // t h e d a t a s t o r e d i n t h i s node
left ; // the l e f t subtree
right ; // the r i g h t subtree
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ;
this . right = r ;
} }
f u n c t i o n insertInBST ( v , t r e e ) {
i f ( t r e e === n u l l ) { / / base case
r e t u r n new B i n Tr e e ( v , n u l l , n u l l ) ;
}
i f ( t r e e . d a t a === v ) { / / base case
return tree ;
}
i f ( t r e e . data > v ) {
t r e e . l e f t = insertInBST ( v , t r e e . l e f t ) ; // recursion
} else { / / ( t r e e . data < v )
t r e e . r i g h t = insertInBST ( v , t r e e . r i g h t ) ; / / r e c u r s i o n
}
return tree ;
}
Building a Binary Search Tree
c l a s s B i n Tr e e {
data ; // t h e d a t a s t o r e d i n t h i s node
left ; // the l e f t subtree
right ; // the r i g h t subtree
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ;
this . right = r ;
} }
constructor ( d , l , r ) {
t h i s . data = d ;
this . left = l ;
this . right = r ;
} }
i f ( t r e e . d a t a > v ) { r e t u r n findValInBST ( v , t r e e . l e f t ) ; } / / r e c u r s i o n
else { r e t u r n findValInBST ( v , t r e e . r i g h t ) ; } / / r e c u r s i o n
}
fi nd Val In BST ( 4 8 , bst ) ; // true
fi nd Val In BST ( 2 9 , bst ) ; // false
How to Represent a Tree (Not Binary)
c l a s s Tr e e {
data ; / / t h e d a t a s t o r e d i n t h i s node
children ; / / an a r r a y o f c h i l d r e n n o d e s
c o n s t r u c t o r ( d , ch s ) {
t h i s . data = d;
t h i s . c h i l d r e n = ch s ;
} }
f i l e 1 = new T e x t f i l e ( ” f i l e 1 . t x t ” , ”Some t e x t 1 ” ) ;
f i l e 2 = new T e x t f i l e ( ” f i l e 2 . t x t ” , ”Some t e x t 2 ” ) ;
workd = new F o l d e r ( ” Work ” , []);
r o o t = new F o l d e r ( ”home ” , [ f i l e 1 , workd , f i l e 2 ] ) ;