You are on page 1of 32

Tree

CHAPTER 5 LEC 1
Tree - Introduction
Tree is a non-linear data structure
Tree consist of nodes and arcs
A tree can be defined recursively as the following
1. An empty structure is an empty tree.

2. If t1, t2, … tk, are disjoint trees, then the structure


whose root has as its children the root of t1, t2, … tk
is also tree
3. Only structure generated by rules 1 and 2 are true
Tree – Introduction
Tree – introduction
Terminology
 Root – nodes that has no parent

 Leaves- nodes that have no children

 Path (edge)– the unique sequence of arcs to reach a


node
 Level of a node – the length of the path from the root
plus 1
 Height of non empty tree – maximum level of a node
in the tree
Tree – motivation
 Search in linked list needs n traversal, (worst case for
sorted linked list).
 If all the elements are stored in orderly tree, the number of
tests can be reduced substantially.
Binary tree
 A tree can have any number of children.

 A binary tree is a tree whose nodes have two children


(possibly empty), and each child is designated as either a
left child or right child.
Binary tree - types

Complete binary tree: all non-terminal nodes have both


their children, and all leaves are at the same level.
 at level i+1 we have 2i nodes
 the numbers of leaves m is greater than the number
of none terminal k and m = k + 1, k = m - 1
 we have 2i leaves, 2i-1 nonterminal nodes, and 2i+1-1
total node
Binary tree - types
 Decision tree: all nodes have
either zero or two nonempty
children.

 Binary search tree/ ordered binary tree: for each node n of


the tree, all values stored in its left subtree are less than value
v stored in n, and all values stored in the right subtree are
greater than v
Binary Search Tree
56, 26, 200, 18, 28,
56
190, 213, 12, 24, 27
26 200

18 28 190 213

12 24 27

Comp 122, Spring 2004


Binary tree - implementation
Array based
Linked list
Array structure
a node is decleard as an object with
left (int) left reference
right (int) riht reference
data ( any data type)
Binary tree- array
implementation
Example

Problem: deletion of an element would require moving


elements, which also require updating reference.
Binary tree –linked list
implementation (Node)
public class IntBSNode{
protected int key
protected IntBSNode left ,right;
public IntBSNode()
{ left = right = null; }
public IntBSNode(int el)
{ key=el; left = right = null; }
public IntBSNode(int el, IntBSNode lt, IntBSNode rt)
{ key=el; left = lt; right = rt; }
}
Binary tree –linked list
implementation (Tree)
public class IntBST{
protected IntBSNode root;
public IntBST()
{ root = null; }
public void insert (IntBSNode p, int el) { … }
public void delete (int el ) { … }

}
Searching a Binary Search Tree
Algorithm Code
For every node public IntBSNode search
Compare the key with the val on (IntBSNode p, int el) {
the node P=root;
If the key is less than the value , while(p!=null)
go to the left subtree and try
again if (el== p.key)
If it is greater than the val , try return p;
the right subtree else if(el<p.key)
Abort the search if it is the same p=p.left;
or no way to go(indicating the
else p=p.right
key is not on the tree)
Return null;
}
Searching- BST (Analysis)
Example; worst case for this graph is when it is searched for
26, 27, 28, 29 or 30(why?)

Worst case for BTS is O(log n)

is that?

Form tree for 3 4 5 6 7

Worst case can be O(n)


Tree traversal
 Is the process of visiting each node in the tree exactly one
time
 Depending on the order in which the nodes are visited we
can have n! combination
 Based on importance we would like to restrict our
attention to two class only
Breadth-first
Depth – first traversal
Tree traversal -Breadth-first
 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)
 Out of the four possibility one is top-down, left to right
 Example

13, 10, 25, 2, 12, 20, 31, 29


Tree traversal -Breadth-first
Tree traversal- Depth first
Depth – first traversal proceeds as far as possible to the left
(or right ), then backs up until the right (or left), and again as
far as possible to the left (or right).
There are three tasks of interest
V- visiting a node
L – traversing the left subtree
R –traversing the right subtree
3!=6, VLR, LVR, LRV, VRL, RVL. RLV
VLR – PreOrder Tree Traversal
LVR – InOrder Tree Traversal
LRV – PostOrder tree Traversa;
Tree Traversal – Depth first
Exercise: try non recursive implementation
for preorder tree traversal
Finding Maximum and Minimum
Values

Minimum: Go to the left most leaf starting from the root

Maximum: Go to the right most leaf starting from root


Insert to binary tree
Insert to binary tree (code)
Deletion
Complexity of deletion depends on the node

there are three cases to consider:


1. The node to be deleted is a leaf (has no children).
2. The node to be deleted has one child.
3. The node to be deleted has two children.
1. The node to be deleted is a leaf (has
no children).
 simply change the appropriate child field in the node's
parent to point to null, instead of to the node.
2. The node to be deleted has one child.
 “snip” the node out of this sequence by connecting its
parent directly to its child.
3. The Node to Be Deleted Has Two
Children
Two approach
Deletion by merging
Deletion by copying
Deletion by merging
Example
Deletion by copying
See u next class!!!

You might also like