Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Programming C# 4

PCS4
Feb 2019

1
Overview
Wee Subject Book
k
1 Delegates & Events Nothing
2 RFID Reader & Sorting (Selection § 18.3.1
sort)
3 Recursion & Binary search § 7.15 & §18.2
4 Passing arguments by: § 7.16
value/reference
“ref” & “out” parameters
5 Linked Lists, Stacks, Queues § 19.4
6 Binary trees § 19.7
7 Repetition (Mock exam)
8 Exam

2
Content
• Topics
– Binary tree
– Traversing a (binary) tree

• Learning objectives
– You can implement your own binary tree with a linked list
– You can apply recursion to traverse the binary tree

3
Repetition week 5 – Linked lists
• Singly
10| 20| 30|
null
next next next

head

• Doubly

prev|10| prev|20| prev|30|


nul null
next next next
l

head

4
Binary trees

5
What is a binary tree?
• A binary tree is a rooted
(directed) tree a
– “b” is the left child of “a”
– “c” is the right child of “a” b c
– “a” is the parent of “b” and “c”

d e f

g h

6
Sub-trees
• The blue indicates the left a
subtree
• The red indicates the right b c
subtree
d e f

g h

7
How does it work?
• How do we program a node in
a
C#?
• How can we represent the
b c
children?
• What kind of operations can
d e f
we perform?
g h

8
Binary tree: The node
class TreeNode
{ a
public int Value{ get; set; }
public TreeNode LeftTree { get; set; }
b c
public TreeNode RightTree { get; set; }
public TreeNode(int value)
{ d e f
Value = value;
LeftTree = RightTree = null; g h
}
}

9
Binary Tree: Tree Code (part 1)
class BinaryTree
{ a
private TreeNode Root { get; set; }
public BinaryTree()
b c
{
Root = null;
} d e f
}
g h

10
Binary Tree: Tree Code (part 2)

• How to insert a new Node? a


– Insert into tree
– Insert into node(as child) b c

d e f

g h

11
• If we want to display a Binary tree in a GUI we could do
this by:
– Preorder
a
– Inorder
– Postorder
b c

d e f

g h

12
Binary tree: Preorder
• Preorder:
a
1. Start with the
root
2. Order the left b c

subtree in
preorder d e f
3. Order the right
subtree in g h
preorder

13
Binary tree: Preorder result
public void Preorder(TreeNode n){
Console.Write(n.Value); a
if(n.LeftTree != null)
{
b c
Preorder(n.LeftTree);
}
if(n.RightTree != null) d e f
{
Preorder(n.RightTree); g h
}
}
• a,b,d,g,h,c,e,f

14
Binary tree: Inorder
• Inorder:
1. Order the left a
subtree in
inorder b c
2. Process the root
3. Order the right d e f
subtree in
inorder
g h

15
Binary tree: Inorder result
public void Inorder(TreeNode n){
if(n.LeftTree != null) a
{
Inorder(n.LeftTree);
b c
}
Console.Write(n.Value);
if(n.RightTree != null) d e f
{
Inorder(n.RightTree); g h
}
}
• g,d,h,b,a,e,c,f

16
Binary tree: Postorder
• Postorder:
a
1. Order the left
subtree in
postorder b c

2. Order the right


subtree in d e f
postorder
3. Process the root g h

17
Binary tree: Postorder result
public void Postorder(TreeNode n){
if(n.LeftTree != null) a
{
Postorder(n.LeftTree);
b c
}
if(n.RightTree != null)
{ d e f
Postorder(n.RightTree);
} g h
Console.Write(n.Value);
}
• g,h,d,b,e,f,c,a

18
19

You might also like