lecture23-to

Download Report

Transcript lecture23-to

Tree Data Structures
Introductory Examples
Willliam
Bill
Curt
Mary
Marjorie
Richard Anne
Data organization such that items of information
are related by branches.
Branch Meaning: Parent Of
Introductory Examples
Curt
Bill
Alison Ashley William Laura
Dylan
Carol
Ronald
Victoria Jennifer Todd
Trey Shelby
General Tree Definition

A tree is a finite set of one or more nodes such
that:
There is a specially designated node called the root
 The remaining nodes are partitioned into n >= 0
disjoint sets T1, T2, .. Tn, where each of these sets
is a tree. T1, T2, … Tn are called subtrees of the
root.

Introductory Examples
Curt
Bill
Carol
Alison Ashley William Laura
Dylan
Ronald
Victoria Jennifer Todd
Trey Shelby
Root: Curt
Bill is a subtree of Curt, and is a tree in itself
Introductory Examples
Bill
Alison
Ashley
William
Laura
Dylan
Root: Bill
Ashley is a subtree of Bill, and a tree in itself
Dylan is a subtree of Ashley, and a tree in itself
Tree Definition


Order of subtrees is irrelevant
These two tree are equivalent:
Bill
Ronald
Curt
Carol
Curt
Bill
Ronald
Carol
Tree Definitions

A node is an item of information and has branches to
other nodes




Similar to linked list node
The degree of a node is the number of subtrees of
that node
Nodes with a degree of 0 are terminal nodes or leaf
nodes
Nodes with a degree > 0 are non-terminal nodes
Tree Definitions
Curt
Bill
Carol
Ronald
Alison Ashley William Laura Victoria Jennifer Todd
Dylan
Trey Shelby
Degree(Curt): 3 Degree(Bill): 4 Degree(Ashley): 1 Degree(William): 0
Terminal Nodes: Alison, Dylan, William, Laura, Trey, Shelby,
Jennifer, Todd, Ronald – Anyone with no children in real life
Tree Definitions



Roots of subtrees of nodes are children
Parent has one or more children
Children of the same parent are siblings
Introductory Examples
Curt
Bill
Carol
Ronald
Alison Ashley William Laura Victoria Jennifer Todd
Dylan
Trey Shelby
Children(Curt): Bill, Carol, Ronald
Parent(Jennifer): Carol
Sibling(Ashley): Alison, William, Laura
Tree Definitions

Grandparent is the parent of the parent of a
node

Ancestors: All nodes along the path from the

root to the current node.
Descendants: All elements in all subtrees for
which this node is a parent.
Introductory Examples
Curt
Bill
Carol
Ronald
Alison Ashley William Laura Victoria Jennifer Todd
Dylan
Grandparent(Dylan): Bill
Trey Shelby
Ancestors(Dylan): Ashley, Bill, Curt
Descendants(Bill): Alison, Ashley, William, Laura, Dylan
Tree Definitions

Degree(Tree T): Maximum degree of the
nodes in T.

Level(Node N):
Level(Root): 1
 Level(Non-Root): (Number of links between root
and the node) + 1


Height(Tree T): Maximum level of any node
in T, also called Depth
Introductory Examples
Curt
Bill
Carol
Ronald
Alison Ashley William Laura Victoria Jennifer Todd
Dylan
Degree(T): 4 [from Bill]
Trey Shelby
Level(Curt): 1, Level(Ashley): 3, Level(Dylan): 4
Height(T): 4 [from Dylan]
Recursion and Trees

Note that the tree definition itself is recursive


Children of a node are roots of their own tree
Most operators we define will be recursive:
Computing height
 Traversing
 Searching for a node

Tree Representations

Coding a Data Structure For Trees:

First thought:


Follow our linked list node: Store data and pointers to the
children
Sticky issues:


Need pointers to all children
What if variable number of children?
Tree Representations

For a tree with fixed maximum degree of k, could have
each node store k pointers:
Node for tree with max degree 4
DATA

CHILD 1
CHILD 2
CHILD 3
CHILD 4
Potentially very wasteful: Assume k-ary tree, with n
nodes


nk – n + 1 unused pointers
Only use if most all pointers are going to be used – very
dense tree
Tree Representation

Proof:
n nodes, each with k pointers : nk available pointers
 Exactly one pointer to each valid node except root:
(n-1) used pointers
 Total unused = nk – (n-1)
=> nk – n + 1


Example:



Turkett Family Tree: 4-ary, 14 nodes
56 – 14 + 1 = 43 unused pointers
(172 bytes wasted)
Mainly wasted in terminal nodes
Tree Representation

Representing arbitrary trees with just two links
per node

Left-Child, Right-Sibling
Data
Left Child
Right Sibling
Tree Representation
Left Child, Right Sibling Family Tree
Curt
0
Bill
Carol
Ronald
0
Alison
0
0
Victoria
Specialized Trees

Binary Tree:




A restriction of trees such that the maximum degree of a
node is 2.
Order of nodes is now relevant
May have zero nodes
Formal Definition:

A binary tree is a finite set of nodes that either is empty or
consists of a root and two disjoint subtrees called the left
subtree and the right subtree.
Specialized Trees
These are equivalent trees, but are not equivalent binary trees,
as binary trees enforce a more specific structure (a meaning of left
and right).
Types of Binary Trees
Skewed - Unbalanced
Complete – More balanced,
All terminals on same or adjacent
levels (Definition will come later)
Properties of Binary Trees

Given definition of binary tree, interesting
(useful?) properties that hold:
Maximum number of nodes on level l is 2l-1
 Maximum number of nodes in a binary tree of
depth k is 2k - 1

Proofs


Maximum number of nodes on level l is 2l-1
Proof by induction:

Base step:


Level 1 = 21-1 = 20 = 1, which holds (root)
Assume holds for n, n > 1
Level n = 2n-1
 Each node in level n can have at most 2 children in level
n+1, so at most 2n-1 * 2 = 2n nodes in level n+1 => it
holds (n+1-1 = n)

Proofs

Maximum number of nodes in a binary tree of depth k
is 2k – 1
Maximum number of nodes in tree is sum over maximum
nodes in a level
Sum (l = 1 to k) (2l-1) =
1+2+4+8…
Sum of all powers of 2 up to, but not including, 2k
= 2k -1
Properties of Binary Trees

A full binary tree of depth k is a binary tree of
depth k having 2k – 1 nodes
All nodes have two children
 All terminal nodes on same level

1
3
2
4
5
6
7
Properties of Binary Trees

A binary tree with n nodes and depth k is
complete if its nodes correspond to the nodes
numbered from 1 to n in a full binary tree of
depth k

The height of a complete binary tree with n
nodes is (log2(n+1)) – Important and useful!
Properties of Binary Trees

Complete trees
1
3
2
4
5
6
7
1
3
2
1
4
5
2
3
Binary Tree Implementation

Array implementation
1
3
2
4

5
6
7
Map each numbered node into appropriate spot
in one-dimensional array
Binary Tree Implementation

Array implementation:

If a complete binary tree with n nodes is represented
in an array:
parent(i) is at floor ( i / 2) if i != 1
[i == 1 is the root which has no parent]
 left_child(i) = 2i. If 2i > n, i has no left child
 right_child(i) = 2i + 1. If 2i + 1 > n, i has no right child

Array Index: 0
1
2
3
4
5
6
7
Blank Data1 Data2 Data3 Data4 Data5 Data6 Data7
Binary Tree Implementation

As before, array representations are not the best choice:

Fixed memory size




Doesn’t expand easily
Very wasteful of space unless well balanced.
Better representation: Similar to linked list node
Can move away from sibling representation and just
point to two children from a node
Binary Tree Node
class BinaryTree; // forward declaration
class BinaryTreeNode
{
friend class BinaryTree;
private:
char data;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
};
Binary Tree Class
class BinaryTree
{
public:
// public member methods
private:
BinaryTreeNode* root;
};
Binary Tree Node

Only problem with linked node representation:


Difficult to find parent
OK:



Generally not that important to a lot of algorithms
If is important, when traversing list maintain a parent pointer
Could add a parent pointer link to all nodes if particularly
important without substantial changes, but with n (number
of nodes) bytes more use of memory
Questions

For the binary tree below:
What are terminal nodes?
 What are non-terminal nodes?
 What is level of each node?

A
B
C
E
D
D,E
A,B,C
A=1,B=2,CD=3,E=4
Questions

What is the maximum number of nodes in a kary tree of height h?
For 3 = 1 + 3 + 9 + 27 + 81 …
 For 4 = 1 + 4 + 16 + 64 …
 For k = 1 + k + k2 + k3 …

Sum of geometric series: (kh-1) / (k-1)
k = 3, h = 4 = (34 – 1) / (3-1) = 80/2 = 40
Does it hold for 2? (2h-1) / (2-1) = 2h-1
Questions
What would an array representation of the
following tree look like:
Blank
A
B
Blank
C
A
B
C
E
D
D
Blank Blank
E
Blank
Binary Tree Class

What are functions of interest?
class BinaryTree
{
public:
BinaryTree(); // create empty tree
BinaryTree(char data, BinaryTree bt1, BinaryTree bt2);
//construct a new tree , setting root data to data and links to
//other trees
bool isEmpty(); // test whether empty
BinaryTree leftChild(); // get left child of *this
BinaryTree rightChild(); // get right child of *this
char Data(); // return data in root node of *this
};
Binary Tree Functions

Most operations performed on binary trees require
moving through the tree:






Visiting nodes
Inserting an element
Deleting an element
Non-recursive height calculation
…
Useful to have a simple mechanism for moving through
trees
Binary Tree Traversal

Tree traversal –
Visit each node in the tree exactly once
 Perform some operation

Print data
 Add to sum
 Check for max height


A traversal produces a linear ordering of the nodes
[the order of visits]
Binary Tree Traversal

Treat trees and subtrees in same fashion



Traverse in same order
Use recursion!
Let the following define traversal operations:



L => Move left
V => Visit [Perform operation – print, sum, …]
R => Move right
Binary Tree Traversal

Six possible methods of traversal:


LVR, LRV, VLR, VRL, RVL, RLV
Usually, only use three of these, all with left
before right:
LVR
Inorder
LRV
Postorder
VLR
Preorder
Binary Tree Traversal


Let’s trace the traversal functions with examples
of expressions (operators and operand)
Traversal Name corresponds to order of
outputted expression
Binary Tree Traversal
Inorder: LVR
+
*
D
*
/
A
C
B
E
A/B*C*D+E
Infix expression
Visit left child before
parent
Binary Tree Traversal

Inorder implementation:
void BinaryTree::inorder()
{
inorder(root);
}
Void BinaryTree::inorder(BinaryTreeNode* node)
{
if (node)
{
inorder(node->leftChild);
cout << node->data;
// replace cout with
inorder(node->rightChild);
//arbitrary processing
}
}
Binary Tree Traversal
Postorder: LRV
+
*
D
*
/
A
C
B
E
AB/C*D*E+
Postfix expression
Visit left and right
child before parent
Binary Tree Traversal

Postorder implementation:
void BinaryTree::postorder()
{
postorder(root);
}
void BinaryTree::postorder(BinaryTreeNode* node)
{
if (node)
{
postorder(node->leftChild);
postorder(node->rightChild);
cout << node->data;
}
}
Binary Tree Traversal
Preorder: VLR
+
*
D
*
/
A
C
B
E
+**/ABCDE
Prefix expression
Visit parent before
either child
Binary Tree Traversal

Preorder implementation:
void BinaryTree::preorder()
{
preorder(root);
}
Void BinaryTree::preorder(BinaryTreeNode* node)
{
if (node)
{
cout << node->data;
preorder(node->leftChild);
preorder(node->rightChild);
}
}