0% found this document useful (0 votes)
8 views

Binary Tree 1

Uploaded by

Seankolt 33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Binary Tree 1

Uploaded by

Seankolt 33
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Binary Trees 1

Outline
• Binary Trees and Binary Search Trees
• Implementing Binary Trees
• Insertion and Deletion in Binary Search Trees
• Tree Traversal
Definitions
• A tree is a data type that
consists of nodes and arcs
• These trees are depicted
upside down with the root at
the top and the leaves
(terminal nodes) at the
bottom
• The root is a node that has no
parent
• Leaves have no children
Definitions
• Each node must be reachable from the root through a
unique sequence of arcs, called a path
• The number of arcs in a path is called the length of the
path
• The level of a node is the length of the path from the
root to the node plus 1, which is the number of nodes in
the path
• The height of a nonempty tree is the maximum level of a
node in the tree
Binary Trees
• A binary tree is a tree whose nodes have two children
(possibly empty), and each child is designated as either
a left child or a right child
Binary Trees
• A complete binary tree is tree where all nonterminal
nodes have both their children, and all leaves are at the
same level
• A decision tree is a binary tree in which all nodes have
either zero or two nonempty children
Binary Search Tree
• binary search tree (BST) or ordered binary tree is
binary tree with the following properties;
– for each node n of the tree, all values stored in its left subtree
are less than value v stored in n
– all values stored in the right subtree are greater than v.
Implementing Binary Trees
• Binary trees can be implemented in at least two ways:
– As arrays
– As linked structures
• To implement a tree as an array, a node is declared as
an object with an information field and two “reference”
fields that points to the left and right child nodes
Implementing Binary Trees

Array representation of the binary tree


Implementing BST

Node of an integer binary search tree


Searching a BST
1. For every node, compare the key to be located with the
value stored in the node currently referred.
2. If the key is less than the value, go to the left subtree
and try again.
3. If the key is greater than the value, go to the right
subtree and try again.
4. If it is the same, the search can be discontinued.
5. The search is also aborted if there is no way to go,
indicating that the key is not in the tree.
Searching a BST
• The internal path length (IPL) is the sum of all path
lengths of all nodes
• It is calculated by summing Σ(i – 1)li over all levels i,
where li is the number of nodes on
level I
• A depth of a node in the tree is determined by the path
length
• An average depth, called an average path length, is
given by the formula IPL/n, which depends on the shape
of the tree
Searching a BST
Inserting Nodes into a BST
Inserting Nodes into a BST
Deleting a Node from a BST
• There are three cases of deleting a node from the binary
search tree:
– The node is a leaf; it has no children
– The node has one child
– The node has two children
Deleting a Node from a BST

Deleting a leaf

Deleting a node with one child


Delete by Merging
• Making one tree out of the two subtrees of the node and
then attaching it to the node’s parent is called deleting
by merging
Delete by Merging

The height of a tree can be (a) extended or (b) reduced after deleting by merging.
Deletion by Copying
• If the node has two children, the problem can be reduced
to:
– The node is a leaf
– The node has only one nonempty child
• Solution: replace the key being deleted with its
immediate predecessor (or successor)
• A key’s predecessor is the key in the rightmost node in
the left subtree
Deletion by Copying

Deletion by copying
Tree Traversal
• Tree traversal is the process of visiting each node in the
tree exactly one time
• Breadth-First traversal is visiting each node starting
from the lowest (or highest) level and moving down (or
up) level by level, visiting nodes on each level from left to
right (or from right to left)
Breadth-First Traversal

Top-down, left-to-right, breadth-first traversal implementation


Depth-First Traversal
• Depth-First traversal proceeds as far as possible to the
left (or right), then backs up until the first crossroad, goes
one step to the right (or left), and again as far as
possible to the left (or right)
• Three tasks of interest for depth-first traversal
– V—Visiting a node
– L—Traversing the left subtree
– R—Traversing the right subtree
Depth-First Traversal
• The depth-first traversal can be configured in three ways
if the move is always left to right (or right to left)
o VLR—Preorder tree traversal
o LVR—Inorder tree traversal
o LRV—Postorder tree traversal
Depth-First Traversal

Depth-first traversal implementation


Depth-First Traversal

Inorder tree traversal


End Part 1

You might also like