Binary Tree Theory
• Another mathematical model that we will use is that of binary trees
• A binary tree can be thought of as a structure comprising zero or more nodes, each with a label of some mathematical type, say T, called the label type
• We call this math type binary tree of T
8 February 2019 OSU CSE 2
Recursive Structure• A binary tree (type binary tree of T) is
either– the empty tree (empty_tree), which has no nodes
at all; or– a non-empty tree, which consists of:
• A root node (type T) • Two subtrees of the root, each of which is a binary tree (type binary tree of T)
• Since a non-empty binary tree contains two other binary trees (each of which may contain others), its structure is recursive
8 February 2019 OSU CSE 3
Recursive Structure• A binary tree (type binary tree of T) is
either– the empty tree (empty_tree), which has no nodes
at all; or– a non-empty tree, which consists of:
• A root node (type T) • Two subtrees of the root, each of which is a binary tree (type binary tree of T)
• Since a non-empty binary tree contains two other binary trees (each of which may contain others), its structure is recursive
8 February 2019 OSU CSE 4
Previously, with general trees and XMLTree, we downplayed the recursive structure. Not here!
The Empty Tree
• The empty binary tree is denoted by empty_tree and is shown in our diagrams as simply the absence of a tree where there could be one, or (when it’s not completely clear from context) as
8 February 2019 OSU CSE 5
Non-Empty Trees
• Every non-empty tree is the result of the mathematical function compose applied to a value of the label type T and two other values of type binary tree of T, which are the root and the left and right subtrees of the resulting binary tree
• In the diagrams that follow, T is integer
8 February 2019 OSU CSE 6
The compose Function
8 February 2019 OSU CSE 8
x, t1, t2 compose(x, t1, t2)
x = 42
t1 =
t2 =
42
Diagrams use color triangles to stand for any non-empty tree.
The compose Function
8 February 2019 OSU CSE 9
x, t1, t2 compose(x, t1, t2)
x = 42
t1 =
t2 =
42
root
leftsubtree
rightsubtree
Size, Height, and Labels• The size of t, i.e., the total number of
nodes in t, is denoted by |t|• The height of t, i.e., the number of nodes
on the longest path from the root to an empty subtree of t, is denoted by ht(t)
• The labels of t, i.e., the set whose elements are exactly the labels of t (i.e., the tree’s labels without duplicates and ignoring order) is denoted by labels(t)
8 February 2019 OSU CSE 13