10.09.2013 Views

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LECTURE NOTES OF ADVANCED DATA STRUCTURE (MT-CSE 110)<br />

comparator that set up the binary search tree (hence the name).To see why this<br />

is the case, note that if n is a node in a binary search tree, then everything in n 's<br />

left subtree is less than n, and everything in n 's right subtree is greater than or<br />

equal to n. Thus, if we visit the left subtree in order, <strong>using</strong> a recursive call, and<br />

then visit n, and then visit the right subtree in order, we have visited the entire<br />

subtree rooted at n in order. We can assume the recursive calls correctly visit<br />

the subtrees in order <strong>using</strong> the mathematical principle of structural induction.<br />

Traversing in reverse inorder similarly gives the values in decreasing order.<br />

Preorder traversal<br />

Traversing a tree in preorder while inserting the values into a new tree is<br />

common way of making a complete copy of a binary search tree.One can also<br />

use preorder traversals to get a prefix expression (Polish notation) from<br />

expression trees: traverse the expression tree preorderly. To calculate the value<br />

of such an expression: scan from right to left, placing the elements in a stack.<br />

Each time we find an operator, we replace the two top symbols of the stack<br />

with the result of applying the operator to those elements. For instance, the<br />

expression + 2 3 4, which in infix notation is (2 + 3) 4, would be evaluated<br />

like this:<br />

Using prefix traversal to evaluate an expression tree<br />

Expression (remaining) Stack<br />

+ 2 3 4 <br />

Prepared By :­<br />

Er. Harvinder Singh<br />

Assist Prof., CSE, H.C.T.M (Kaithal) Page ‐ 197 ‐

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!