Topic 4
Topic 4
Binary Trees
1. Binary Tree
2. Binary Search Tree
3. AVL Tree
4. B-Tree
In an ordered binary tree,
The keys of all the nodes in the left sub-tree are less than that of the root,
The keys of all the nodes in the right sub-tree are greater than that of the
root,
The left and right sub-trees are themselves ordered binary trees.
Binary Search Tree Construction
Maintain key property (invariant) Search will end at node Y (if X not in
• Smaller values in left subtree tree)
• Larger values in right subtree
Binary Search Tree – Insertion If X < Y, insert new leaf X as new left
subtree for Y
If X > Y, insert new leaf X as new right
subtree for Y
Operator treated as the node/parent
Operand – the leaf
Binary Search Tree – Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
Replace with largest value Y on left subtree OR smallest value Z on right
subtree.
Delete replacement value (Y or Z) from subtree
Observation
O( log(n) ) operation for balanced tree
Deletions may unbalance tree
Example 1
Example 2
Example 3
Tree traversal methods
Walk, pre-order and post-order
Traversing a tree means visiting every node in the tree. You might, for instance,
want to add all the values in the tree or find the largest one. For all these
operations, you will need to visit each node of the tree.
pre-order walk : A walk in which each parent node is traversed before its children
post-order walk: a walk in which the children are traversed before their
respective parents are traversed
post-order Traversal
post-order walk: a walk in which the children are traversed before their
respective parents are traversed
pre-order traversal
pre-order walk : A walk in which each parent node is traversed before its
children
Depth First Search or Level Order
Traversal
BFS (Breadth-First Search) is a vertex-based method for determining the
shortest path in a graph.
In-order traversal :
a walk in which a node's left subtree, then the node itself, and then finally its
right subtree are traversed
(This last scenario, referring to exactly two subtrees, a left subtree and a
right subtree, assumes specifically a binary tree.)
Steps of Inorder traversal
First, visit all the nodes in the left subtree
Then the root node
Visit all the nodes in the right subtree
In-order Traversal
Consider:
Show NLR, LNR, LRN and Level traversal
for the tree
Solution:
Contd…
Let us see how the above algorithm will be imlemented using an example.
Initially the Stack is empty and our Postfix string has no characters. Now, the
first character scanned is 'a'. 'a' is added to the Postfix string. The next
Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which
is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*'
will be pushed to the stack.
·
If the scannned character is an operand, add it to the stack. If the scanned
character is an operator, there will be atleast two operands in the stack.
If the scanned character is an Operator, then we store the top most element
of the stack(topStack) in a variable temp. Pop the stack. Now evaluate
topStack(Operator)temp. Let the result of this operation be retVal. Pop the
stack and Push retVal into the stack.
After all characters are scanned, we will have only one element in the stack.
Return topStack.
Evaluating Postfix use the infix notation
Expression like a+b*c. The
corresponding postfix
Infix Expression:
notation is abc*+. The
Any expression in the algorithm for the
standard form like "2*3- conversion is as
4/5" is an Infix(Inorder) follows:
expression.
Scan the Postfix string
from left to right.
Postfix Expression: Initialise an empty
The Postfix(Postorder) stack.
form of the above
expression is "23*45/-".
Postfix Evaluation:
In normal algebra we
REVIEW QUESTIONS