0% found this document useful (0 votes)
8 views25 pages

Trees

The document provides an overview of tree data structures, including basic concepts, properties of binary trees, and traversal methods such as depth-first and breadth-first. It also covers specific types of trees, such as expression trees and binary search trees, detailing operations like insertion, deletion, and searching. Additionally, it explains the rules for general trees and their manipulation, emphasizing the importance of maintaining tree structure during operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Trees

The document provides an overview of tree data structures, including basic concepts, properties of binary trees, and traversal methods such as depth-first and breadth-first. It also covers specific types of trees, such as expression trees and binary search trees, detailing operations like insertion, deletion, and searching. Additionally, it explains the rules for general trees and their manipulation, emphasizing the importance of maintaining tree structure during operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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

Requires that each node of the tree be


processed once and only once in a
predetermined sequence. There are two
general approaches to the traversal
sequence, depth first and breadth first.
Depth first traversal is the processing
proceeds along a path from the root through
one child to the most distant descendent of
that first child before processing a second
child. In other words, in the depth-first
traversal, all of the descendents of a child are
processed before the next child.
Breadth first traversal, the processing
proceeds horizontally from the root to all of
its children, then to its children’s children,
and so forth until all nodes have been
processed. In other words, in the breadth-
first traversal, each level is completely
processed before the next level is started.
algorithm breadthFirst (val root <node pointer>)
Process tree using breadth-first traversal
Pre root is a pointer to a tree node
Post tree has been processed
1 pointer = root
2 while (pointer not null)
1 process (pointer)
2 if (pointer->left not null)
1 enque (pointer->left)
3 if (pointer->right not null)
1 enque (right pointer)
4 if (not emptyQueue)
1 deque (pointer)
5 else
1 pointer = null
3 return
end breadthFirst
In the Preorder Traversal, the root node is
processed first, followed by the left subtree,
and then the right subtree. It draws its name
from the latin prefix, pre, which means to go
before. Thus the root goes before the
subtrees.
algorithm preOrder (val root <node
pointer>)
Traverse a binary tree in node-left-
right sequence
Pre root is the entry node
of a tree or subtree
Post each node has been
processed in order
1 if (root is not null)
1 process (root)
2 preorder (root->leftSubtree)
3 preOrder (root->rightSubtree)
2 return
end preOrder
The Inorder Traversal processes the left
subtree first, then the root, and finally the
right subtree. The meaning of the prefix in is
that the root is processed in between the
subtrees.
algorithm inOrder (val root <node
pointer>)
Traverse a binary tree in a left-
node-right sequence
Pre root is the entry
node of a tree or subtree
Post each node has been
processed in order
1 if (root is not null)
1 inOrder (root->leftSubtree)
2 process (root)
3 inOrder (root->rightSubtree)
2 return
end inOrder
The Postorder traversal processes the root
node after (post) the left and right subtrees
have been processed. It starts by locating the
leftmost leaf and processing it. If then
processes its right sibling, including its
subtrees if any.
algorithm postOrder (valFinally,
root <node it processes the root
pointer>)
node.
Traverse a binary tree in a left-right-
node sequence
Pre root is the entry node
of a tree or subtree
Post each node has been
processed in order
1 if (root is not null)
1 postOrder (root->leftSubtree)
2 postOrder (root->rightSubtree)
3 process (root)

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.

Pre root is a pointer to


a non-empty BST
Return address of smallest node
1 if (root-> left null)
1 return (root)
2 return findSmallestBST (root->left)
end findSmallestBST
Find Largest Node returns the largest node
from the right branches to the last node in
the tree.
algorithm findLargestBST (val root
<pointer>)
This algorithm finds the largest
node in a BST.
Pre root is a pointer
to a non-empty BST
Return address of largest
node returned
1 if (root->right null)
1 return (root)
2 return findLargestBST (root-
>right)
end findLargestBST
Find a requested node returns a specific
node inalgorithm
the tree.searchBST (val root <pointer>,
val argument
<key>)
Search a binary search tree for a given
value.
Pre root is the root to a binary
tree or subtree
argument is the key value
requested
Return the node address if the
value is found
Null if the node is not in
the tree
1 if (root is null)
1 return null
2 if (argument < root->key)
1 return searchBST (root->left,
argument)
3 else if (argument > root->key)
1 return searchBST (root->right,
argument)
4 else
1 return root
end searchBST
 Insert Node performs insertion of an element in a BST by
first locating the insertion point and then shift all elements
from that point to the end of the array. Just follow the
branches to an empty subtree and then insert the new
node. In other words, all inserts take place at a leaf or a
leaflike
algorithm node,
insertBSTa(ref
noderootthat has one null branch.
<pointer>,
val new <pointer>)
Insert node containing new node into BST using iteration
Pre root is address of first node in a BST
new is address of node containing data to be
inserted
Post new node inserted into the tree
1 if (root is null)
1 root = new
2 else
1 pWalk = root
2 loop (pWlak not null)
1 parent = pWalk
2 if (new->key < pWalk->key)
1 pWalk = pWalk->left
3 else
1 pWalk = pWalk ->right
Location for new node found
3 if (new->key < parent->key)
1 parent->left = new
4 else
1 parent->right = new
3 return
end insertBST
 Recursive Insert Node. Inserts a new node in a BST (null
tree or subtree) by assigning the new node’s address to
replace the null tree. If we are not a null tree, then we
determine which branch we need to follow and call ourself
recursively to determine
algorithm addBST if we are at a leaf yet.
(ref root <pointer>,
val new <pointer>)
Insert node containing new data into BST using recursion
Pre root is address of first node in a BST
new is address of node containing data to be
inserted
Post new node inserted into the tree
1 if (root is null)
1 root = new
2 root->left = null
3 root->right = null
2 else
Locate null subtree for insertion
1 if (new->key < root->key)
1 addBST (root->left, new)
2 else
1 addBST (root->right, new)
3 return
end addBST
 Delete Node. To delete a node from a binary search tree, we must first
locate it. There are four possible cases when we delete a node.
1. The node to be deleted has no children.
 When the delete node has no children, all we need to do is set the delete
node’s parent to null, recycle its memory, and return.
2. The node to be deleted has only a right subtree.
 If there is only a right subtree, thenw e can simply attach the right subtree
to the delete node’s parent, recycle its memory, and we are done.
3. The node to be deleted has only a left subtree.
 If there is only a left subtree, then we attach the left subtree to the delete
node’s parent, recycle its memory, and we are done.
4. The node to be deleted has two subtrees.
 While it is possible to delete a node from the middle of a tree, the result
tends to create very unbalanced trees. Rather than simply delete the node,
therefore, we try to maintain the existing structure as much as possible by
finding data to take the deleted data’s place. This can be done in one of two
ways: (1) we can find the largest node in the deleted node’s left subtree and
move its data to replace the deleted node’s data, or (2) we can find the
smallest node on the deleted node’s right subtree and move its data to
replace the deleted nodes data. Regardless of which logic we use, we will be
exchanging data with a leaf or a leaflike node that can be then deleted. Prove
to yourself that either of these moves will preserve the integrity of the binary
search tree.
algorithm deleteBST (ref root <pointer>,
val dltKey <key>)
This algorithm deletes a node from a BST.
Pre root is pointer to tree containing data to be deleted
DltKey is key of node to be deleted
Post node deleted and memory recycled
If dltkey not found, root unchanged
Return true if node deleted, false if not found
1 if (root null)
1 return false
2 if (dltkey < root-> data.key)
1 return deleteBST (root->left, dltkey)
3 else if (dltkey > root-> data.key)
1 return deleteBST (root->right, dltKey)
4 else
Delete node found—Test for leaf node
1 if (root->left null)
1 dltPtr = root
2 root = root->right
3 recycle (dltPtr)
4 return true
2 else if (root->right null)
1 dltPtr = root
2 root = root->left
3 recycle (dltPtr)
4 return true
3 else
Node to be deleted not a leaf. Find largest node on left subtree
1 dltPtr = root->left
2 loop (dltPtr->right not null)
1 dltPtr = dltPtr->right
Node found. Move data and delete leaf node
3 root->data = dltPtr->data
4 return deleteBSt (root->left, dltPtr->data.key)
end deleteBST
AVL TREES
- A search tree in which the heights of the subtrees differ
by no more than one. It is a binary tree that is either
empty or that consists of two AVL subtrees, and whose
heights differ by no more than one.
Balancing Trees
- There are four cases that require rebalancing. All
unbalanced trees fall into one of these four cases:
(1) Left of left, a subtree of a tree that is left high has
also become left high.
(2) Right of right, a subtree of a tree that is right high
has also become right high.
(3) Right of left, a subtree of a tree that is left high has
become right high.
(4) Left of right, a subtree of a tree that is right high
has become left high.

You might also like