Presentation is loading. Please wait.

Presentation is loading. Please wait.

Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals.

Similar presentations


Presentation on theme: "Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals."— Presentation transcript:

1

2 Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “.

3 A A B B C C D D E E F F G G H H I I J J K K L L NULL f r 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue A 3.Get node from the queue and print data, then add the left and right child to queue r f A Level Order Traversal of a Binary Tree

4 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue B 3.Get node from the queue and print data, then add the left and right child to queue r f A C r f 4.Go to step 3 until node is NULL B DE rr Level Order Traversal of a Binary Tree

5 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue 3.Get node from the queue and print data, then add the left and right child to queue A C f 4.Go to step 3 until node is NULL B DE rf C r FG fr D Level Order Traversal of a Binary Tree

6 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue 3.Get node from the queue and print data, then add the left and right child to queue A 4.Go to step 3 until node is NULL B E C FG fr D 5.If child node is NULL, skip and not to add to queue H rf E f F f Level Order Traversal of a Binary Tree

7 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue 3.Get node from the queue and print data, then add the left and right child to queue A 4.Go to step 3 until node is NULL B C D 5.If child node is NULL, skip and not to add to queue H r EF f G I r H ff I J r K r Level Order Traversal of a Binary Tree

8 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue 3.Get node from the queue and print data, then add the left and right child to queue A 4.Go to step 3 until node is NULL B C D 5.If child node is NULL, skip and not to add to queue EFGH f I JK rf J f K L r f L Level Order Traversal of a Binary Tree

9 A A B B C C D D E E F F G G H H I I J J K K L L NULL 1.Prepare a tree and a queue, f: front r:rear 2.Add root to queue 3.Get node from the queue and print data, then add the left and right child to queue A 4.Go to step 3 until node is NULL B C D 5.If child node is NULL, skip and not to add to queue EFGHIJK r f L If queue is empty, the node would get NULL and break the loop


Download ppt "Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals."

Similar presentations


Ads by Google