Title: Data Structures
1Data Structures
Andreas Savva
2Lists vs Trees
- Lists have great advantages of flexibility but
the have one week feature - They are sequential lists can move through them
only one position at a time. - Trees
- Valuable for a range of applications, especially
for problems of information retrieval. - The are not sequential.
3Binary Trees
- A binary tree is either empty or it consists of a
node called a root together with two binary trees
called the left subtree and the right subtree of
the root.
Amy Ann Dot Eva Guy Jan Jim Jon Kay Kim
Ron Roy Tim Tom
4Binary Trees
5Nodes
- Let x y mean that the root has x nodes on the
left and y nodes on the right, then
6Traversal of Binary Trees
- Traversal means moving through all the nodes
visiting each one in turn. - It is one of the most important operations on
binary trees. - There are many different orders in which all the
nodes could be traversed. - V visiting a node (root)
- L traversing the left subtree (left)
- R traversing the right subtree (right)
- VLR LVR LRV
- preorder inorder
postorder - VRL RVL RLV
mirror images
7Examples
- Preorder 1, 2, 3
- Inorder 2, 1, 3
- Postorder 2, 3, 1
Preorder 1, 5, 2, 4, 3 Inorder 5, 1, 4, 2,
3 Postorder 5, 4, 3, 2, 1
Preorder 1, 2, 3, 4, 5 Inorder 1, 4, 3, 5,
2 Postorder 4, 5, 3, 2, 1
8Examples (continue)
Preorder Jim, Dot, Amy, Ann, Guy, Eva, Jan, Ron,
Kay, Jon, Kim, Tim, Roy, Tom Inorder Amy, Ann,
Dot, Eva, Guy, Jan, Jim, Jon, Kay, Kim, Ron, Roy,
Tim, Tom Postorder Ann, Amy, Eva, Jan, Guy, Dot,
Jon, Kim, Kay, Roy, Tom, Tim, Ron, Jim
9Expression Trees
Preorder a b Inorder a b Postorder a b
Preorder log x Inorder log x Postorder x log
Preorder ! n Inorder n ! Postorder n !
Preorder a b c Inorder a b
c Postorder a b c
Preorder or lt a b lt c d Inorder a lt b or c lt
d Postorder a b lt c d lt or
10- Related to the Polish form
- Preorder Prefix
- Inorder Infix
- Postorder Postfix
Preorder X / b ? ? b 2
4 a c 0.5 2 a Inorder X ( b ( b ?
2 4 a c) ? 0.5 ) / ( 2 a) Postorder X b
b 2 ? 4 a c 0.5 ? 2 a
/
11The Node Implementation
templateltclass Entrygt struct Node // data
members Entry entry NodeltEntrygt left,
right // constructors Node()
Node(const Entry x) templateltclass
Entrygt NodeltEntrygtNode() left right
NULL templateltclass Entrygt NodeltEntrygtNode(c
onst Entry x) entry x left right
NULL
12Linked Implementation of Binary Trees
13The Tree Implementation
template ltclass Entrygt class Tree
public Tree() void clear() bool empty()
const int size() const int height()
const void preorder(void (visit)(Entry
)) void inorder(void (visit)(Entry )) void
postorder(void (visit)(Entry )) Error_code
insert(const Entry x) Error_code remove(const
Entry x) // Safequards Tree() Tree(const
TreeltEntrygt original) void operator (const
TreeltEntrygt original) protected int
size_recursive(NodeltEntrygt sub_root) const int
height_recursive(NodeltEntrygt sub_root)
const void recursive_preorder(NodeltEntrygt
sub_root, void(visit)(Entry )) void
recursive_inorder(NodeltEntrygt sub_root,
void(visit)(Entry )) void recursive_postorder(
NodeltEntrygt sub_root, void(visit)(Entry
)) Error_code insert_recursive(NodeltEntrygt
subroot, const Entry x) Error_code
remove_recursive(NodeltEntrygt subroot, const
Entry x) Error_code remove_root(NodeltEntrygt
subroot) NodeltEntrygt root
14Create Tree
Constructor
- template ltclass Entrygt
- TreeltEntrygtTree()
-
- root NULL
15Empty
template ltclass Entrygt bool TreeltEntrygtempty()
const return root NULL
16Size
template ltclass Entrygt int TreeltEntrygtsize_recur
sive(NodeltEntrygt sub_root) const if
(sub_root NULL) return 0 else return 1
size_recursive(sub_root-gtleft)
size_recursive(sub_root-gtright)
template ltclass Entrygt int TreeltEntrygtsize()
const return size_recursive(root)
17Height
template ltclass Entrygt int TreeltEntrygtheight_rec
ursive(NodeltEntrygt sub_root) const if
(sub_root NULL) return 0 else return 1
max(height_recursive(sub_root-gtleft),
height_recursive(sub_ro
ot-gtright)) template ltclass Entrygt int
TreeltEntrygtheight() const return
height_recursive(root)
18Preorder
template ltclass Entrygt void TreeltEntrygtrecursive
_preorder(NodeltEntrygt sub_root,
void(visit)(Entry )) if (sub_root !
NULL) (visit)(subroot-gtentry)
recursive_preorder(sub_root-gtleft, visit)
recursive_preorder(sub_root-gtright,
visit) template ltclass Entrygt void
TreeltEntrygtpreorder(void(visit)(Entry ))
recursive_preorder(root, visit)
19Traverse Preorder
void print(int x) cout ltlt x ltlt endl
the_tree.preorder(print)
20Inorder
template ltclass Entrygt void TreeltEntrygtrecursive
_inorder(NodeltEntrygt sub_root,
void(visit)(Entry )) if (sub_root !
NULL) recursive_inorder(sub_root-gtleft
, visit) (visit)(subroot-gtentry)
recursive_inorder(sub_root-gtright,
visit) template ltclass Entrygt void
TreeltEntrygtinorder(void(visit)(Entry ))
recursive_inorder(root, visit)
21Postorder
template ltclass Entrygt void TreeltEntrygtrecursive
_postorder(NodeltEntrygt sub_root,
void(visit)(Entry )) if (sub_root !
NULL) recursive_postorder(sub_root-gtle
ft, visit) recursive_postorder(sub_root
-gtright, visit) (visit)(subroot-gtentr
y) template ltclass Entrygt void
TreeltEntrygtpostorder(void(visit)(Entry ))
recursive_postorder(root, visit)
22The Class Tree
template ltclass Entrygt class Tree public . .
. . . void preorder() void inorder() void
postorder() . . . . . protected . . . .
. void recursive_preorder(NodeltEntrygt
sub_root) void recursive_inorder(NodeltEntrygt
sub_root) void recursive_postorder(NodeltEntrygt
sub_root) . . . . . NodeltEntrygt root
23Display Preorder
template ltclass Entrygt void TreeltEntrygtrecursive
_preorder(NodeltEntrygt sub_root) if
(sub_root ! NULL) cout ltlt
subroot-gtentry ltlt
recursive_preorder(sub_root-gtleft)
recursive_preorder(sub_root-gtright)
template ltclass Entrygt void
TreeltEntrygtpreorder() recursive_preorder(
root)
24Display Inorder
template ltclass Entrygt void TreeltEntrygtrecursive
_inorder(NodeltEntrygt sub_root) if
(sub_root ! NULL) recursive_inorder(s
ub_root-gtleft) cout ltlt subroot-gtentry
ltlt recursive_inorder(sub_root-gtrig
ht) template ltclass Entrygt void
TreeltEntrygtinorder() recursive_inorder(ro
ot)
25Display Postorder
template ltclass Entrygt void TreeltEntrygtrecursive
_postorder(NodeltEntrygt sub_root) if
(sub_root ! NULL) recursive_postorder
(sub_root-gtleft) recursive_postorder(su
b_root-gtright) cout ltlt subroot-gtentry
ltlt template ltclass Entrygt void
TreeltEntrygtpostorder()
recursive_postorder(root)
26Insert into a Binary Search Tree
- Insert e, b, d, f, a, g, c
Insert 6, 3, 8, 1, 4, 7, 5, 9, 2
e
6
- Exercises
- Insert b, d, f, a, g, c, e
- Insert d, c, a, f, g, b, e
- Insert 1, 2, 3, 4, 5, 6, 7, 8, 9
- Insert 8, 12, 3, 5, 1, 15, 4, 10, 6, 13, 7, 20,
18, 2, 9, 14
27Insert
template ltclass Entrygt Error_code
TreeltEntrygtinsert_recursive(NodeltEntrygt
sub_root, const Entry x) if (sub_root
NULL) NodeltEntrygt temp new
NodeltEntrygt(x) if (temp NULL)
return overflow sub_root temp
return success else if (x lt
sub_root-gtentry) return
insert_recursive(sub_root-gtleft, x) else
return insert_recursive(sub_root-gtright,
x) template ltclass Entrygt Error_code
TreeltEntrygtinsert(const Entry x)
return insert_recursive(root, x)
28Remove
29Remove
30Remove
5
2
31Remove
8
32Remove
template ltclass Entrygt Error_code
TreeltEntrygtremove_recursive(NodeltEntrygt
sub_root, const Entry x) if (sub_root
NULL) return not_found if
(sub_root-gtentry x) return
remove_root(sub_root) else if (x lt
sub_root-gtentry) return
remove_recursive(sub_root-gtleft, x) else
return remove_recursive(sub_root-gtright,
x) template ltclass Entrygt Error_code
TreeltEntrygtremove(const Entry x) return
remove_recursive(root, x)
33Remove Root
template ltclass Entrygt Error_code
TreeltEntrygtremove_root(NodeltEntrygt sub_root)
NodeltEntrygt to_delete sub_root //
remember to delete at end if
(sub_root-gtrightNULL) sub_root
sub_root-gtleft else if (sub_root-gtleft
NULL) sub_root sub_root-gtright else
// neither subtree is empty. to_delete
sub_root-gtleft // Move left to find
predecessor NodeltEntrygt parent
sub_root // Parent of to_delete while
(to_delete-gtright ! NULL) // to_delete is not
the predecessor parent
to_delete to_delete
to_delete-gtright
sub_root-gtentry to_delete-gtentry // Move from
to_delete to root if (parent
sub_root) sub_root-gtleft to_delete-gtleft
else parent-gtright to_delete-gtleft
delete to_delete return success