Trees: basic definitions and terminology
Contrary to arrays, stacks, queues and sequences all of which are one-
dimensional data structures, trees are two-dimensional data structures with
hierarchical relationship between data items.
Definition 1 A tree is a non-empty collection of vertices (nodes) and edges
that satisfy certain requirements.
Definition 2 A path in a tree is a list of distinct vertices in which successive
vertices are connected by edges in the tree.
One node in the tree is designated as the root. Each tree has exactly one
path between the root and each of the other nodes. If there is more than one
path between the root and some node, or no path at all, we have a graph.
Definition 3 A set of trees is called a forest.
Definition 4 (Recursive definition) A tree is either a single node or a root
node connected to a forest.
Example of a tree:
root
siblings
subtree
internal nodes
external nodes, or leaves
More definitions
Definition 5 An ordered tree is a tree in which the order of children is
specified.
Definition 6 A level (depth) of a node in the number of nodes on the path
from that node to the root.
Definition 7 The height (maximum distance) of a tree is the maximum
level among all of the nodes in the tree.
Definition 8 The path length of a tree is the sum of the levels of all the
nodes in the tree.
Definition 9 A tree where each node has a specific number of children
appearing in a specific order is call a multiway tree. The simplest type of
a multiway tree is the binary tree. Each node in a binary tree has exactly
two children one of which is designated as a left child, and the other is
designated as a right child.
Definition 10 (Recursive definition) A binary tree is either an external node,
or an internal node and two binary trees.
Example of a binary tree
root
left child right child
one or both children
might be external nodes
special external nodes with
no name and no data associated
with them
More binary trees examples
1. Binary tree for representing arithmetic expressions. The underlying hierarchical
relationship is that of an arithmetic operator and its two operands.
Arithmetic expression in an infix form: (A - B) + C * (E / F)
+
- *
A B C /
E F
Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right
subtree next, and finally the root) returns the postfix form of the arithmetic
expression, while the pre-order traversal (root is visited first, then the left subtree,
then the right subtree) returns the prefix form of the arithmetic expression.
2. Binary tree with a heap property. The underlying hierarchical relationship
suggests that the datum in each node is greater than or equal to the data in
its left and right subtrees.
87
84 63
68 79 12
32 67 6 10
8 9
3. Binary tree with an ordering property. The underlying hierarchical
relationship suggests that the datum in each node is greater than the data in
its left subtree, and less than or equal to the data in its right subtrees.
87
84 103
68 86 90 109
32 74 88 97
70 80
4. Decision trees. The underlying hierarchical relationship depends on the nature of
the domain represented by the binary tree. For example, consider a domain that
consists of the following statements (from J.Ignizio “Intro to ES”):
 If the plane’s engine is propeller, then the plane is C130.
 If the plane’s engine is jet and the wing position is low, then the plane is B747.
 If the plane’s engine is jet and the wing position is high and no bulges are seen, then
the plane is C5A
 If the plane’s engine is jet and the wing position is high and bulges are aft of wing, then
plane is C141 .
The following decision tree can be generated from these rules:
Engine type
Jet Propeller
Wing Position C130
Low High
B747 Bulges
None Aft Wing
C5A C141
Properties of binary trees
1. The number of external nodes is 1 more than the number of internal nodes.
It is easy to see this if we start removing external nodes with their internal
parent, one pair at a time (assume that a method removeAboveExternal(n)
does this). At the end of this process, only the root with its two external
children will remain.
2. The number of external nodes is at least h + 1, where h is the height of the
tree, and at most 2
h
. The later holds for a full binary tree, which is a tree
where internal nodes completely fill every level.
3. The number of internal nodes is at least h and at most 2h
- 1.
4. The total number of nodes in a binary tree is at least 2*h + 1 and at most
2
h+1
- 1.
5. The height, h, of a binary tree with n nodes is at least log n+1 and at most
n.
6. A binary tree with n nodes has exactly n - 1 edges.
Full binary trees and complete binary trees
Here is an example of a full binary tree:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
A complete binary tree is a full binary tree where the internal nodes on the
bottom level all appear to the left of the external nodes on that level. Here is
an example of a complete binary tree:
1
2 3
4 5 6
Properties of binary trees (cont.)
The following property holds for a complete binary tree.
Let i be a number assigned to a node in a complete binary tree. Then:
1. If i = 1, then this node is the root of the tree. If i > 1, then the parent of this
node is assigned the number (i / 2).
2. If 2*i > n, then the corresponding node has no left child. Otherwise, the left
child of that node is assigned the number 2*i.
3. If 2*i + 1 > n, then the corresponding node has no right child. Otherwise, the
right child of that node is assigned the number 2*i + 1.
This property suggests a trivial array-based representation of a complete binary
tree, where i is the index of the node in the array. We will see that a slight
modification in this representation allows us to represent any binary tree in a
linear fashion.
Linear (or sequence-based) representation of a
binary tree
Linear representation of a binary tree utilizes one-dimensional array of size
2
h+1
- 1. Consider the following tree:
+ level 0 (d = 0)
- * level 1 (d = 1)
A B C / level 2 (d = 2)
E F level 3 (d = 3)
To represent this tree, we need an array of size 23+1
- 1 = 15
The tree is represented as follows:
1. The root is stored in BinaryTree[1].
2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the
right child is stored in BinaryTree[2*n+1]
i: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
BinaryTree[i]: + - * A B C / E F
Linear representation of a binary tree (cont.)
Advantages of linear representation:
1. Simplicity.
2. Given the location of the child (say, k), the location of the parent is easy to
determine (k / 2).
Disadvantages of linear representation:
1. Additions and deletions of nodes are inefficient, because of the data
movements in the array.
2. Space is wasted if the binary tree is not complete. That is, the linear
representation is useful if the number of missing nodes is small.
Note that linear representation of a binary tree can be implemented by means
of a linked list instead of an array. For example, we can use the Positional
Sequence ADT to implement a binary tree in a linear fashion. This way the
above mentioned disadvantages of the linear representation will be resolved.
Linked representation of a binary tree
Linked representation uses explicit links to connect the nodes. Example:
1
2 5
3 4 6 7
8 9
Nodes in this tree can be viewed as positions in a sequence (numbered 1
through 9).
+
BA
- *
C /
E F
Traversals of a binary tree
Preorder traversal
void preOrder (BTNode localRoot) {
if (localRoot != null) {
localRoot.displayBTNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild); } }
Example Consider a tree with an ordering property, where nodes are inserted
in the following order b i n a r y t r e e, i.e.
b
a i
e n
e r
y
t
r
The preorder traversal is: b a i e e n r y t r
Traversals of a binary tree (cont.)
Post-order traversal
void postOrder (BTNode localRoot) {
if (localRoot != null) {
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayBTNode(); } }
The nodes in the example tree are traversed in post-order as follows:
a e e r t y r n i b
In-order traversal
void inOrder (BTNode localRoot) {
if (localRoot != null) {
inOrder(localRoot.leftChild);
localRoot.displayBTNode();
inOrder(localRoot.rightChild); } }
The nodes in the example tree are traversed in in-order as follows:
a b e e i n r r t y
A Binary Search Tree is a binary
tree with the following properties:
 All items in the left subtree are less than
the root.
 All items in the right subtree are greater or
equal to the root.
 Each subtree is itself a binary search tree.
Basic Property
 In a binary search tree,
 the left subtree contains key values less
than the root
 the right subtree contains key values
greater than or equal to the root.
7-1 Basic Concepts
Binary search trees provide an excellent structure for searching a
list and at the same time for inserting and deleting data into the
list.
Insertion in binary search tree
Insert 9 in the the following tree:
3
2 15
1 11
7 13
Step 1: search for 9 3
2 15
1 11
search stops here 7 13
Step 2: insert 9 at the point where the search terminates unsuccessfully
3
2 15
1 11
7 13 That is, new nodes are always inserted at the leaf level.
9
Deletion in binary search tree
Consider the tree:
3 7
2 15 Deleting 3 2 15
1 11 1 11
7 13 13
The following cases of deletions are possible:
1. Delete a note with no children, for example 1. This only requires the appropriate link in
the parent node to be made null.
2. Delete a node which has only one child, for example 15. In this case, we must set the
corresponding child link of the parent’s parent to point to the only child of the node being
deleted.
3. Delete a node with two children, for example 3. The delete method is based on the
following consideration: in-order traversal of the resulting tree (after delete operation)
must yield an ordered list. To ensure this, the following steps are carried out:
Step 1: Replace 3 with the node with the next largest datum, i.e. 7.
Step 2: Make the left link of 11 point to the right child of 7 (which is null here).
Step 3: Copy the links from the node containing 3 to the node containing 7, and make
the parent node of 3 point to 7.
Trees in Data Structure

Trees in Data Structure

  • 2.
    Trees: basic definitionsand terminology Contrary to arrays, stacks, queues and sequences all of which are one- dimensional data structures, trees are two-dimensional data structures with hierarchical relationship between data items. Definition 1 A tree is a non-empty collection of vertices (nodes) and edges that satisfy certain requirements. Definition 2 A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree. One node in the tree is designated as the root. Each tree has exactly one path between the root and each of the other nodes. If there is more than one path between the root and some node, or no path at all, we have a graph. Definition 3 A set of trees is called a forest. Definition 4 (Recursive definition) A tree is either a single node or a root node connected to a forest.
  • 3.
    Example of atree: root siblings subtree internal nodes external nodes, or leaves
  • 4.
    More definitions Definition 5An ordered tree is a tree in which the order of children is specified. Definition 6 A level (depth) of a node in the number of nodes on the path from that node to the root. Definition 7 The height (maximum distance) of a tree is the maximum level among all of the nodes in the tree. Definition 8 The path length of a tree is the sum of the levels of all the nodes in the tree. Definition 9 A tree where each node has a specific number of children appearing in a specific order is call a multiway tree. The simplest type of a multiway tree is the binary tree. Each node in a binary tree has exactly two children one of which is designated as a left child, and the other is designated as a right child. Definition 10 (Recursive definition) A binary tree is either an external node, or an internal node and two binary trees.
  • 5.
    Example of abinary tree root left child right child one or both children might be external nodes special external nodes with no name and no data associated with them
  • 6.
    More binary treesexamples 1. Binary tree for representing arithmetic expressions. The underlying hierarchical relationship is that of an arithmetic operator and its two operands. Arithmetic expression in an infix form: (A - B) + C * (E / F) + - * A B C / E F Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right subtree next, and finally the root) returns the postfix form of the arithmetic expression, while the pre-order traversal (root is visited first, then the left subtree, then the right subtree) returns the prefix form of the arithmetic expression.
  • 7.
    2. Binary treewith a heap property. The underlying hierarchical relationship suggests that the datum in each node is greater than or equal to the data in its left and right subtrees. 87 84 63 68 79 12 32 67 6 10 8 9
  • 8.
    3. Binary treewith an ordering property. The underlying hierarchical relationship suggests that the datum in each node is greater than the data in its left subtree, and less than or equal to the data in its right subtrees. 87 84 103 68 86 90 109 32 74 88 97 70 80
  • 9.
    4. Decision trees.The underlying hierarchical relationship depends on the nature of the domain represented by the binary tree. For example, consider a domain that consists of the following statements (from J.Ignizio “Intro to ES”):  If the plane’s engine is propeller, then the plane is C130.  If the plane’s engine is jet and the wing position is low, then the plane is B747.  If the plane’s engine is jet and the wing position is high and no bulges are seen, then the plane is C5A  If the plane’s engine is jet and the wing position is high and bulges are aft of wing, then plane is C141 . The following decision tree can be generated from these rules: Engine type Jet Propeller Wing Position C130 Low High B747 Bulges None Aft Wing C5A C141
  • 10.
    Properties of binarytrees 1. The number of external nodes is 1 more than the number of internal nodes. It is easy to see this if we start removing external nodes with their internal parent, one pair at a time (assume that a method removeAboveExternal(n) does this). At the end of this process, only the root with its two external children will remain. 2. The number of external nodes is at least h + 1, where h is the height of the tree, and at most 2 h . The later holds for a full binary tree, which is a tree where internal nodes completely fill every level. 3. The number of internal nodes is at least h and at most 2h - 1. 4. The total number of nodes in a binary tree is at least 2*h + 1 and at most 2 h+1 - 1. 5. The height, h, of a binary tree with n nodes is at least log n+1 and at most n. 6. A binary tree with n nodes has exactly n - 1 edges.
  • 11.
    Full binary treesand complete binary trees Here is an example of a full binary tree: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 A complete binary tree is a full binary tree where the internal nodes on the bottom level all appear to the left of the external nodes on that level. Here is an example of a complete binary tree: 1 2 3 4 5 6
  • 12.
    Properties of binarytrees (cont.) The following property holds for a complete binary tree. Let i be a number assigned to a node in a complete binary tree. Then: 1. If i = 1, then this node is the root of the tree. If i > 1, then the parent of this node is assigned the number (i / 2). 2. If 2*i > n, then the corresponding node has no left child. Otherwise, the left child of that node is assigned the number 2*i. 3. If 2*i + 1 > n, then the corresponding node has no right child. Otherwise, the right child of that node is assigned the number 2*i + 1. This property suggests a trivial array-based representation of a complete binary tree, where i is the index of the node in the array. We will see that a slight modification in this representation allows us to represent any binary tree in a linear fashion.
  • 13.
    Linear (or sequence-based)representation of a binary tree Linear representation of a binary tree utilizes one-dimensional array of size 2 h+1 - 1. Consider the following tree: + level 0 (d = 0) - * level 1 (d = 1) A B C / level 2 (d = 2) E F level 3 (d = 3) To represent this tree, we need an array of size 23+1 - 1 = 15 The tree is represented as follows: 1. The root is stored in BinaryTree[1]. 2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the right child is stored in BinaryTree[2*n+1] i: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 BinaryTree[i]: + - * A B C / E F
  • 14.
    Linear representation ofa binary tree (cont.) Advantages of linear representation: 1. Simplicity. 2. Given the location of the child (say, k), the location of the parent is easy to determine (k / 2). Disadvantages of linear representation: 1. Additions and deletions of nodes are inefficient, because of the data movements in the array. 2. Space is wasted if the binary tree is not complete. That is, the linear representation is useful if the number of missing nodes is small. Note that linear representation of a binary tree can be implemented by means of a linked list instead of an array. For example, we can use the Positional Sequence ADT to implement a binary tree in a linear fashion. This way the above mentioned disadvantages of the linear representation will be resolved.
  • 15.
    Linked representation ofa binary tree Linked representation uses explicit links to connect the nodes. Example: 1 2 5 3 4 6 7 8 9 Nodes in this tree can be viewed as positions in a sequence (numbered 1 through 9). + BA - * C / E F
  • 16.
    Traversals of abinary tree Preorder traversal void preOrder (BTNode localRoot) { if (localRoot != null) { localRoot.displayBTNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); } } Example Consider a tree with an ordering property, where nodes are inserted in the following order b i n a r y t r e e, i.e. b a i e n e r y t r The preorder traversal is: b a i e e n r y t r
  • 17.
    Traversals of abinary tree (cont.) Post-order traversal void postOrder (BTNode localRoot) { if (localRoot != null) { postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayBTNode(); } } The nodes in the example tree are traversed in post-order as follows: a e e r t y r n i b In-order traversal void inOrder (BTNode localRoot) { if (localRoot != null) { inOrder(localRoot.leftChild); localRoot.displayBTNode(); inOrder(localRoot.rightChild); } } The nodes in the example tree are traversed in in-order as follows: a b e e i n r r t y
  • 18.
    A Binary SearchTree is a binary tree with the following properties:  All items in the left subtree are less than the root.  All items in the right subtree are greater or equal to the root.  Each subtree is itself a binary search tree.
  • 19.
    Basic Property  Ina binary search tree,  the left subtree contains key values less than the root  the right subtree contains key values greater than or equal to the root.
  • 20.
    7-1 Basic Concepts Binarysearch trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list.
  • 22.
    Insertion in binarysearch tree Insert 9 in the the following tree: 3 2 15 1 11 7 13 Step 1: search for 9 3 2 15 1 11 search stops here 7 13 Step 2: insert 9 at the point where the search terminates unsuccessfully 3 2 15 1 11 7 13 That is, new nodes are always inserted at the leaf level. 9
  • 23.
    Deletion in binarysearch tree Consider the tree: 3 7 2 15 Deleting 3 2 15 1 11 1 11 7 13 13 The following cases of deletions are possible: 1. Delete a note with no children, for example 1. This only requires the appropriate link in the parent node to be made null. 2. Delete a node which has only one child, for example 15. In this case, we must set the corresponding child link of the parent’s parent to point to the only child of the node being deleted. 3. Delete a node with two children, for example 3. The delete method is based on the following consideration: in-order traversal of the resulting tree (after delete operation) must yield an ordered list. To ensure this, the following steps are carried out: Step 1: Replace 3 with the node with the next largest datum, i.e. 7. Step 2: Make the left link of 11 point to the right child of 7 (which is null here). Step 3: Copy the links from the node containing 3 to the node containing 7, and make the parent node of 3 point to 7.