Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS 3610 Midterm Review.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Binary Search Trees … From
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Tree.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Data Structure By Amee Trivedi.
AVL TREES.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic expression can be easily represented using Binary Tree. ( A + B ) + AB Operands Operator Traversals? InOrder PreOrder PostOrder Infix Prefix Postfix A + B + A B A B + Operators normally have maximum of 2 operands.

Types of Binary Trees Expression Tree: –A binary tree which stores an, Arithmetic expression where, Operands are stored as, –Leaves / Leaf nodes / External nodes of an expression tree and, Operators are stored as, –Internal nodes of an expression tree. –Traversals: InOrder: –Gives Infix notation of arithmetic expression. PreOrder: –Gives Prefix notation. PostOrder: –Gives Postfix notation of arithmetic expression.

Expression Tree Draw the expression tree for expression ‘(A + B)’. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + 1) A A ) B A B 3) + A B 4000 (Operator)(Operand) Evaluate it for A = 5, B = 6.

Expression Tree Draw the expression tree for expression ‘(A + B)’ and evaluate it for A = 5, B = 6. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + AAB+ AB Evaluation? ) A2) B3) +

Expression Tree ( ( A + B ) * ( C – D ) )A B + C D – * Postfix A B + C D – * A [A = 1, B = 2, C = 4, D = 2] AB+ AB C+ AB C+ AB D + AB - CD + AB - CD * + AB - CD * = AB+ CD -*

Expression Tree a * ( b + c ) / d ( ( a * ( b + c ) ) / d ) ( a * ( ( b + c ) / d ) ) a b c + * d / a b c + d / * a b c + * d / aab ab ab c c a+ + b c * * a+ b c d * a+ b c d / * a+ b c d / Postfix Evaluate: a = 2, b = 1, c = 1, d =

Expression Tree Draw the expression tree for following expression: ( a + b ) * c – d ^ e Evaluate the following expression tree for A = 4, B = 15, C = 5, D = 2, E = 3 + /* CBA^ DE

Types of Binary Trees A B C D EF G Main Disadvantage? 7 nodes 8 null links Linked Representation of a Binary Tree

Types of Binary Trees Threaded Binary Tree: –If there are n (n>0) nodes in a binary tree, No. of null link fields in linked representation would be: –n+1 –More than 50% of link fields are with null values and hence, Lot of memory space is wasted. –One way to use these null link fields has been given by Perlis & Thornton. Store pointers / links of some nodes in these null link fields. These extra pointers / links are called: –Threads

Threaded Binary Tree A B C D EF G NULL Inorder traversal: B A G E D F C Inorder Threading

Threaded Binary Tree A B C D EF G Preorder traversal: A B C D E G F Preorder Threading NULL

Types of Binary Trees Threaded Binary Tree: –3 ways to thread a binary tree: Inorder threading: –Left null link points to Inorder predecessor. –Right null link points to Inorder successor. Preorder threading: –Left null link points to Preorder predecessor. –Right null link points to Preorder successor. Postorder threading: –Left null link points to Postorder predecessor. –Right null link points to Postorder successor.

Types of Binary Trees Threaded Binary Tree: –Advantages: Traversal operation is faster. –Non-recursive traversal can be easily implemented and, –Non-recursive algorithm will work faster than recursive algorithm. Predecessor and Successor (Inorder / Preorder / Postorder) can be easily determined for any node.

Types of Binary Trees Threaded Binary Tree: –Disadvantage: How to distinguish / identify a link from a thread? –How to tell which is a link and which is a thread as both will contain pointers to nodes. 2 ways to overcome this disadvantage: –1] Node structure with 2 extra fields. »LTAG = 1: Pointer of LC is a Thread. »LTAG = 0: Pointer of LC is a Link. –Disadvantage: »2 extra bytes for TAG’s are required in each node. LTAGLCDATARCRTAG

Types of Binary Trees Threaded Binary Tree: –Disadvantage: How to distinguish / identify a link from a thread? –How to tell which is a link and which is a thread as both will contain pointers to nodes. 2 ways to overcome this disadvantage: –2] Store +ve value for Link and –ve value for Thread. »Get the actual pointer value by taking the magnitude / mod of the pointer. –Disadvantage: »Not possible in a programming language that does not allow –ve valued pointers.

Types of Binary Trees Tree T1 Tree T2 Tree T3 Heap Tree

Types of Binary Trees Heap Tree: –Suppose H is a complete binary tree. –It will be called a heap tree if it satisfies following property: For each and every node in H, –Value at N is greater than or equal to the value of each of children of N. OR For each and every node in H, –N has a value which is greater than or equal to the value of every successor of N.

Heap Tree Tree T1 Tree T2 Tree T3 No Yes Condition not satisfied for node 20. Not a complete binary tree. Which is a heap tree?

Heap Tree Tree T1 Yes Tree T2 Yes Max HeapMin Heap Max Value Min Value Which is a heap tree?

Types of Binary Trees Heap Tree: –Suppose H is a complete binary tree. –It will be called a heap tree if it satisfies following property: For each node in H, –Value at N is greater (or smaller) than or equal to the value of each of children of N. OR For each node in H, –N has a value which is greater (or smaller) than or equal to the value of every successor of N.

Heap Tree Representation of Heap Tree: –Any binary tree can be represented in the form of: Array (Sequential Representation) Linked List (Linked Representation) –Which representation is better for a Heap tree? ‘Array representation’ has certain advantages over ‘Linked representation’ in case of Heap tree. –There will be no / less memory wastage because, »A heap tree is a complete binary tree. –No empty space between 2 non-null entries. »If there are NULL entries, they will be at the tail of the array.

Heap Tree Max HeapMin Heap Index Value Array representation of Heap Tree

Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree

Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree

Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree

Heap Tree Insertion into a Heap Tree: –Exercise: Form / Build a Max Heap with the following data: –19, 55, 44, 98, 67, 48, 95, 66, 70, 69, 30, 24, 99, 82

Types of Binary Trees Max Heap Max Value Delete a value Any node can be deleted, but deleting root node has some special importance. Deletion from a Heap Tree

Types of Binary Trees Deletion from a Heap Tree: –Importance of deletion of root node: Deleting root node continuously from max heap gives: –Data sorted in descending order. Deleting root node continuously from min heap gives: –Data sorted in ascending order. –This sorting of data using a heap tree is called: Heap Sort

Heap Sort: –Works on the basic concept of: Heap Tree –It is always a complete binary tree. –Stored in the form of array. –2 types: »Max Heap »Min Heap –Basic steps in a heap sort (using max heap) are: Create heap from the array which is to be sorted. Repeat the following steps till heap tree is empty: –Delete root node from heap tree. –Rebuild heap after deletion. –Place the deleted node in output.

1. Create Heap Array A Array B Output Array A

2. Sort the Heap Heap Array A Array B Array B would not be required Directly swap the root with the last element in the heap tree.

2. Sort the Heap Heap Array A Iteration-1 Nij Iteration-2 ij

2. Sort the Heap Heap Array A Iteration-3 Nij Iteration-4 ij OUTPUT

Heap Sort BuildMaxHeap(A) For i = N down to 2 Swap( A[ 1 ], A[ i ] ) For j = 1 to i – 1 lchild = 2 * j rchild = 2 * j + 1 If( A[ j ] A[ rchild ] ) Swap(A[ j ], A[ lchild ]) j = lchild Else If( A[ j ] A[ lchild] ) Swap(A[ j ], A[ rchild ]) j = rchild Else Break; EndIf EndFor Create Max Heap 1 st iteration: Swap 1 st with last. 2 nd iteration: Swap 1 st with second last. And so on... Check the position of newly placed element with leftchild and rightchild. Rebuild the heap until the newly placed element comes to proper position.

Heap Sort Algorithm: –HeapSort Input: –Array A which is to be sorted. Output: –Array A with elements sorted in ascending order. Data Structure: –Array.

Heap Sort Steps BuildMaxHeap(A) For i = N down to 2 Swap( A[ 1 ], A[ i ] ) For j = 1 to i – 1 lchild = 2 * j rchild = 2 * j + 1 If( A[ j ] A[ rchild ] ) Swap(A[ j ], A[ lchild ]) j = lchild Else If( A[ j ] A[ lchild] ) Swap(A[ j ], A[ rchild ]) j = rchild Else Break; EndIf EndFor

Types of Binary Trees Binary Search Tree (BST) Heap Tree Tree T1Tree T2Tree T3

Types of Binary Trees Binary Search Tree (BST): –A binary tree is called a Binary Search Tree if, Each node N satisfies following property: The value at N is, –Greater than every value in the left sub-tree of N and is, –Less than every value in the right sub-tree of N.

Binary Search Tree (BST) Which is a BST? No Heap Tree (Min Heap) No Not satisfied for node with value 30. Tree T1Tree T2Tree T3 Yes

Binary Search Tree (BST) Which is a BST? Yes. Lexicographical ordering. Jan DecMar AugFeb JulNov Jun (Not numerical ordering)

Binary Search Tree (BST) Applications of BST Smallest / Minimum value?15 2. Largest / Maximum value?88 3. Search Value 54?Found. 4. Search Value 90?Not Found. 5. InOrder Traversal: Data sorted in Ascending Order. Hence also known as: Binary Sorted Tree.

Types of Binary Trees Creation of BST / Insertion into BST 20, 10, 30 20, 30, 10 10, 20, 30 10, 30, 20 30, 10, 20 30, 20, For a different sequence of the same set of data, there will be a different BST. Conclusion:

Binary Search Tree (BST) Applications of BST: –To find out the minimum / maximum value from a set of data. –To search a particular data from a set of data. –To sort the data and hence a BST is also called, Binary Sorted Tree. –To remove duplicate values from set of data.

Binary Search Tree (BST) Create a BST for data '57, 28, 25, 69, 88, 81, 74, 65, 19, 15'

Types of Binary Trees Binary Search Tree (BST): –Consider the following set of data: 10, 20, 30, 40, 50, 60 –No. of ways to arrange this data: 6! = 720 Some ways to arrange the data are: –10, 20, 30, 40, 50, 60 –60, 50, 40, 30, 20, 10 –30, 20, 40, 10, 50, 60 –20, 40, 30, 10, 50, 60 –So number of Binary Search Trees (BST) that can be constructed are: 720.

Binary Search Tree (BST) 10 10, 20, 30, 40, 50, , 50, 40, 30, 20,

Binary Search Tree (BST) 30 30, 20, 40, 10, 50, 6030, 20, 50, 10, 40,

Binary Search Tree (BST) Question: In which tree, searching operation will be faster? Looks more balanced.

Binary Search Tree (BST) When can we say that a tree is balanced or not? Depends on the balance factor of any node. What is this balance factor of a node? Balance Factor (bf) = Height of left sub-tree (h L ) – Height of right sub-tree (h R )

Types of Binary Trees Height Balanced Tree Calculating Balance Factor of every node h L = 2 h R = 2 2 – 2 = 0 0 h L = 1 h R = 0 1 – 0 = = – 0 =

Types of Binary Trees Height Balanced Tree: –A binary search tree is said to be a height balanced tree if, All its nodes have a balance factor of 1, 0 or -1. That is: –| bf | = | h L – h R | <= 1 for every node in the tree.

Types of Binary Trees Which is a Height Balanced Tree? T1 3-2 = 1 1-2= = = = = 0 Yes = T2 No Balance Factor not satisfied for root node (6)

Types of Binary Trees Height Balanced Tree: –Properties: Every Height Balanced Tree is always a Binary Search Tree (BST) whereas, –Every Binary Search Tree (BST) might not be a Height Balanced Tree. Complete Binary Search Tree (BST) is always a Height Balanced Tree. –Note: It is also called AVL tree after the name of the scientists: –Adelson-Velskii, Lendis.

Height Balanced Tree Creation of BST / Insertion into BST Create different BSTs for set of data 10, 20, 30. Arrangement of 3 elements can be done in: 1)20, 10, 30 2)20, 30, 10 3)10, 20, 30 4)30, 20, 10 5)10, 30, 20 6)30, 10, 20 3! = 6 ways

Height Balanced Tree 20, 10, Height Balanced Tree 0 0 0

20, 30, Height Balanced Tree 0 0 0

Left Rotation 10, 20, Not Height Balanced Left Rotation Height Balanced Tree

Right Rotation 30, 20, Right Rotation 2 Not Height Balanced Height Balanced Tree

10, 30, Not Height Balanced Left Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree

Height Balanced Tree Left-Right Rotation 10, 30, Not Height Balanced -2 Right Rotation Not Height Balanced Binary Search Tree Left Rotation Height Balanced Tree

Height Balanced Tree 30, 10, Right Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree 2 Not Height Balanced

Height Balanced Tree Right-Left Rotation 30, 10, Left Rotation Right Rotation 2 Not Height Balanced Binary Search Tree Height Balanced Tree

Height Balanced Tree AVL Rotations: –4 types: Left Rotation: –Also known as ‘LL Rotation’. –Involves only a single Left Rotation. Right Rotation: –Also known as ‘RR Rotation’. –Involves only a single Right Rotation. Left Right Rotation: –Also known as ‘LR Rotation’. –Involves 2 rotations: »First a Right, then a Left. Right Left Rotation: –Also known as ‘RL Rotation’. –Involves 2 rotations: »First a Left, then a Right.

Tree General Tree & Not a Binary Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter

Tree Representation of Tree: –A tree can be represented in a computer using: Array Linked List

Tree Array representation of Tree Mono Sumo KapilRaviDickHariLiza--Azu Mono Sumo KapilRaviDickHariLizaAzuMaryJohnPrem Karan … Will not work Works

Tree Array representation of Tree Mono Sumo Kapil Ravi Dick Hari Liza Azu Mary John Prem Karan Peter Tom DATA Array LEFT CHILD Array SIBLING Array INDEX

Tree Linked representation of Tree Fixed Size Node (Version 1) Mono Sumo KapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom Advantage: Easy Algorithm Disadvantage: Null Links

Tree Linked representation of Tree Variable Size Node (Version 2) Mono4 Sumo2Kapil1Ravi2Dick0 Hari0Liza3Azu0Mary1John0 Prem0Peter0Karan0Tom0 Advantage: Memory Saved Disadvantage: Complex Algorithm

Tree General Tree using a Linked Representation of Binary Tree Mono Sumo Left Child Sibling KapilRaviDick HariLizaAzuMaryJohn Peter Prem Karan Tom

Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter Binary Tree representation of Tree Mono SumoKapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom

Forest A DCB EF W ZYX P SRQ T1 T2 T3

Forest Forest: –Forest is a collection of disjoint trees. It can also be represented in the form of: –Linked Representation of Binary Tree.

Forest A DCB EF W ZYX P SRQ T1 T2 T3 Conversion of Forest to a Binary Tree A DCB EF T1’ W ZYX P SRQ T2’ T3’

Forest Conversion of Forest to a Binary Tree A DCB EF T1’ W ZYX P SRQ T2’ T3’

Trees FF F F F F F F F F F F F F F F Binary Search Trees / 2-way Search Trees Height Balanced Trees Failure Nodes are not on same level.All Failure nodes are on same level. B Tree of Order 2 B Tree

2040 F X FFFFFFF FF 3-way Search Tree. Not a B Tree. All Failure nodes are not on the same level. Is it a B Tree?

B Tree 30X F 20X X FFFFFFFFF 3-way Search Tree. YesIs it a B Tree? 25X35X4550 All Failure nodes are on the same level. B Tree of Order 3

B Tree General Node Structure of B Tree of Order m. P1P1 K1K1 P2P2 K2K2 P3P3 K3K3 P4P4 ….K m-1 PmPm Where: Ki: Key values in the node. Pi: Pointers to sub-trees.

B Tree B Tree: –A ‘B Tree’ of order m is an, m-way search tree where, All failure nodes are at the same level where, –A failure node is, »A node which can be reached during a search only if, »The value being searched for is not in the tree. –Applications: Used in index creation in database. –Note: Every B Tree will always be a Search Tree however, –Every Search Tree might not be a B Tree.

B Tree Construct a B Tree of order 3 for following set of data: 10, 20, 30, 40, 50, 60, 70 10X 20 X 30 10X30X 20X 40 10X3040 X 70 20X60X 10X30X50X70X