SKP's Algorithms and Data Structures #2
I Hereby Post My Version Of The Inorder, Preorder, And Postorder Code In C++. Please Provide Your Valuable Feedback Or Additions To The Code.
Join the DZone community and get the full member experience.
Join For FreeMy Article Series on Algorithms and Data Structures in a Sort of 'Programming Language Agnostic Way'. Few of the Algorithms and Data Structures in C, Few in C++, and Others in Core Java. Assorted Collection for Learning, Revising, Revisiting, Quick Refresh, and a Quick Glance for Interviews. You May Even Include them Directly for Professional or Open Source Efforts. Have Included Explanation Only for Few of These! Hope these turn out to be Really Helpful as per the Author's Intention.
Binary Search Tree Traversal Algorithms
I Hereby Post My Version Of The Inorder, Preorder, And Postorder Code In C++. Please Provide Your Valuable Feedback Or Additions To The Code.
I Am Also Providing A Simple Explanation Here Along With The Download For An Explanation Of How To Perform These Simple Tree Traversals. Please Provide Your Comments On The Documentation As Well.
In A Binary Search Tree, Every Node Has The Left Subtree With Elements Lesser Than Itself And The Right Subtree Withe Elements Greater Than (Or Equal) To Itself. In A BST, The Tree Can Be Traversed Using The Following Mechanisms.
For A Set Of Elements, That Are Inserted As Mentioned Below, The Inorder Tree Traversal Algorithms Is As Follows. It Is Recursive In Nature And Can Start At Root Always.
1. Traverse the Left Subtree
2. Print the Node
3. Traverse the Right Subtree
Postorder Tree Traversal - C++
Postorder Is A Similar Algorithm In Nature - Except That The Traversal Order Is Different.
1. Traverse the Left Subtree
2. Traverse the Right Subtree
3. Print the Node
http://bit.do/skp-postorder
Preorder Tree Traversal - C++
Whenever We Want To Traverse The Node Before The Two Subtrees, We Use Pre-Order Traversal.
1. Print the Node
2. Traverse the Left Subtree
3. Traverse the Right Subtree
http://bit.do/skp-preorder
We Use the Following Insertion Order for All Algorithms Above: 9 7 6 1 3 5 4 2 8
You Can Run the C++ Code to See the Output Yourself. It is Left as an Exercise (or Direct Code Reuse) for the User.
Published at DZone with permission of Sumith Puri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments