C CS 342 - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

C CS 342

Description:

... of nodes. Much of the terminology for trees comes from family ... 1. If tree is not COMPLETE add fake nodes. 2. Start at last node of left subtree. Trace the ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 68
Provided by: salga5
Category:
Tags: family | trace | tree

less

Transcript and Presenter's Notes

Title: C CS 342


1
C CS 342
Data Structures Trees
2
Binary Trees
  • A binary tree is an example of a NON-LINEAR
    structure.
  • This requires a more complex linking between
    the
  • components or NODES.
  • A binary tree is a finite sets of nodes.
  • Much of the terminology for trees comes from
    family
  • relations
  • The following slide will show an example of a
    binary
  • tree and the terminology for trees.

3
Binary Trees
LEFT SUBTREE
ROOT
LEAF
LEAF
LEAF
RIGHT SUBTREE
LEAF
4
Binary Trees
LT CHILD of M PARENT of A,H
RT CHILD of M PARENT of N,X
RT CHILD of G PARENT of I
LT CHILD of G PARENTof B
5
Binary Trees
2 nodes with same Parent A H are sibling of G
N X are Descendent of P
LEAF
H, G, M are Ancestors of I
LEAF and a Right Child of A
A LEAF and is NOT a Sibling of H
6
Depth of a node Start at NODE N and move upward
towards root
Depth of ROOT is 0
1
2
3
1
Depth of node G is 1
2
1
Depth of node X is 2
Depth of node I is 3
7
Depth or Height of a Tree
0
1
2
3
Depth or Height of the Tree 3
8
A full binary tree with h levels contains 2h -1
elements
Level 1
0
3 levels 23 -1 8-1 7 elements
1
2
Level 2
3
4
5
6
Level 3
0 1 2 3 4
5 6
9
A COMPLETE binary tree BUT NOT FULL
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0 1 2 3 4 5
6 7 8 9 10
10
A Binary tree that is neither COMPLETE nor FULL
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0 1 2 3 4 5
6 7 8 9 10
11
In-Order Traversal
Done!
visit Left subtree visit Root visit Right
subtree
Algorithm
1. If tree is not COMPLETE add fake nodes 2.
Start at root Trace the entire tree. 3. Mark
node when visited 4. Write node when visited a
SECOND time
12
In-Order Traversal
Done!
visit Left subtree visit Root visit Right
subtree
13
Pre-Order Traversal
Done!
visit Root visit Left Subree visit Right
Subtree
1. If tree is not COMPLETE add fake nodes 2.
Start at root Trace the entire tree. 3.
Write node when visited the FIRST time
Algorithm
14
Pre-Order Traversal
Done!
visit Root visit Left subtree visit Right
subtree
15
Post-Order Traversal
Done!
visit Left Subree visit Right
Subtree visit Root
1. If tree is not COMPLETE add fake nodes 2.
Start at last node of left subtree. Trace the
left child, right child, and parent. (Repeat) 3.
Write node when visited the FIRST time
Algorithm
16
Post-Order Traversal
Done!
visit Left subtree visit Right
subtree visit Root
17
Summary of tree traversals
Position or Location of root
Pre-Order Traversal
Beginning of output list
In-Order Traversal
Middle of output list
Post-Order Traversal
End of output list
18
Complete Binary Trees
  • Can be Implemented as
  • static arrays
  • dynamic arrays (shrink grow as needed)
  • root is always at 0
  • To find the parent of node i ------gt (i -
    1)/2
  • (integer division)
  • To find the Left Child of a node-----gt 2i 1
  • To find the Right Child of a node --gt 2i 2

19
Complete Binary Trees
PARENT
0 1 2 3 4 5 6
2
2
2
2
0 1 2 3 4 5 6
2
If i 2 parent ( i -1 )/2 ( 2 -1
)/2 1/2 0
If i 2 Left Child ( 2i 1 ( 22
1) 4 1 5
If i 2 Right Child ( 2i 2 ( 22
2 ) 42 6
Left Child
Right Child
Int division
20
Complete Binary Trees
PARENT
0 1 2 3 4 5 6
1
2
2
2
0 1 2 3 4 5 6
1
If i 1 parent ( i -1 )/2 ( 1 -1
)/2 0/2 0
If i 1 Left Child ( 2i 1 ( 21
1) 2 1 3
If i 1 Right Child ( 2i 2 ( 21
2 ) 22 4
Right Child
Left Child
Int division
21
Complete Binary Trees
Parent ROOT
0 1 2 3 4 5 6
0
2
2
2
0 1 2 3 4 5 6
0
If i 0 parent ( i -1 )/2 ( 0 -1
)/2 -1/2 0
If i 0 Left Child ( 2i 1 ( 20
1) 0 1 1
If i 0 Right Child ( 2i 2 ( 20
2 ) 02 2
Right Child
Left Child
Int division
22
Complete Binary Trees
  • We can store the tree as a fixed-sized array
  • by using the formulas
  • class requirements
  • 1) declare the array in private
  • 2) declare a counter to keep track of
  • how much of the array is used
  • if dynamic
  • 3) declare a var to keep track of the
  • complete size of the dynamic array
  • also
  • The formulas make it easy to traverse
  • the tree, when moving from node to node

23
Binary Search Trees
  • How to build a BST
  • BST storage rules
  • RULE 1
  • The entry in node n is greater than every
  • entry in its left sub-tree
  • (however it could be equal)
  • RULE 2
  • The entry in node n is less than every entry
  • in its right sub-tree

24
Binary Search Trees examples
3 \ 9 \ 17 \ 45
\ 53 \ 53 \
54
45, 53, 17, 9, 53, 3, 54 Left Sub-Tree LINEAR
45, 53, 9,17, 53, 3, 54 MORE BALANCED
3, 9, 17, 45, 53, 53, 54 BAD ROOT Linear linked
list
45 / \ 17 53 / /
\ 9 53 54 / 3
45 / \ 9 53 /
\ / \ 3 17 53 54
25
Binary Search Trees insert an item
45, 53, 9, 17, 53, 3, 54
45 \ 53
45 / \ 9 53
45 / \ 9 53 \
17
45
45 / \ 9 53 \
/ 17 53
45 / \ 9 53 /
\ / 3 17 53
45 / \ 9 53
/ \ / \ 3 17 53 54
26
TreeNode LeftPtr
data RightPtr
123
NULL
NULL
27
Declaring a Tree NODE class
Make Tree a friend allows Tree to access this
class (functions)
Any type of data
templateltclass NODETYPEgt class TreeNode
friend class TreeltNODETYPEgt
public TreeNode(const NODETYPE )
NODETYPE getData() const
private TreeNode leftPtr NODETYPE data
TreeNode rightPtr
constructor
Returns the data of the node
points to the left next node
Data
points to the right next node
28
Declaring a Tree NODE class
templateltclass NODETYPEgt TreeNodeltNODETYPEgtTreeN
ode(const NODETYPE info) leftPtr 0 data
info rightPtr 0
Constructor initialiazes a node
29
Declaring a Tree NODE class
templateltclass NODETYPEgt NODETYPE
TreeNodeltNODETYPEgtgetData() const return
data
Returns a copy of the data in the node
30
Declaring A class Tree (friend of TreeNode) and
the rest of the FUNCTIONS
  • We need a class (Tree) that consists of all
  • functions needed to solve the problem
  • a pointer that points to the ROOT
  • of the TreeNode class
  • utility functions
  • insert-Node-Helper, remove-Node-Helper
  • Pre-Order-Helper
  • In-Order-Helper, and
  • Post-Order-Helper

31
BST Class
templateltclass NODETYPEgt class Tree public
Tree() void insertNode (const
NODETYPE ) void removeNode(const
NODETYPE ) void preOrderTraversal()
const void inOrderTraversal()
const void postOrderTraversal()
const TreeNodeltNODETYPEgt
findMin (TreeNodeltNODETYPEgt ) const
32
BST Class
private TreeNodeltNODETYPEgt rootPtr
void insertNodeHelper (TreeNodeltNODETYPEgt ,
const NODETYPE ) void removeNodeHelper (TreeNod
eltNODETYPEgt , NODETYPE ) void
preOrderHelper (TreeNodeltNODETYPEgt )
const void inOrderHelper (TreeNodeltNODETYPEgt
) const void postOrderHelper
(TreeNodeltNODETYPEgt ) const
33
  • Call this function from the MAIN driver
  • and pass the key to insert into BST
  • then, this function will call the insert
  • function to get the job done
  • RECURSIVELY

templateltclass NODETYPEgt void TreeltNODETYPEgtinse
rtNode (const NODETYPE value) insertNodeHel
per(rootPtr, value)
34
BST insert a node
templateltclass NODETYPEgt void TreeltNODETYPEgt
insertNodeHelper (TreeNodeltNODETYPEgt ptr,
const NODETYPE value ) if ( ptr 0
) // tree empty ptr new TreeNodeltNODETYPEgt(
value) assert(ptr ! 0)
35
BST insert a node
else if ( value lt (ptr)-gtdata)
insertNodeHelper( ( (ptr)-gtleftPtr ), value
) else if ( value gt (ptr)-gtdata)
insertNodeHelper( ( (ptr)-gtrightPtr
),value) else cout ltlt value ltlt " dup " ltlt
endl
36
BST TraversalsInOrder
templateltclass NODETYPEgt void TreeltNODETYPEgtinOr
derTraversal() const if (rootPtr NULL
) cout ltlt "\n BST empty ..." else inOrder
Helper(rootPtr)
37
BST TraversalsIn-Order
templateltclass NODETYPEgt void TreeltNODETYPEgtinOr
derHelper ( TreeNodeltNODETYPEgt ptr)
const if ( ptr ! 0 ) inOrderHelper(ptr-gtle
ftPtr) cout ltlt ptr-gtdata ltlt '
' inOrderHelper(ptr-gtrightPtr)
38
BST TraversalsPre-Order
templateltclass NODETYPEgt void TreeltNODETYPEgtpreO
rderTraversal() const if (rootPtr NULL
) cout ltlt "\n BST empty ..." else preOrde
rHelper(rootPtr)
39
BST TraversalsPre-Order
templateltclass NODETYPEgt void TreeltNODETYPEgtpreO
rderHelper ( TreeNodeltNODETYPEgt ptr)
const if ( ptr ! 0 ) cout ltlt ptr-gtdata ltlt
' ' preOrderHelper(ptr-gtleftPtr) preOrderH
elper(ptr-gtrightPtr)
40
BST TraversalsPostOrder
templateltclass NODETYPEgt void TreeltNODETYPEgtpost
OrderTraversal() const if (rootPtr NULL
) cout ltlt "\n BST empty ..." else postOrd
erHelper(rootPtr)
41
BST TraversalsPost-Order
templateltclass NODETYPEgt void TreeltNODETYPEgtpost
OrderHelper ( TreeNodeltNODETYPEgt ptr)
const if ( ptr ! 0 ) postOrderHelper(ptr-gt
leftPtr) postOrderHelper(ptr-gtrightPtr) co
ut ltlt ptr-gtdata ltlt ' '
42
Remove a node from a BST
  • CASE 1 delete a LEAF
  • find the node
  • check if node is a left or
  • right child
  • set the parent to 0 or NULL

43
CASE 1 delete a leaf node 3
10
10
Par
Par
5
5
15
15
0
3
8
8
ParLeftPtr NULL
44
CASE 1 delete a leaf node 8
10
10
5
5
15
Par
Par
15
0
3
8
3
ParRightPtr NULL
45
Remove a node from a BST
  • CASE 2 delete a node with one child
  • find the node
  • check if node is a left or right child
  • the node can be removed after its
  • parent adjusts a pointer to bypass
  • the node

46
CASE 2a delete 5 with a RT or LT child
Par
Par
10
10
delPtr
5
5
15
15
delPtr
8
3
Par--gtleftPtr delPtr-gtrightPtr
Par--gtleftPtr delPtr-gtleftPtr
47
CASE 2b delete 15 with a RT or a LT child
Par
10
Par
5
15
delPtr
delPtr
3
13
Par--gtrightPtr delPtr-gtleftPtr
Par--gtrightPtr delPtr-gtrightPtr
48
Remove a node from a BST
  • CASE 3 delete a node with Two Children
  • Find the node
  • Identify the right sub-tree
  • Go right
  • Find the smallest key of the right
  • sub-tree using a tempPtr
  • Replace the key of the node to be
  • deleted with the smallest key of the
  • right sub-tree
  • Adjust pointers Delete tempPtr

49
Par
12
5
15
delPtr
3
9
10
7
8
6
Find the node to be deleted
50
Par
12
5
15
delPtr
3
9
Right Sub-Tree
10
7
8
6
Identify the right sub-tree from that node
51
Par
12
delPtr
5
15
3
9
Go right
10
7
8
6
Go right
52
Par
12
delPtr
5
15
3
9
Go left
Right Sub-Tree
10
TempPtr
7
8
6
Go left as far as you can go
53
Par
12
5
15
delPtr
3
9
Right Sub-Tree
Go left
10
7
TempPtr
8
6
Go left as far as you can go
54
Par
12
delPtr
5
15
3
9
Right Sub-Tree
Smallest Key
10
7
TempPtr
8
6
Replace 5 with 6
55
Par
12
delPtr
6
15
3
9
Right Sub-Tree
Smallest Key
10
7
TempPtr
8
6
Replace 5 with 6
56
Par
12
delPtr
6
15
3
9
Right Sub-Tree
Smallest Key
10
7
TempPtr
8
6
Adjust pointers Delete TempPtr
57
12
6
15
3
9
10
7
8
6
Done !
58
Remove a node from a BST
  • How to check if a node has
  • NO children
  • (LEAF)

Ptr
If ( Ptr-gtLC NULL Ptr-gtRC NULL )
59
Remove a node from a BST
  • How to check if a node
  • has TWO children

Ptr
If ( Ptr-gtLC ! NULL Ptr-gtRC ! NULL )
60
Remove a node from a BST
  • How to check if a node
  • has ONE child
  • Right Child

Ptr
If ( Ptr-gtLC NULL Ptr-gtRC ! NULL )
61
Remove a node from a BST
  • How to check if a node
  • has ONE child
  • Left Child

Ptr
If ( Ptr-gtLC ! NULL Ptr-gtRC NULL )
62
Function call rootroot-gtrightPtr
  • How to find the
  • smallest key of
  • the right Sub-Tree

template ltclass NODETYPEgt TreeNodeltNODETYPEgt
TreeltNODETYPEgt findMin(TreeNodeltNODETYP
Egt ptr) const if ( ptr NULL ) return
NULL else if ( ptr-gtleftPtr NULL ) return
ptr else return findMin(ptr-gtleftPtr)
63
  • Call this function from the MAIN driver
  • and pass the key to be deleted
  • then, this function will call the remove
  • function to get the job done
  • RECURSIVELY

templateltclass NODETYPEgt void TreeltNODETYPEgt
removeNode(const NODETYPE value)
removeNodeHelper(rootPtr, value)
64
BST Remove function
templateltclass NODETYPEgt void TreeltNODETYPEgtremo
veNodeHelper ( TreeNodeltNODETYPEgt root,
NODETYPE value) TreeNodeltNODETYPEgt
temp if ( root NULL ) cout ltlt "\n
Element " ltlt value ltlt "not found" else if
(value lt root-gtdata) removeNodeHelper(
root-gtleftPtr, value) else if (value gt
root-gtdata) removeNodeHelper(
root-gtrightPtr, value)
65
node to be removed found has TWO Children
else if (root-gtleftPtr ! NULL
root-gtrightPtr ! NULL) temp
findMin(root-gtrightPtr)
root-gtdata temp-gtdata removeNodeHelper(roo
t-gtrightPtr, root-gtdata)
66
The node has 1 child or none
else temp root if
(root-gtleftPtr NULL ) root
root-gtrightPtr else if (root-gtrightPtr
NULL ) rootroot-gtleftPtr delete temp

Only a RIGHT child
Only a LEFT child
67
BST summary
  • Because of the recursive definition of BST
  • it is common to write the RECURSIVE functions
  • to implement BSTs
  • the average depth of a BST is (log n)
  • running time
  • insert search
  • Best-case O( log n )
  • if tree is completely balanced
  • Worst-case O( n )
  • if tree is linear
  • Average-case ( log n )
Write a Comment
User Comments (0)
About PowerShow.com