The Tree ADT
The Tree ADT
Algorithms
Trees
T1 T2 T3 T4 … T10
Preliminaries
From the recursive definition, we find that a tree is a collection of N nodes, one of which
is the root, and N-1 edges. That there are N-1 edges follows from the fact that each
edge connects some node to its parent, and every node except the root has one parent.
A
The root is A.
Node E has A as a parent and I, J as
children.
Each node may have an arbitrary
B C D E F G number of children, possibly zero.
Nodes with no children are known as
leaves.
H I J
Nodes with the same parent are
siblings.
Grandparent and grandchild
relations can be defined in a similar
K L
manner.
Preliminaries
A path from node n1 to nk is defined as a sequence of
nodes n1, n2, n3…, nk such that ni is the parent of ni+1 for
1i<k.
B C D E F G
H I J
K L
Implementation of Trees
One way to implement a tree would be to have in
each node, besides its data, a pointer to each child of
the node.
B C D E F G B C D E F G
H I J H I J
K L K L
B C D E
F G H I J
K L
Application of Trees
There are many applications for trees. One of the popular
uses is the directory structure in many common operating
systems.
/usr*
B C D E
F G H I J
K L
Tree Traversal
Answer:
Preorder traversal: A B F G C H K L D E I J
Postorder traversal: F G B K L H C D I J E A
Tree Traversal
Example: Suppose A
the operation on
each node is print
the name of this B C D
node. Please give
the outputs of the
preorder and E
postorder traversals F G
on the left tree.
H I J
K L
Tree Traversal
Answer:
Preorder traversal: A B C E H I J K L D F G
Postorder traversal: B H I K L J E C F G D A
Tree Traversal
Write codes to implement the preorder and postorder
tree traversal.
Binary Trees
A binary tree is a tree in which no node can have more than two children.
A property of a binary tree that is sometimes important is that the depth of an average binary tree is
considerably smaller than N if the tree has N nodes.
root
B
C
TL TR
L
Binary Tree Traversal
Answer:
Preorder traversal: A B D E G I J L H K C F
Postorder traversal: D I L J G K H E B F C A
Inorder traversal: D B I G L J E H K A C F
Binary Tree Traversal
Example: Suppose A
the operation on
B
each node is print C
the name of this
node. Please give D G
H
the outputs of the
preorder, postorder,
and inorder E I J K
traversals on the left
tree.
L
Binary Tree Traversal
Answer:
Preorder traversal: A B D E C G I J L H K
Postorder traversal: E D B I L J G K H C A
Inorder traversal: D E B A I G L J C H K
Binary Tree Traversal
Write codes to implement the preorder, postorder,
and inorder binary tree traversal.
Expression Trees
One of the principal uses of binary trees is in the area of
compiler design.
Expression tree for (a+b*c)+((d*e+f)*g)
+ The leaves of an expression tree are operands,
such as constants or variable names
The other nodes contain operators.
This particular tree happens to be binary, because
all of the operations are binary
+ * It is possible for nodes to have more than two
children. It is also possible for a node to have only
one child, such as unary minus operator
a *
We can evaluate an expression tree, T, by applying
+ g the operator at the root to the values obtained by
recursively evaluating the left and right subtrees.
In this example, the left subtree evaluates to a+
(b*c) and the right subtree evaluates to ((d*e)
b c * f +f)*g. The entire tree therefore represents (a+
(b*c))+(((d*e)+f)*g).
d e
Expression Trees
We can produce an (overly parenthesized) infix expression by
recursively producing a parenthesized left expression, then
printing out the operator at the root, and finally recursively
producing a parenthesized right expression. This general strategy
(left, node, right) is an inorder traversal;
A third traversal strategy is to print out the operator first and then
recursively print out the left and right subtrees. The resulting
expression, ++a*bc*+*defg, is the less useful prefix notation and
the traversal strategy (node, left, right) is a preorder traversal.
Constructing an Expression
Tree
We now give an algorithm to convert a postfix
expression into an expression tree.
a b
Next, c, d, and e are read, and for each a one-node tree is created a b
and a pointer to the corresponding tree is pushed onto the stack.
+ c d e
a b
Constructing an Expression
Tree
Now a ‘+’ is read, so two trees are merged.
+ c +
a b d e
Continuing, a ‘*’ is read, so we pop two tree pointers and form
a new tree with a ‘*’ as root.
+ *
a b c +
d e
Constructing an Expression
Tree
Finally, the last symbol is read, two trees are merged, and a
pointer to the final tree is left on the stack.
+ *
a b c +
d e
Constructing an Expression
Tree
Write codes to implement the process of constructing an
expression tree.
The Search Tree ADT-Binary
Search Trees
An important application of binary trees is their use in
searching.
Let us assume that each node in the tree is assigned a
key value. We will also assume that all the keys are
distinct.
The property that makes a binary tree into a binary
search tree is that for every node, X, in the tree, the
values of all the keys in its left subtree are smaller
than the key value in X, and the values of all the keys
in its right subtree are larger than the key value in X.
Notice that this implies that all the elements in the
tree can be ordered in some consistent manner.
The Search Tree ADT-Binary
Search Trees
6 6
2 8 2 8
1 4 1 4
3 3 7
6 6
2 8 2 8 Binary search
trees before
1 4 1 4
and after
inserting 5.
3 3 5
The Search Tree ADT-Binary
Search Trees
(5) Delete: Once we have found the node to be deleted, we need
to consider several possibilities.
(a) If the node is a leaf, it can be deleted immediately.
(b) If the node has one child, the node can be deleted after its
parent adjusts a pointer to bypass the node.
6 6
Deletion of a
2 8 2 8 node (4) with
one child,
1 4 1 4 before and
after.
3 3
The Search Tree ADT-Binary
Search Trees
(c) The complicated case deals with a node with two children. The
general strategy is to replace the data of this node with the
smallest data of the right subtree and recursively delete that
node. Because the smallest node in the right subtree cannot
have a left child, the second Delete is an easy one.
6 6
3 3
4 4
The Search Tree ADT-Binary
Search Trees
Write codes to implement the previous operations.
Binary Search Tree Traversals
Inorder traversal
Preorder traversal
Postorder traversal
Homework
Exercises
4.1
4.2
4.3
4.8
4.9
4.39