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

Clean Tree Notes 20

Pages 11-20

Uploaded by

george.due
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views

Clean Tree Notes 20

Pages 11-20

Uploaded by

george.due
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
05-Jan-19 DEPENDING ON THE DEPTH / LEVEL OF BINARY TREE, IT CAN BE CLASSIFIED. 3. ALMOST COMPLETE BINARY TREE: * A binary tree of depth / level ‘d’ is known as almost complete binary tree if each node in the tree is either at level d ord—1. LINKED LIST REPRESENTATION POINTER TO LEFT. eee cm SUB-TREE (LEFT Reece EE (RIGHT. CHILD NODE) CHILD NODE) As above mentioned in diagram we have three members for structure while working with linked list representation of trees. [Left Pointer — DATA — Right Pointer] struct node { int data; struct node *left_tree; struct node *right_tree; k 1. 05-Jan-19 LINKED LIST REPRESENTATION TREE TRAVERSAL TECHNIQUES QO Traversal methods are used to display data from the tree. CQ) In this techniques, each node of tree is visited only once. O Followings are different traversal techniques: 1. Preorder Traversal (ROOT — LEFT — RIGHT) 2. Inorder Traversal (LEFT —- ROOT — RIGHT) 3. Postorder Traversal (LEFT — RIGHT — ROOT) 12 05-Jan-19 TREE TRAVERSAL TECHNIQUES Q Preorder Traversal: 1. Visit the root node. 2. Traverse the left sub-tree — Recursively. 3. Traverse the right sub-tree — Recursively. In the preorder traversal (D) is used for root node, (L) for left sub-tree and (R) is used for right sub-tree. So, the preorder traversal is D—L—R. TREE TRAVERSAL TECHNIQUES D- PRE ORDER TRAVERSAL ROOT TECHNIQUE [D —L—R] L- R- LEFT RIGHT SUB -TREE SUB -TREE 13 05-Jan-19 TREE TRAVERSAL TECHNIQUES Consider following tree. The sequence for preorder traversal is: 1) Root Ais processed. 2) After that the left sub-tree of root is processed. 2.1) To process the left sub-tree, first process the root of left sub-tree i.e. B. 2.2) Then its left sub-tree (i.e. C) and its right sub-tree (D). 3) Now, start the processing for right sub-tree of root (i.e. E). And do the recursive process as we have done for left sub-tree. DEG R ALGORITHM FOR Pre-Order Binary Tree: Temp = pointer variable initialized with root. Data = data member of structure node Left = pointer to left child node and member pointer of structure node. Right = pointer to right child node and member pointer of structure node. Step — 1: Repeat step 2,3,4 and check temp is not null Step 2: Print data of node Step — 3: Call function itself as a left most node. Step -4: Call function itself as a right most node. 14 05-Jan-19 TREE TRAVERSAL TECHNIQUES Q Inorder Traversal: 1. Traverse the left sub-tree — Recursively. 2. Visit the root node. 3. Traverse the right sub-tree — Recursively. In the inorder traversal (D) is used for root node, (L) for left sub-tree and (R) is used for right sub-tree. So, the inorder traversal is L—-D—R. TREE TRAVERSAL TECHNIQUES D- IN ORDER TRAVERSAL ROOT TECHNIQUE [L- D—R] L- R- LEFT RIGHT SUB -TREE SUB -TREE 15 05-Jan-19 TREE TRAVERSAL TECHNIQUES Consider following tree. The sequence for inorder traversal is: 1) Left Sub-tree processed first. 1.1) For that process upto the left most leaf node of the tree (ie.c). 1.2) Then process to the root of left most leaf node of the tree (i.e. B) 1.3) And then process to the root of right most leaf node of the tree (i.e. D). 2) Now, process the root node (i.e. A). 3) After processing of root node, now process its right sub-tree as per the rule of inorder traversing (i.e. L-D-R). ALGORITHM FOR InOrder Binary Tree Temp = pointer variable initialized with root. Data = data member of structure node Left = pointer to left child node and member pointer of structure node. Right = pointer to right child node and member pointer of structure node. Step — 1: Repeat step 2,3,4 and check temp is not null Step ~ 2: Call function itself as a left most node. Step - 3: Print data of node. Step -4: Call function itself as a right most node. 16 05-Jan-19 TREE TRAVERSAL TECHNIQUES Q Postorder Traversal: 1. Traverse the left sub-tree — Recursively. 2. Traverse the right sub-tree — Recursively. 3. Visit the root node. In the postorder traversal (D) is used for root node, (L) for left sub-tree and (R) is used for right sub-tree. So, the postorder traversal is L—R—-D. TREE TRAVERSAL TECHNIQUES D- POST ORDER TRAVERSAL ROOT TECHNIQUE [L- R-D] L- R- LEFT RIGHT SUB -TREE SUB -TREE 17 05-Jan-19 TREE TRAVERSAL TECHNIQUES Consider following tree. The sequence for postorder traversal is: 1) Left Sub-tree processed first. 1.1) For that process upto the left most leaf node of the tree (ie. C). 1,2) Then process to the right sub-tree (i.e, D) and then the root node (i.e. B). 2) Now, follow the rule L-R-D and process the right sub-tree. 2.1) That is, as per the rule of post order (L-R-D), first process the left sub-tree (i.e. F) 2.2) Then process right sub-tree (i.e. G) and then its parent (i.e. E), 3) At last process the root A. Jojeleicje] a | L R D ALGORITHM FOR PostOrder Binary Tree: Temp = pointer variable initialized with root. Data = data member of structure node Left = pointer to left child node and member pointer of structure node. Right = pointer to right child node and member pointer of structure node. Step — 1: Repeat step 2,3,4 and check temp is not null Step ~ 2: Call function itself as a left most node. Step — 3: Call function itself as a right most node. Step -4: Print data of node. 18 05-Jan-19 BINARY TREE PROGRAM BINARY SEARCH TREE (BST. QA Binary Search Tree is a binary tree, which is either empty or satisfies Q Every node has unique value. Uf there is an existence of left child (left sub-tree) then its value is less than the value of the root. Cif there is an existence of right child (right sub-tree) then its value is greater (larger) than the value of Si! the root. 19 05-Jan-19 BINARY SEARCH TREE (BST. O The primitive operations performed on BST are: + Inserting a node “ Searching a node * Deleting a node BINARY SEARCH TREE PROGRAM BALANCED BINARY TREES QA largest path through the left sub-tree is the same length as the largest path through the right sub-tree is called a BALANCED BINARY TREE. Un this, the searching time is very less as compared to unbalanced binary tree. O There are two types of Balanced Trees: QHeight Balanced Trees Weight Balanced Trees 20

You might also like