Trees
Trees
ICT 106
Basic Tree Concepts
A tree consists of a finite set of elements,
called nodes, and a finite set of directed
lines, called branches, that connect the
nodes. The number of branches associated
with a node is the degree of the node.
When the branch is directed towards the
node, it is an indegree branch; when the
branch is directed away from the node it is an
outdegree branch. The sum of the indegree
and outdegree branches equals the degree of
the node.
If the tree is not empty, then the first node is
called the root. The indegree of the root is
zero. With the exception of the root, all the
nodes in a tree must have an indegree of
exactly one.
A leaf is any node with an outdegree of zero.
Nodes that are not a root or a leaf are known
as internal nodes because they are found in
the middle portion of a tree.
A node is a parent if it has a successor
node; that is, it has an outdegree greater
than zero. Conversely, a node with a
predecessor is a child. A child node has an
indegree of one. Two or more nodes with the
same parent are siblings.
An ancestor is any node in the path from the
root to the node. A descendant is any node
in the path below the parent node; that is, all
nodes in the paths from a given node to a leaf
are descendants of the node.
A path is a sequence of nodes in which each node is adjacent
to the next one.
The level of a node is its distance from the root. The height of
the tree is the level of the leaf in the longest path from the
root plus one. By definition, the height of an empty tree is -1.
A subtree is any connected structure below the root. The first
node in a subtree is known as the root of the subtree and is
used to name the subtree. The concept of subtrees leads us to
a recursive definition of a tree: A tree is a set of nodes that is
1. either empty, or
2. has a designated node called the root from which
hierarchically descend zero or more subtrees, which are also
trees.
BINARY TREES
A tree in which no node can have more than two
subtrees. These subtrees are designated as the left
subtree and right subtree. A null tree is a tree with
no nodes.
Properties
A binary tree is balanced if the height of its subtrees
differs by no more than one (its balance factor is -1, 0,
or +1) and its subtrees are also balanced.
A complete tree has the maximum number of entries
for its height. This occurs when the last level is full. A
tree is considered nearly complete if it has the
minimum height for its nodes and all nodes in the last
level are found on the left.
Binary Tree Traversals
2 return
end postOrder
EXPRESSION TREES
An expression is a sequence of tokens that
follow prescribed rules. A token may be
either an operand or an operator.
An expression tree is a binary tree with the
following properties:
1.Each leaf is an operand.
2.The root and internal nodes are operators.
3.Subtrees are subexpressions with the root
being an operator.
Expression Tree Traversal
The infix traversal of an expression tree as
produced through the inorder traversal.
The prefix traversal uses the standard
preorder tree traversal.
The postfix traversal of an expression used the
basic postorder traversal of any binary tree.
GENERAL TREES
A general tree is a tree in which each node can have an unlimited
outdegree. Each node may have as many children as is necessary to
satisfy its requirements.
Insertions into General Trees
FIFO insertion, the nodes are inserted at the end of the sibling list,
much as a new node is inserted at the rear of a queue. When the list is
then processed, the siblings will be processed in first-in, first-out
(FIFO) order. It is used when the application requires that the data be
processed in the order in which it was input.
LIFO insertion, places the new node at the beginning of the sibling
list. It is the equivalent of a stack.
Key-Sequenced insertion places the new node in key sequence
among the sibling nodes. The logic for inserting in key sequence is
similar to the insertion into a linked list. Starting at the parent’s first
child, we follow the sibling (right) pointers until we locate the correct
insertion point and then build the links with the predecessors and
successors (if any).
Deletions into General Trees (rules)
A node may be deleted only if it has a leaf. In general
trees, this means a node cannot be deleted if it has any
children; that is, it cannot have a left subtree – it is OK
if it has right subtrees.
If a user tries to delete a node that has children, the
program provides an error message that it cannot be
deleted until its children are deleted.
When the node is deleted, we must also check for
siblings; that is, if there are siblings, then the logic
follows the basic rules for linked lists. If the first node is
deleted, then the parent’s left pointer must be updated.
If any other node is deleted, then its predecessor’s right
pointer must be updated to point to the deleted node’s
successor.
BINARY SEARCH TREES
A binary search tree is a binary tree in
which the left subtree contains key values
less than the root and the right subtree
contains key values greater than or equal to
the root.
It is a binary tree with the following
properties:
1. All items in the left subtree are less than the
root.
2. All items in the right subtree are greater
than or equal to the root.
3. Each subtree is itself a binary search tree.
Operations on Binary Search Tree
The Binary Search Tree Traversal
algorithm. Uses the inorder traversal to
produce an ordered list.
The Binary Search Tree Search
Algorithms. (1) find smallest node, (2) find
the largest node, and (3) find a requested
node.
Find smallest
algorithm node
findSmallestBST returns the smallest
(val root
<pointer>)
value in the
This algorithm
in a BST.
leftmost
finds leaf
the smallest node node in the tree.