Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

Similar presentations


Presentation on theme: "ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners."— Presentation transcript:

1 ISOM MIS 215 Module 5 – Binary Trees

2 ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners MIS215 Binary Search Search Techniques Sorting Techniques Bubblesort Basic Algorithms Fast Sorting algos (quicksort, mergesort) Hashtables Graphs, Trees Linked Lists Stacks, Queues List StructuresAdvanced structures

3 ISOM Today’s buzzwords Binary trees  Linked structures where a node can be connected to at most two other nodes Binary search trees  Trees designed for searching – the left subtree has items “smaller” than the node item, the right subtree has “bigger” Balanced binary search trees  Binary trees that adjust the height to ensure that the subtree heights are same Key  A component of an object that is typically used for quick retrieval of the object

4 ISOM Binary Trees: A Definition DEFINITION A binary tree is either empty, or it consists of a node called the root together with the two binary trees called the left subtree and the right subtree of the root Some examples of binary trees are given below: one empty binary tree, one binary tree with one node, and two with two nodes. These are different from each other. and

5 ISOM Binary Tree: More Examples We never draw any part of a binary tree to look like The binary trees with three nodes are a b c a b c a b c a b c a b c

6 ISOM Traversal of binary trees There are four standard traversal orders:  With preorder traversal we first visit a node, then traverse its left subtree, and then traverse its right subtree.  With inorder traversal we first traverse the left subtree, then visit the node, and then traverse the right subtree.  With postorder traversal, we first traverse the left subtree, then the right subtree, and then vist the node.  With level-order traversal, we traverse the tree by levels – the top level first, then the next level, and so on, visiting the nodes left-to-right.

7 ISOM Traversal exercises a b c a b c a b c a b c a b c 1 234 5 Inorder: Preorder: Postorder: Level-order:

8 ISOM Expression Trees + a a a b b c a+b a-(b*c) (a<b)or(c<d) x x - log ! n or bc d <<

9 ISOM A Linked Binary Tree Jim Dot Amy Tim Guy Ann Kay Ron Eva JanJon Kim Roy Tom

10 ISOM Binary Search Trees: A Definition DEFINITION: A binary search tree is a binary tree that is either empty or in which every node contains a key and satisfies the conditions:  The key in the left child of a node(if it exists) is less than the key in its parent nodes.  The key in the right child of the node (if it exists) is greater than the key in its parent nodes.  The left and right sub-trees of the root are again binary search trees.

11 ISOM Binary Search Trees: Some Comments We can regard binary search tree as a new ADT. We can regard binary search tree as a specialization of binary trees. We may study binary search trees as a new implementation of the OrderedList.

12 ISOM Binary Search Trees with same Keys

13 ISOM Tree Insertion e b e b e d d f b e d f b e a d f b e a g d f b e a g c (a) insert e (b) insert b (c) insert d (d) insert f (e) insert a (f) insert g (g) insert c

14 ISOM Deletion in a Binary Search Tree: General Cases x Delete x Case 1: deletion of a leaf x x Delete x y y Case 2: x has no right sub-tree x y z y z Delete x Case 2: x has two sub-trees

15 ISOM Deletion in a Binary Search Tree x b r a y z Delete y b x a z x b r c y z a b c a x y z

16 ISOM AVL Trees: A Height Balance Technique Definition: An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees. With each node of an AVL tree is associated a balance factor that is left higher, equal or right higher according, respectively, as the left sub-tree has height greater than, equal to, or less than that of the right sub-tree.

17 ISOM Examples of AVL and Non-AVL Trees - - / - \ / - - - \ - - - - / / - - - -- -- Non-AVL trees AVL trees

18 ISOM Insertions into an AVL tree

19 ISOM Insertions into an AVL tree: Change the Order of the List m k m u k m k u t v v t u m p k m k u m k u t v p Rotate left Double rotation left

20 ISOM Deletions in AVL Trees: Sample Cases (Part 1) T1 T2 - T1 T2 \ p p Deleted T1 T2 / T1 T2 - p p Deleted Height unchanged Height reduced Case 2 Case 1

21 ISOM Deletions in AVL Trees: Sample Cases (Part 2) Deleted T1 T3 - p Height unchanged Height reduced T2 \\ q h-1 h h T1 T3 \ q T2 / p h-1 h h T1 T3 \ p Deleted T2 \\ q h-1 h T1 T3 - q T2 - p h-1 h Case 3b Case 3a


Download ppt "ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners."

Similar presentations


Ads by Google