Professional Documents
Culture Documents
Chapter 08 Trees
Chapter 08 Trees
Introduction
A tree consists of a nonempty finite set
of elements called nodes (vertices) and
a finite set of directed edges or lines
called branches that connect these
nodes.
A tree is an acyclic directed graph (does
not contain any cycles – never return to
starting point).
Common Tree Terminologies
Node (vertex) – Element in tree which
contains information and link to other node in
tree.
Branches– Connections between nodes
(vertices) that represents some relationship
associated with the nodes.
Parent – A node is a parent if it has one or
more children ( has out-degree more than
zero).
Common Trees Terminologies
Siblings – Children with the same
parent node.
Children – If N is a node with
successors X and Y, then X and Y are
the children of N.
Terminal node/leaf – A node that has
no children. A terminal node is also
called a leaf.
Common Trees Terminologies
Ancestor – All the nodes along the path from
the root to that node.
Descendant – All the nodes that are in its
subtree.
Traversal – Visiting the nodes of tree.
In-degree – The number of arrow or lines
coming into a node.
Out-degree – The number of arrow or lines
going out of a node.
Common Trees Terminologies
Depth of node – The number of steps
taken when moving upward from a
node to its parent.
Height – The number of branches from
the root to its farthest descendant.
Binary Tree Example
A
B C
D F G
E
H I K
J
B C
Exercise H
D
I
E F G
J K
L
Struct btree_node
{
int data; // Serves as data field
struct btree_node *left_child;// Points to left subtree
struct btree_node *right_child;// Points to right subtree
};
Binary Search Tree (BST)
Every non empty BST contain a value
(usually an integer but can be a string)
and satisfies the following conditions:
Every node or element has a value and no
two nodes in the BST has equal value.
Its left child (if exist) contain value smaller
than parent value.
Its right child (if exist) contain value larger
than parent value.
Binary Search Tree (BST)
Binary search tree begin by comparing
the search key with the data value
stored in root node.
If it matches the root key, the search is
over.
If no match is found, the search key is
determine whether it is less than or
greater than root node.
Binary Search Tree (BST)
If the search key is less than the root
value, the searching continue with the
left subtree or otherwise searching will
continue with the right subtree.
The searching is done recursively until
the key value is found.
Searching In Binary Tree
6388
2154 7100
6838 7133
3602
877
2154 7100
7500
2154 7100
7500
Inserting BST Algorithm
Assume binary tree T and NEWITEM to
insert.
1. Compare NEWITEM with the key value at
root node N of the tree.
• 1.1 If NEWITEM<N, proceed to the left child of N.
• 1.2 If NEWITEM>N, proceed to the right child of N.
2. Repeat step 1 until an empty place to insert
NEWITEM is found.
Deleting Binary Search Tree
Deleting a node from a binary tree
depends on how many children the
node ( to be deleted) has. There are
three cases of deletion:
Deletion of node without children.
Deletion of node with only one subtree or
child.
Deletion of node with two subtrees or
children.
Deletion of node without children
For a node without children, deletion is
done without further adjustment to the
tree.
Adjust the link field of parent to null
and remove the child from the tree.
Deletion of node without children
6388
2154 7100
7500
6388
2154 7100
7133
877 6838
3602 7500
6388
2154 7100
877 6838
3602 7500
3602
2154 7100
877 6838
7500
B C
D F G
E
B C
D F G
E
2154 7133
6838 8386
877 3602
B C
D F G
E
2154 7133
6838 8386
877 3602
B C
D F G
E
2154 7133
6838 8386
877 3602