lecture 17,18. .pptx

mhumza1976 14 views 28 slides Mar 01, 2025
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Jjj


Slide Content

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. Recursive definition: A tree is either: empty ( NULL ), or a root node that contains: data , a left subtree, and a right subtree. (The left and/or right subtree could be empty.) 7 6 3 2 1 5 4 root

Trees in computer science folders/files on a computer family genealogy; organizational charts AI: decision trees compilers: parse tree a = (b + c) * d; cell phone T9 d + * a = c b

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

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 59 left data right 27 left data right 86

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 40 81 9 41 17 6 29 m_root

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

Pre order traverse As we see the above Diagram according to this picture the pre order traverse is * + a – b c / - d e - + f g h

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

Inorder the ans of INORDERIS D B F E A G C L J H K

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

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

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

Post order

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

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

EXIAMPLE

in order TRAVERSING USING STACKS

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

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: 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.

Example:

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: 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.

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

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

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

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