Lesson 9 - Trees (Compatibility Mode)
Lesson 9 - Trees (Compatibility Mode)
6/3/2017
Tree Terminology
Root: node without parent (A)
Internal node: node with at least
one child (A, B, C, F)
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H,
D)
Ancestors of a node: parent,
grandparent, grand-grandparent,
etc.
Depth of a node: number of
ancestors
Height of a tree: maximum depth
of any node (3)
Descendant of a node: child,
grandchild, grand-grandchild,
etc.
Subtree: tree consisting of a
node and its descendants
6/3/2017
Parts of a Tree
nodes
6/3/2017
Parts of a Tree
parent
node
6/3/2017
Parts of a Tree
parent child
node nodes
6/3/2017
Parts of a Tree
parent child
node nodes
6/3/2017
Parts of a Tree
leaf
root node
nodes
6/3/2017
Parts of a Tree
sub-tree
6/3/2017
Parts of a Tree
sub-tree
6/3/2017
Parts of a Tree
sub-tree
6/3/2017
Binary Tree
• Each node can have at most 2 children
6/3/2017
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the expression
(2 × (a − 1) + (3 × b))
6/3/2017
Tree Traversals
Systematic
Visit each node in a tree.
Recursive
Repeat the process at each node.
6/3/2017
Tree Traversals
Preorder traversal:
Visit the current node
Visit the left subtree
Visit the right subtree
Inorder traversal:
Visit the left subtree
Visit the current node
Visit the right subtree
Postorder traversal:
Visit the left subtree
Visit the right subtree
Visit the current node
6/3/2017
Inorder Traversal
6/3/2017
Preorder Traversal
6/3/2017
Postorder Traversal
6/3/2017
Tree Traversal Exercise
M
J S
D L R U
T
A E
6/3/2017
Binary Search Tree
A Binary Tree such that:
Every node entry has a unique key.
All the keys in the left subtree of a
node are less than the key of the node.
All the keys in the right subtree of a
node are greater than the key of the
node.
6/3/2017
Example 1: key is an integer
43
31 64
20 40 56 89
28 33 47 59
6/3/2017
Insert
Create new node for the item.
Find a parent node.
Attach new node as a leaf.
6/3/2017
Insert
57
Example:
43
31 64
20 40 56 89
28 33 47 59
6/3/2017
Insert
57
Example:
43
31 64
20 40 56 89
28 33 47 59
57
6/3/2017
Search: Checklist
if target key is less than current node’s
key, search the left sub-tree.
else, if target key is greater than current
node’s key, search the right sub-tree.
returns:
if found, pointer to node containing target
key.
otherwise, NULL pointer.
6/3/2017
Search
Example: 59
43
31 64
20 40 56 89
28 33 47 59
57
found
6/3/2017 26
Search
Example: 61
43
31 64
20 40 56 89
28 33 47 59
57
failed
6/3/2017 27
Recall - Binary Search Tree
A Binary Tree such that:
Every node entry has a unique key.
All the keys in the left subtree of a
node are less than the key of the node.
All the keys in the right subtree of a
node are greater than the key of the
node.
6/3/2017
Example 1: key is an integer
43
31 64
20 40 56 89
28 33 47 59
6/3/2017
Example 2: key is a string
Fred
Dan Mary
6/3/2017
Binary Tree Node
entry
link to right
link to left
child node
child node
6/3/2017
Binary Search Tree Node
Example 1:
struct TreeNodeRec
{
int key;
6/3/2017
Binary Search Tree Node
Example 2:
#define MAXLEN 15
struct TreeNodeRec
{
char key[MAXLEN];
6/3/2017
Recall:
maximum
#define MAXLEN 15
string length
struct TreeNodeRec is fixed
{
char key[MAXLEN];
6/3/2017
Recall: •Allows strings of
arbitrary length.
•Memory needs to be
allocated dynamically
before use.
struct TreeNodeRec
{ •Use strcmp to
char* key; compare strings.
6/3/2017
6/3/2017
Book Record
struct BookRec
{ key
char* author;
char* title;
char* publisher;
6/3/2017
Example 4: Binary Search Tree Node
struct TreeNodeRec
{
Book info;
6/3/2017
Tree Node
struct TreeNodeRec
{
float key;
6/3/2017
#ifndef TREEH
#define TREEH
struct TreeNodeRec
{
float key;
struct TreeNodeRec* leftPtr;
struct TreeNodeRec* rightPtr;
};
#endif
6/3/2017
MakeNode
parameter: item to be inserted
steps:
allocate memory for the new node
check if memory allocation is successful
if so, put item into the new node
set left and right branches to NULL
returns: pointer to (i.e. address of) new
node
6/3/2017
TreeNode* makeTreeNode(float value)
{
TreeNode* newNodePtr = NULL;
newNodePtr = (TreeNode*)malloc(sizeof(TreeNode));
if (newNodePtr == NULL)
{
fprintf(stderr, “Out of memory\n”);
exit(1);
}
else
{
newNodePtr->key = value;
newNodePtr->leftPtr = NULL;
newNodePtr->rightPtr = NULL;
}
return newNodePtr;
}
6/3/2017
value 3.3
newNodePtr NULL
NULL NULL
6/3/2017
Inorder
Inorder traversal of a Binary Search Tree
always gives the sorted order of the keys.
6/3/2017
Inorder
Inorder traversal of a Binary Search Tree
always gives the sorted order of the keys.
6/3/2017
Inorder
nodePtr
43
31 64
20 40 56 89
28 33 47 59
57
found
6/3/2017 70
Search
Example: 61
43
31 64
20 40 56 89
28 33 47 59
57
failed
6/3/2017 71
Search: Checklist
if target key is less than current node’s
key, search the left sub-tree.
else, if target key is greater than current
node’s key, search the right sub-tree.
returns:
if found, or if target key is equal to current
node’s key, a pointer to node containing target
key.
otherwise, NULL pointer.
6/3/2017
TreeNode* search(TreeNode* nodePtr, float target)
{
if (nodePtr != NULL)
{
if (target < nodePtr->key)
{
nodePtr = search(nodePtr->leftPtr, target);
}
else if (target > nodePtr->key)
{
nodePtr = search(nodePtr->rightPtr, target);
}
}
return nodePtr;
}
6/3/2017
Function Call to Search
/* …and so on… */
6/3/2017
Search Find 0.7
nodePtr 1.0
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
43
31 64
20 40 56 89
28 33 47 59
6/3/2017
Insert
57
Example:
43
31 64
20 40 56 89
28 33 47 59
57
6/3/2017
Insert
Create new node for the item.
Find a parent node.
Attach new node as a leaf.
6/3/2017
Insert: Recursive
parameters:
pointer to current node (initially: root node).
item to be inserted.
If current node is NULL
Create a new node and return it.
Else if item’s key is less (greater) than
current node’s key:
otherwise, let the left (right) child node be the
current node, setting the parent left (right) link
equal that node, and repeat recursively.
6/3/2017
TreeNode* insert(TreeNode* nodePtr, float item)
{
if (nodePtr == NULL)
{
nodePtr = makeTreeNode(item);
}
else if (item < nodePtr->key)
{
nodePtr->leftPtr = insert(nodePtr->leftPtr, item);
}
else if (item > nodePtr->key)
{
nodePtr->rightPtr = insert(nodePtr->rightPtr, item);
}
return nodePtr;
}
6/3/2017
Function Call to Insert
/* …and so on… */
6/3/2017
Insert Insert 0.9
nodePtr 1.0
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
0.6 1.9
6/3/2017
Sort
6/3/2017
Sort
1.0
6/3/2017
Sort
1.0
2.5
6/3/2017
Sort
1.0
0.5 2.5
6/3/2017
Sort
1.0
0.5 2.5
0.7
6/3/2017
Sort
1.0
0.5 2.5
0.7 3.6
6/3/2017
Sort
1.0
0.5 2.5
6/3/2017