Professional Documents
Culture Documents
not full!
• Height of a tree h: #levels = L
(Warning: some books define h
as #levels-1).
20
21
22
23
What is the total #nodes N
of a full tree with height h?
h 1
N 2 2 ... 2
0 1
2 1
h
l=0 l=1 l=h-1
2 1 N
h
2 N 1
h
h log( N 1) O(log N )
Why is h important?
• Tree operations (e.g., insert, delete, retrieve
etc.) are typically expressed in terms of h.
No O(N)
Binary Search Trees (BSTs)
• Binary Search Tree Property:
The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child
Binary Search Trees (BSTs)
Where is the
smallest element?
Ans: leftmost element
template<class ItemType>
struct TreeNode<ItemType> {
ItemType info;
TreeNode<ItemType>* left;
TreeNode<ItemType>* right;
};
Binary Search Tree Specification
#include <fstream.h>
struct TreeNode<ItemType>;
enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER};
template<class ItemType>
class TreeType {
public:
TreeType();
~TreeType();
TreeType(const TreeType<ItemType>&);
void operator=(const TreeType<ItemType>&);
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
int NumberOfNodes() const;
Binary Search Tree Specification
(cont.)
void RetrieveItem(ItemType&, bool& found);
void InsertItem(ItemType);
void DeleteItem(ItemType);
void ResetTree(OrderType);
void GetNextItem(ItemType&, OrderType, bool&);
void PrintTree(ofstream&) const;
private:
TreeNode<ItemType>* root;
};
Function NumberOfNodes
• Recursive implementation
#nodes in a tree =
#nodes in left subtree + #nodes in right subtree + 1
template<class ItemType>
void Retrieve(TreeNode<ItemType>* tree, ItemType& item, bool& found)
{
if (tree == NULL) // base case 2
found = false;
else if(item < tree->info)
Retrieve(tree->left, item, found);
Running Time?
else if(item > tree->info)
Retrieve(tree->right, item, found);
O(h)
else { // base case 1
item = tree->info;
found = true;
}
}
Function
InsertItem
• Use the
binary search
tree property
to insert the
new item at
the correct
place
Function
InsertItem
(cont.)
• Implementing
insertion
resursively
e.g., insert 11
Function InsertItem (cont.)
• What is the size of the problem?
Number of nodes in the tree we are examining
• What is the base case(s)?
The tree is empty
• What is the general case?
Choose the left or right subtree
Function InsertItem (cont.)
template<class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
{
Insert(root, item);
}
template<class ItemType>
void Insert(TreeNode<ItemType>*& tree, ItemType item)
{
if(tree == NULL) { // base case
tree = new TreeNode<ItemType>;
tree->right = NULL;
tree->left = NULL;
tree->info = item;
} Running Time?
else if(item < tree->info)
Insert(tree->left, item); O(h)
else
Insert(tree->right, item);
}
Function InsertItem (cont.)
Insert 11
Does the order of inserting
elements into a tree matter?
• Yes, certain orders might produce very
unbalanced trees!
Does the
order of
inserting
elements
into a tree
matter?
(cont.)
Does the order of inserting
elements into a tree matter?
(cont’d)
• Unbalanced trees are not desirable because
search time increases!
• Advanced tree structures, such as red-black
trees, guarantee balanced trees.
Function DeleteItem
‘J’
‘E’ ‘T’
Warning:
Warning "visit" implies do something with the value at
the node (e.g., print, save, update etc.).
Preorder Traversal: J E A H T M Y
Visit first
tree
‘J’
‘E’ ‘T’
‘J’
‘E’ ‘T’
ADJMQRT
Function PrintTree (cont.)
void TreeType::PrintTree(ofstream& outFile)
{
Print(root, outFile);
}
template<class ItemType>
void Print(TreeNode<ItemType>* tree, ofstream& outFile)
{
if(tree != NULL) {
Print(tree->left, outFile);
outFile << tree->info; // “visit”
Print(tree->right, outFile);
}
}
How should we
delete the nodes
of a tree?
Use postorder!
}
}
Copy Constructor
How should we
create a copy of
a tree?
Use preorder!
Copy Constructor (cont’d)
template<class ItemType>
TreeType<ItemType>::TreeType(const TreeType<ItemType>&
originalTree)
{
CopyTree(root, originalTree.root);
}
template<class ItemType)
void CopyTree(TreeNode<ItemType>*& copy,
TreeNode<ItemType>* originalTree)
{
if(originalTree == NULL)
copy = NULL;
else { preorder
copy = new TreeNode<ItemType>;
copy->info = originalTree->info;
// “visit”
CopyTree(copy->left, originalTree->left);
CopyTree(copy->right, originalTree->right);
}
}
ResetTree and GetNextItem
• User needs to specify the tree
traversal order.
• For efficiency, ResetTree stores in
a queue the results of the
specified tree traversal.
• Then, GetNextItem, dequeues the
node values from the queue.
void ResetTree(OrderType);
void GetNextItem(ItemType&,
OrderType, bool&);
Trees and
Binary Trees
Become Rich
branche
s root
Computer Scientist’s View
root
leaves
branches
nodes
•
What
A tree is a finite nonempty set
is a Tree
of elements.
• It is an abstract model of a
Computers”R”Us
hierarchical structure.
• consists of nodes with a
parent-child relation.
• Applications: Sales Manufacturing R&D
– Organization charts
– File systems
– Programming environments
US International Laptops Desktops
I J K
subtree
Tree Properties
Property Value
A Number of nodes
Height
C Root Node
B
Leaves
Interior nodes
Ancestors of H
D E F
Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
G
H I
Tree ADT
• We use positions to abstract nodes Query methods:
• Generic methods: boolean isInternal(p)
– integer size() boolean isExternal(p)
– boolean isEmpty() boolean isRoot(p)
– objectIterator elements() Update methods:
– positionIterator positions() swapElements(p, q)
• Accessor methods: object replaceElement(p, o)
– position root() Additional update methods may
– position parent(p) be defined by data structures
– positionIterator children(p) implementing the Tree ADT
Intuitive Representation of Tree Node
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of links to sub-trees
Data
• A node is represented by an
object storing
– Element
– Parent node B
– Sequence of children nodes
A D F
B
A D F
C E
C E
Left Child, Right Sibling Representation
Data
Left Right
Child Sibling A
B C D
E F G H I
J K L
Tree Traversal
• Two main methods:
– Preorder
– Postorder
• Recursive definition
• Preorder:
– visit the root
– traverse in preorder the children (subtrees)
• Postorder
– traverse in postorder the children (subtrees)
– visit the root
Preorder Traversal
• A traversal visits the nodes of a tree in a Algorithm preOrder(v)
systematic manner
visit(v)
• In a preorder traversal, a node is visited
for each child w of v
before its descendants
preorder (w)
• Application: print a structured document
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a CS 2.2 Start a 2.3 Acquired
Life Poor Friends PhD Web Site by Google
Postorder Traversal
• In a postorder traversal, a node is Algorithm postOrder(v)
visited after its descendants
for each child w of v
• Application: compute space used by
postOrder (w)
files in a directory and its
subdirectories visit(v)
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Binary Tree
• A binary tree is a tree with the following Applications:
properties: arithmetic expressions
– Each internal node has at most two children
(degree of two) decision processes
– The children of a node are an ordered pair searching
H I
BinaryTree ADT
• The BinaryTree ADT extends •
Update methods may be
the Tree ADT, i.e., it inherits defined by data structures
all the methods of the Tree implementing the BinaryTree
ADT ADT
• Additional methods:
– position leftChild(p)
– position rightChild(p)
– position sibling(p)
Examples of the Binary Tree
Skewed Binary Tree Complete Binary Tree
A 1 A
A
B B 2
B C
C
3 D E F G
D
4 H I
E 5
Differences Between A Tree and A Binary Tree
A A
B B
B A D
A D
C E
C E
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))
2 3 b
a 1
Decision Tree
• Binary tree associated with a decision process
– internal nodes: questions with yes/no answer
– external nodes: decisions
• Example: dining decision
Yes No
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
Maximum Number of Nodes in a
Binary Tree
Prove by induction.
k
2 i
i 0
2 k 1
1
Full Binary Tree
• A full binary tree of a given height k has 2k+1–1 nodes.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
1 1
2 2 3
3
4 5 6 7 4 5 6 7
8 9 10 11 12 13 14 15
8 9
Complete binary tree Full binary tree of depth 3
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
2 8
1 4 7 9
3 5
Print Arithmetic Expressions
• Specialization of an inorder traversal Algorithm inOrder (v)
– print operand or operator when if isInternal (v){
visiting node
– print “(“ before traversing left subtree
print(“(’’)
– print “)“ after traversing right subtree inOrder (leftChild
(v))}
print(v.element ())
if isInternal (v){
inOrder (rightChild
(v))
print (“)’’)}
a 1
Evaluate Arithmetic Expressions
2 3 2
5 1
Creativity:
pathLength(tree) = depth(v) v tree
Algorithm pathLength(v, n)
Input: a tree node v and an initial value n
Output: the pathLength of the tree with root v
Usage: pl = pathLength(root, 0);
if isExternal (v)
return n
return
(pathLength(leftChild (v), n + 1) +
pathLength(rightChild (v), n + 1) + n)
•
Euler Tour Traversal
Generic traversal of a binary tree
• Includes a special cases the preorder, postorder and inorder traversals
• Walk around the tree and visit each node three times:
– on the left (preorder)
– from below (inorder)
– on the right (postorder)
L R
B
2 3 2
5 1
Euler
eulerTour(node v) {
Tour Traversal
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}
Revise Tree Class Specification
enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER};
template<class ItemType>
class TreeType { new member
public: functions
// previous member functions
void PreOrder(TreeNode<ItemType>, QueType<ItemType>&)
void InOrder(TreeNode<ItemType>, QueType<ItemType>&)
void PostOrder(TreeNode<ItemType>, QueType<ItemType>&)
private:
TreeNode<ItemType>* root;
QueType<ItemType> preQue;
QueType<ItemType> inQue; new private data
QueType<ItemType> postQue;
};
ResetTree and GetNextItem (cont.)
template<class ItemType>
void PreOrder(TreeNode<ItemType>tree,
QueType<ItemType>& preQue)
{
if(tree != NULL) {
preQue.Enqueue(tree->info); // “visit”
PreOrder(tree->left, preQue);
PreOrder(tree->right, preQue);
}
}
ResetTree and GetNextItem (cont.)
template<class ItemType>
void InOrder(TreeNode<ItemType>tree,
QueType<ItemType>& inQue)
{
if(tree != NULL) {
InOrder(tree->left, inQue);
inQue.Enqueue(tree->info); // “visit”
InOrder(tree->right, inQue);
}
}
ResetTree and GetNextItem (cont.)
template<class ItemType>
void PostOrder(TreeNode<ItemType>tree,
QueType<ItemType>& postQue)
{
if(tree != NULL) {
PostOrder(tree->left, postQue);
PostOrder(tree->right, postQue);
postQue.Enqueue(tree->info); // “visit”
}
}
ResetTree
template<class ItemType>
void TreeType<ItemType>::ResetTree(OrderType order)
{
switch(order) {
case PRE_ORDER: PreOrder(root, preQue);
break;
case IN_ORDER: InOrder(root, inQue);
break;
case POST_ORDER: PostOrder(root, postQue);
break;
}
}
GetNextItem
template<class ItemType>
void TreeType<ItemType>::GetNextItem(ItemType& item, OrderType
order, bool& finished)
{
finished = false;
switch(order) {
case PRE_ORDER: preQue.Dequeue(item);
if(preQue.IsEmpty())
finished = true;
break;
case IN_ORDER: inQue.Dequeue(item);
if(inQue.IsEmpty())
finished = true;
break;
case POST_ORDER: postQue.Dequeue(item);
if(postQue.IsEmpty())
finished = true;
break;
}
}
Iterative Insertion and Deletion
• Reading Assignment (see textbook)
Comparing Binary Search Trees to Linear Lists
Big-O Comparison
Binary Array- Linked
Operation
Search Tree based List List
Constructor O(1) O(1) O(1)
Destructor O(N) O(1) O(N)
IsFull O(1) O(1) O(1)
IsEmpty O(1) O(1) O(1)
RetrieveItem O(logN)* O(logN) O(N)
InsertItem O(logN)* O(N) O(N)
DeleteItem O(logN)* O(N) O(N)
*assuming h=O(logN)
Exercises 37-41 (p. 539)
Exercise 17 (p. 537)
Exercise 18 (p. 537)