1 trees. 2 outline –tree structures –tree node level and path length –binary tree definition...
Post on 20-Dec-2015
223 views
TRANSCRIPT
2
Outline
– Tree StructuresTree Structures– Tree Node Level and Path LengthTree Node Level and Path Length– Binary Tree DefinitionBinary Tree Definition– Binary Tree NodesBinary Tree Nodes– Binary Search TreesBinary Search Trees
3
Tree Structures
P r e side n t - C E O
P r o duc t io nM a n a ge r
Sa le sM a n a ge r
Sh ip p in gSup e r v iso r
P e r so n n e lM a n a ge r
W a r e h o useSup e r v iso r
P ur c h a sin gSup e r v iso r
H IER A R C H IC A L TR EE S TR U C TU R E
Arrays, vectors, lists are linear structures, one element follows another. Trees are hierarchical structure.
5
Tree Terminology
Tree is a set of nodes. The set may be empty. If not empty, then there is a distinguished node r, called root, all other nodes originating from it, and zero or more non-empty subtrees T1,T2, …,Tk, each of whose roots are connected by a directed edge from r. (inductive definition)
T1 T2 Tk…
r
6
Tree Terminology
A
JI
HGFE
DCB
(a)
(b )
A G EN ERAL T REE
root
Nodes in subtrees are called successors or descendents of the root node
An immediate successor is called a child
A leaf node is a node without any children while an interior node has at least one child.
The link between node describes the parent-child relation.
The link between parent and child is called edge
A path between a parent P and any node N in its subtrees is a sequence of nodes P= X0,X1, …,Xk=N, where k is the length of the path. Each node Xi in the path is the parent of Xi+1, (0≤i<k)
Size of tree is the number of nodes in the tree
7
Tree Node Level and Path Length
A
HG
FE
DCB
L e ve l: 0
L e ve l: 1
L e ve l: 2
L e ve l: 3
The level of a node N in a tree is the length of the path from root to N. Root is at level 0.The depth of a tree is the length of the longest path from root to any node.
8
Binary Tree In a binary tree, no node has more than two
children, and the children are distinguished as left and right.
A binary tree T is a finite set of nodes with one the following properties:1. T is a tree if the set of nodes is empty.2. The set consists of a root R and exactly two
distinct binary trees. The left subtree TL and the right subtree TR, either or both subtree may be empty.
9
Examples of Binary Trees
A
E
D
C
B
A
F
H
ED
CB
I
T ree ASiz e 9 D ep t h 3
T ree BSiz e 5 D ep t h 4
G
Size? depth?
11
Density of a Binary Tree At any level n, a binary tree may contain from
1 to 2n nodes. The number of nodes per level contributes to the density of the tree.
Degenerate tree: there is a single leaf node and each interior node has only one child. An n-node degenerate tree has depth n-1 Equivalent to a linked list
A complete binary tree of depth n is a tree in which each level from 0 to n-1 has all possible nodes and all leaf nodes at level n occupy the leftmost positions in the tree.
12
Complete Binary Tree• complete binary tree: a binary tree in which every
level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible
13
Complete or noncomplete?A
ED
CB
GF
C o m p let e T ree (D ep t h 2 )F u ll w it h all p o s s ib le n o d es
14
Complete or noncomplete?
A
ED
CB
IH
N o n -C o m p let e T ree (D ep t h 3 )Lev el 2 is m is s in g n o d es
15
Complete or noncomplete?
A
ED
CB
GF
KIH
N o n -C o m p let eT ree (D ep t h 3 )N o d es at lev el 3 d o n o t o ccu rp y left m o s t p o s it io n soccupy
17
Perfect Binary Tree• perfect binary tree: a binary tree with all leaf nodes
at the same depth. All internal nodes have exactly two children.
• a perfect binary tree has the maximum number of nodes for a given height
• a perfect binary tree has 2(n+1) - 1 nodes where n is the height of a tree– height = 0 -> 1 node– height = 1 -> 3 nodes– height = 2 -> 7 nodes– height = 3 -> 15 nodes
Binary Search Trees
• Key property– Value at node
• Smaller values in left subtree• Larger values in right subtree
– Example• X > Y• X < Z
Y
X
Z
Binary Search Trees• Examples
Binary search trees
Not a binary search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Iterative Search of Binary TreeNode *Find( Node *n, int key) {
while (n != NULL) { if (n->data == key) // Found it
return n;if (n->data > key) // In left subtree n = n->left;else // In right subtree n = n->right;
} return null;
}Node * n = Find( root, 5);
Recursive Search of Binary Tree
Node *Find( Node *n, int key) {if (n == NULL) // Not found
return( n );else if (n->data == key) // Found it
return( n );else if (n->data > key) // In left subtree
return Find( n->left, key );else // In right subtree
return Find( n->right, key );}Node * n = Find( root, 5);
Example Binary Searches
• Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
Example Binary Searches• Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
Types of Binary Trees• Degenerate – only one child• Complete – always two children• Balanced – “mostly” two children
– more formal definitions exist, above are intuitive ideas
Degenerate binary tree
Balanced binary tree
Complete binary tree
Binary Search Tree Construction
• How to build & maintain binary trees?– Insertion– Deletion
• Maintain key property (invariant)– Smaller values in left subtree– Larger values in right subtree
Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for Y• Observations
– O( log(n) ) operation for balanced tree– Insertions may unbalance tree
Example Insertion
• Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
Binary Search Tree – Deletion
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal nodea) Replace with largest value Y on left subtree OR smallest value Z on right subtreeb) Delete replacement value (Y or Z) from subtree
Observation– O( log(n) ) operation for balanced tree– Deletions may unbalance tree
Example Deletion (Leaf)
• Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
Example Deletion (Internal Node)
• Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10 with largest value in left
subtree
Replacing 5 with largest value in left
subtree
Deleting leaf