0% found this document useful (0 votes)
49 views

CS122 Algorithms and Data Structures Binary Search Trees

The document discusses binary search trees and binary expression trees. It provides information on using binary search trees for storing and retrieving information efficiently. It also discusses how binary expression trees can be used to represent arithmetic expressions with operands as leaves and operators as internal nodes. The document describes different tree traversal algorithms like inorder, preorder and postorder traversals and how they produce different representations of an expression. It also provides pseudocode for constructing an expression tree from a postfix expression in linear time using a stack.

Uploaded by

Napster
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

CS122 Algorithms and Data Structures Binary Search Trees

The document discusses binary search trees and binary expression trees. It provides information on using binary search trees for storing and retrieving information efficiently. It also discusses how binary expression trees can be used to represent arithmetic expressions with operands as leaves and operators as internal nodes. The document describes different tree traversal algorithms like inorder, preorder and postorder traversals and how they produce different representations of an expression. It also provides pseudocode for constructing an expression tree from a postfix expression in linear time using a stack.

Uploaded by

Napster
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

CS122 Algorithms and Data Structures


MW 11:00 am - 12:15 pm, MSEC 101
Instructor: Xiao Qin
Lecture 10: Binary Search Trees and
Binary Expression Trees
Uses for Binary Trees
-- Binary Search Trees
n Use for storing and retrieving
information
n Insert, delete, and search faster than
with a linked list
n Take advantage of logn height
n Idea: Store information in an ordered
way (keys)
A Property of Binary Search Trees
n The key of the root is larger than any
key in the left subtree
n The key of the root is smaller than any
key in the right subtree
n Note: Duplicated keys are not allowed
Traversing Binary Search Trees
n There are three ways to traverse a
binary tree
n Inorder Traversal:
Visit left subtree;
Visit root;
Visit right subtree;
Traversing Binary Search Trees (cont.)
n An Example:
void inorder_print(Node *root) {
if (root != NULL) {
inorder_print(root->left_child);
count << root->info;
inorder_print(root->right_child);
}
}
Searching a Binary Search Trees
n Locate an element in a binary search tree
void search(Node *root, object key) {
if (root == NULL) return -1;
if (root->info == key) return root->info;
else {
if (key < root->info)
search(root->left_child, key);
else search(root->right_child, key);
}
}
2
Inserting an Element in a Binary
Search Trees
n Search for the Position in the tree where
the element would be found
n Insert the element in the position
n Note: a newly inserted node is a leaf
n Running time is:
O(n) the worst case
O(lgn) if the tree is balanced
Uses for Binary Trees
-- Binary Expression Trees
n Binary trees are a good way to express
arithmetic expressions.
The leaves are operands and the other
nodes are operators.
The left and right subtrees of an operator
node represent subexpressions that must
be evaluated before applying the operator
at the root of the subtree.
Binary Expression Trees: Examples
a + b
+
a b
- a
-
a
(a + b) * (c - d) f (e + f)
/
+
a
b
-
c
d
+
e
f
*
/
Merits of Binary Tree Form
n Left and right operands are easy to visualize
n Code optimization algorithms work with the
binary tree form of an expression
n Simple recursive evaluation of expression
+
a b
-
c d
+
e f
*
/
11
Levels Indicate Precedence
The levels of the nodes in the tree indicate
their relative precedence of evaluation (we
do not need parentheses to indicate precedence).
Operations at lower levels of the tree are
evaluated later than those at higher
levels.
The operation at the root is always the last
operation performed.
12
A Binary Expression Tree
What value does it have?
( 4 + 2 ) * 3 = 18
*
+
4
3
2
3
13
Inorder Traversal: (A + H) / (M - Y)
/
+
A H
-
M Y
tree
Print left subtree first Print right subtree last
Print second
Inorder Traversal (cont.)
a
+
*
b c
+
*
+
g
*
d
e
f
Inorder traversal yields: (a + (b * c)) + (((d * e) + f) * g)
15
Preorder Traversal: / + A H - M Y
/
+
A H
-
M Y
tree
Print left subtree second Print right subtree last
Print first
Preorder Traversal (cont.)
a
+
*
b c
+
*
+
g
*
d
e
f
Preorder traversal yields: (+ (+ a (* b c)) (* (+ (* d e) f) g))
17
/
+
A H
-
M Y
tree
Print left subtree first Print right subtree second
Print last
Postorder Traversal: A H + M Y - /
Postorder Traversal (cont.)
a
+
*
b c
+
*
+
g
*
d
e
f
Postorder traversal yields: a b c * + d e * f + g * +
4
Traversals and Expressions
n Note that the postorder traversal produces the
postfix representation of the expression.
n Inorder traversal produces the infix
representation of the expression.
n Preorder traversal produces a representation
that is the same as the way that the
programming language Lisp processes
arithmetic expressions!
20
class ExprTree
ExprTree
*
+
4
3
2
ExprTree
~ExprTree
build
expression
private:
root
evaluate
clear
class ExprTree {
public:
ExprTree ( ); // Constructor
~ExprTree ( ); // Destructor
void build ( ); // build tree from prefix expression
void expression ( ) const;
// output expression in fully parenthesized infix form
float evaluate ( ) const; // evaluate expression
void clear ( ); // clear tree
void showStructure ( ) const; // display tree
private:
void showSub( );
// recursive partners
struct TreeNode *root;
};
22
Each node contains two pointers
struct TreeNode
{
InfoNode info ; // Data member
TreeNode* left ; // Pointer to left child
TreeNode* right ; // Pointer to right child
};
left info right
NULL 6000
whichType operand
OPERAND 7
23
InfoNode has 2 forms
enum OpType { OPERATOR, OPERAND } ;
struct InfoNode
{
OpType whichType;
union // ANONYMOUS union
{
char operation ;
int operand ;
}
};
whichType operation
OPERATOR +
whichType operand
OPERAND 7
int Eval ( TreeNode* ptr )
{ switch ( ptr->info.whichType )
{
case OPERAND : return ptr->info.operand ;
case OPERATOR :
switch ( tree->info.operation )
{
case + : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ;
case - : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ;
case * : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ;
case / : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ;
}
}
}
24
5
Constructing an Expression Tree
n There is a simple O(N) stack-based
algorithm to convert a postfix
expression into an expression tree.
n Recall we also have an algorithm to
convert an infix expression into postfix,
so we can also convert an infix
expression into an expression tree
without difficulty (in O(N) time).
Expression Tree Algorithm
n Read the postfix expression one symbol at at
time:
If the symbol is an operand, create a one-node
tree and push a pointer to it onto the stack.
If the symbol is an operator, pop two tree pointers
T1 and T2 from the stack, and form a new tree
whose root is the operator, and whose children
are T1 and T2.
Push the new tree pointer on the stack.
Example
a b + :
Note: These stacks are depicted horizontally.
a b
+
b a
Example
a b + c d e + :
+
b a
c d e
+
b a
c
d e
+
Example
a b + c d e + * :
+
b a c
d e
+
*
Example
a b + c d e + * * :
+
b a c
d e
+
*
*

You might also like