You are on page 1of 28

1

1
Trees
• tree: A directed, acyclic structure of linked nodes.
– directed : Has one-way links between nodes.
– acyclic : No path wraps back around to the same node twice.

– binary tree: One where each node has at most two children.
root
• Recursive definition: A tree is either:
– empty (NULL), or 1
– a root node that contains:
• data,
2 3
• a left subtree, and
• a right subtree.
– (The left and/or right 4 5 6 7
subtree could be empty.)
2
Trees in computer science
• folders/files on a computer
• family genealogy; organizational charts
• AI: decision trees =
• compilers: parse tree
– a = (b + c) * d; a *
• cell phone T9
+ d

b c

3
Terminology
• node: an object containing a data value and left/right children
• root: topmost node of a tree
• leaf: a node that has no children
• branch: any internal node; neither the root nor a leaf

• parent: a node that refers to this one root


• child: a node that this node refers to height = 3
• sibling: a node with a common level 1
1
• subtree: the smaller tree of nodes on
the left or right of the current node level 2 2 3
• height: length of the longest path
from the root to any node
• level or depth: length of the path level 3 4 5 6 7
from a root to a given node

4
A tree node for integers
• A basic tree node object stores data and refers to left/right
• Multiple nodes can be linked together into a larger tree

left data right


42

left data right left data right


59 27

left data right


86

5
Traversals
• traversal: An examination of the elements of a tree.
– A pattern used in many tree algorithms and methods

• Common orderings for traversals:


– pre-order: process root node, then its left/right subtrees
– in-order: process left subtree, then root node, then right
– post-order: process left/right subtrees, then root node
m_root

17

41 9

29 6 81 40
6
Pre order
[a+(b-c)]*[(d-e)/(f+g-h)]

7
Pre order traverse
• As we see the above Diagram according to this picture the pre
order traverse is

•*+a–bc/-de-+ fgh

8
Post order
[a+(b-c)]*[(d-e)/(f+g-h)]

• Traverse
– a b c - + d e – f g + h –/ *

9
Inorder
the ans of INORDERIS
DBFEAGCLJHK

10
Algorithm for pre-order
• PREORD(INFO,LEFT,RIGHT,ROOT)
• 1-[initialy push the null on STACK, and initialize the
PTR]
– Set top=1,STACK[1]=null and PTR=root
• 2-repeat the step 3 to 5 while PTR=! NULL
• 3-apply process to INFO [PTR]
• 4-if RIGHT[PTR]=!NULL the
– Set TOP=TOP+1, and STACK[TOP]=RIGHT[PTR]
• 5-if LEFT[PTR]!=NULL , then
– Set [PTR= LEFT[PTR]
– Else
– Set PTR=STACK[TOP]
– And TOP=TOP-1
• 6 EXIT 11
POST ORDER
• POSTORD(INFO,LEF,RIGHT,ROOT)
• 1-Push null onto STACK and initialize PTR
– Set TOP=1,STACK[1]=NULL and PTR=root
• 2-repeat steps 3 to 5 while PTR!= NULL
• 3-Set TOP=TOP+1 and STACK[TOP]=PTR
• 4-if RIGHT[PTR]!=NULL then setTOP=TOP+1 and
STACK[TOP]= -RIGHT[PTR]
• 5-set PTR=LEFT[PTR]
• 6-setPTR=STACK[TOP] and TOP=TOP-1

12
• 7- repaeat while PTR>0
– (a)apply process to INFO[PTR]
– (b)set PTR=STACK[TOP] and TOP=TOP-1
• 8- if PTR<0 then
– (a) set PTR= - PTR
– (b)goto step 2
• 9 EXIT

13
Post order

14
15
INORDER Algorithm
• INORD(INFO,LEFT,RIGHT,ROOT)
• [push NULL onto STACK and initilize PTR]
• 1- set TOP=1, STACK[1]=NULL and PTR= ROOT
• 2-repeat while PTR!=NULL
– (a)setTOP=TOP+1 and STACK[TOP=PTR]
– (b)set PTR=LEFT[PTR]
• 3-ser PTR=STACK[TOP] and TOP=TOP-1
• 4-repeat step 5 to 7 while PTR!=NuLL
• 5-apply process to INFO[PTR]
• 6-IF RIGHT[PTR]!=NULL then
– (a)set PTR= RIGHT[PTR]
– (b)goto step 3

16
• 7-set PTR=STACK[TOP] and TOP= TOP-1
• 8-EXIT

17
EXIAMPLE

18
in order

TRAVERSING USING STACKS

19
Built a tree:
13,03,55,21,60,33,58,19,96,09,02,62,08

20
Searching & Insertion
in binary search trees:
Suppose “A” is the given information,
For finding “A” in tree or inserting “A” as a
new node, the process is given below:
(a) Comparing “A” with node “N”
1.if A<N, proceed to left child of N
2.if A>N, proceed to right child of N
(b) Repeat step (a) until……
A=N (search is successful)
sub tree is empty (search is unsuccessful)
(c) insert ”A” at place of empty sub tree.

21
Example:

22
Deletion
in binary search trees:
Again
Suppose “ITEM” is the given information,
For finding “ITEM” in tree or inserting “A” as a
new node, the process is given below:
(a) Comparing “ITEM” with node “N”
1.if ITEM<N, proceed to left child of N
2.if ITEM>N, proceed to right child of N
(b) Repeat step (a) until……
ITEM=N (search is successful)
sub tree is empty (search is unsuccessful)
(c) Delete ”N” ,reset and Exit.

23
Suppose, we want to delete “33”
So, ITEM=33
and, initially N=60`

24
25
Data in form of tables:
Before Deletion:
15,25,33,44,50,60,66,75

26
Table after Deletion:
15,25,44,50,60,66,75

27
Table after Deletion:
15,25,44,50,60,66,75

28

You might also like