T r e e t r a v e r s a l s ( W e i s s 4 . 6 )
T r e e t r a v e r s a l T r a v e r s i n g a t r e e m e a n s v i s i t i n g a l l i t s n o d e s i n s o m e o r d e r Tii s c a n b e i m p l e m e n t e d b y r e c u r s i o n . F o r e x a m p l e , p r i n t i n g t h e v a l u e o f a l l n o d e s i n t h e t r e e : class Node<E> { E value; Node<E> left, right; } void printAll(Node<E> node) { if (node == null) return; // base case System.out.println(node.value); printAll(node.left); printAll(node.right); } Tii s k i n d o f t r a v e r s a l i s c a l l e d a p r e o r d e r t r a v e r s a l b e c a u s e w e v i s i t t h e n o d e b e f o r e w e r e c u r s e i n t o i t s c h i l d r e n .
I n o r d e r t r a v e r s a l I n a n i n o r d e r t r a v e r s a l w e fj r s t v i s i t t h e n o d e ’ s l e f t c h i l d , t h e n t h e n o d e i t s e l f , t h e n t h e n o d e ’ s r i g h t c h i l d . E x a m p l e : p r i n t i n g o u t t h e c o n t e n t s o f a b i n a r y s e a r c h t r e e i n s o r t e d o r d e r ! class Node<E> { E value; Node<E> left, right; } void printSorted(Node<E> node) { if (node == null) return; printSorted(node.left); System.out.println(node.value); printSorted(node.right); }
S o r t i n g a b i n a r y s e a r c h t r e e I f w e d o a n i n o r d e r t r a v e r s a l o f a B S T , w e v i s i t i t s e l e m e n t s i n s o r t e d o r d e r ! 2 . V i s i t r o o t o w l o w l 3 . V i s i t r i g h t s u b t r e e 1 . V i s i t l e f t p e n g u i n p e n g u i n h a m s t e r h a m s t e r s u b t r e e g o r i l l a l e m u r w o l f g o r i l l a l e m u r w o l f a p e h o r s e a p e h o r s e
P o s t o r d e r t r a v e r s a l I n a p o s t o r d e r t r a v e r s a l w e v i s i t e a c h n o d e ’ s c h i l d r e n b e f o r e v i s i t i n g t h e n o d e i t s e l f . E x a m p l e : c a l c u l a t i n g t h e h e i g h t o f a b i n a r y t r e e : int height(Node<E> node) { if (node == null) return 0; else { int leftHeight = height(node.left); int rightHeight = height(node.right); return 1 + Math.max(leftHeight, rightHeight); } }
S u m m a r y – t r a v e r s a l s T r e e t r a v e r s a l s c a p t u r e d i fg e r e n t p a t t e r n s o f r e c u r s i o n f o r v i s i t i n g a l l n o d e s i n a t r e e ● P r e o r d e r – v i s i t r o o t , t h e n r e c u r s i v e l y t r a v e r s e c h i l d r e n ● P o s t o r d e r – r e c u r s i v e l y t r a v e r s e c h i l d r e n , t h e n v i s i t r o o t ● I n o r d e r – r e c u r s i v e l y t r a v e r s e o n e c h i l d , t h e n v i s i t r o o t , t h e n r e c u r s i v e l y t r a v e r s e o t h e r c h i l d ( m a k e s s e n s e f o r e . g . 2 - 3 t r e e s t o o ) N o t h i n g s o p h i s t i c a t e d n e e d e d – a s i m p l e r e c u r s i v e f u n c t i o n d o e s t h e j o b L e s s c o m m o n : l e v e l - o r d e r t r a v e r s a l – v i s i t n o d e s i n o r d e r o f l e v e l i n t h e t r e e
Recommend
More recommend