Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.

Similar presentations


Presentation on theme: "Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can."— Presentation transcript:

1 Binary trees -2 Chapter 6 1

2 2

3 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals We have the pointers reference the next node in an inorder traversal; called threads We need to know if a pointer is an actual link or a thread, so we keep a Boolean for each pointer 3

4 Threaded Tree Example 4 8 75 3 11 13 1 6 9

5 5 Threaded Tree Traversal Start at the leftmost node in the tree, print it, and follow its right thread – following a thread to the right, will output the node and continue to its right – following a link to the right, will go to the leftmost node, print it, and continue

6 6 Threaded Tree Traversal -1 8 75 3 11 13 1 6 9 Start at leftmost node, print it Output 1

7 7 Threaded Tree Traversal -2 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3

8 8 Threaded Tree Traversal -3 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5

9 9 Threaded Tree Traversal -4 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6

10 10 Threaded Tree Traversal -5 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5 6 7

11 11 Threaded Tree Traversal - 6 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6 7 8

12 Amir Kamil8/8/0212 Threaded Tree Traversal - 7 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5 6 7 8 9

13 13 Threaded Tree Traversal -8 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6 7 8 9 11

14 14 Threaded Tree Traversal -9 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5 6 7 8 9 11 13

15 Threaded Tree Traversal Code 15 Node leftMost(Node n) { Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans; } void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); }

16 Traversal Through Tree Transformation (read only) Systematic visiting of all nodes in the tree. Each node visited once, does not specify order. Breadth-first traversal: visit each node, starting from the lowest level and moving down by level, visiting nodes from left to right. Depth-first traversals: go in subtree as deep as you can, backtrack. Differ on the order of visiting root, left, right subtrees. preorder: VLR. inorder: LVR. postorder: LRV. 16

17 Insertion searching does not modify the tree. To insert a new node with key el (new), a tree node with a dead end has to be reached, new node attached to it. found using same procedure as searching: compare key of currently scanned node el (cur) to el (new). If el (new) less than the key el (cur) try left child; otherwise try right child. If the child is empty, discontinue search and make the child point to a new node of key el. 17

18 18

19 Inserting in threaded tree 19 for inorder, only takes care of successors. Node with a right child: has its successor somewhere in the right subtree, does not need a thread. Why ? Threads are for "climbing up the tree", not for going down. A node with no right child has its successor somewhere. Inherits successor from parent. If a node becomes a left node, its parent is successor.

20 20

21 Deleting (try) 21

22 Balancing a tree The main idea of DSW algorithm is a rotation There are two types of rotation, left and right Rotate Right( Gr, Par, Ch ) – If Par is not the root of the tree Grandparent Gr of child Ch, becomes Ch’s parent by replacing Par; – Right subtree of Ch becomes left subtree of Ch’s parent Par; – Node Ch acquires Par as its right child 22

23 A picture will help 23

24 24

25 25

26 AVL tree The height of left and right subtree of every node differ by at most one. 26

27 AVL Trees27 Balanced and unbalanced BST 4 25 13 1 5 2 4 3 7 6 4 26 5713 Is this “balanced”?

28 AVL Trees28 Perfect Balance Want a complete tree after every operation – tree is full except possibly in the lower right This is expensive – For example, insert 2 in the tree on the left and then rebuild as a complete tree Insert 2 & complete tree 6 49 815 5 28 6914

29 AVL Trees29 AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node – height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node – For every node, heights of left and right subtree can differ by no more than 1 – Store current heights in each node

30 AVL Trees30 Node Heights 1 00 2 0 6 49 815 1 height of node = h balance factor = h left -h right empty height = -1 0 0 height=2 BF=1-0=1 0 6 49 15 1 Tree A (AVL)Tree B (AVL)

31 AVL Trees31 Node Heights after Insert 7 2 10 3 0 6 49 815 1 height of node = h balance factor = h left -h right empty height = -1 1 0 2 0 6 49 15 1 0 7 0 7 balance factor 1-(-1) = 2 Tree A (AVL)Tree B (not AVL)

32 AVL Trees32 Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to root node have possibly changed in height – So after the Insert, go back up to the root node by node, updating heights – If a new balance factor (the difference h left -h right ) is 2 or –2, adjust tree by rotation around the node

33 AVL Trees33 Single Rotation in an AVL Tree 2 10 2 0 6 49 815 1 0 7 0 1 0 2 0 6 4 9 8 15 1 0 7

34 AVL Trees34 Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. Insertions in AVL Trees

35 AVL Trees35 j k XY Z Consider a valid AVL subtree AVL Insertion: Outside Case h h h

36 AVL Trees36 j k X Y Z Inserting into X destroys the AVL property at node j AVL Insertion: Outside Case h h+1h

37 AVL Trees37 j k X Y Z Do a “right rotation” AVL Insertion: Outside Case h h+1h

38 AVL Trees38 j k X Y Z Do a “right rotation” Single right rotation h h+1h

39 AVL Trees39 j k X Y Z “Right rotation” done! (“Left rotation” is mirror symmetric) Outside Case Completed AVL property has been restored! h h+1 h

40 AVL Trees40 j k XY Z AVL Insertion: Inside Case Consider a valid AVL subtree h h h

41 AVL Trees41 Inserting into Y destroys the AVL property at node j j k X Y Z AVL Insertion: Inside Case Does “right rotation” restore balance? h h+1h

42 AVL Trees42 j k X Y Z “Right rotation” does not restore balance… now k is out of balance AVL Insertion: Inside Case h h+1 h

43 AVL Trees43 Consider the structure of subtree Y… j k X Y Z AVL Insertion: Inside Case h h+1h

44 AVL Trees44 j k X V Z W i Y = node i and subtrees V and W AVL Insertion: Inside Case h h+1h h or h-1

45 AVL Trees45 j k X V Z W i AVL Insertion: Inside Case We will do a left-right “double rotation”...

46 AVL Trees46 j k X V Z W i Double rotation : first rotation left rotation complete

47 AVL Trees47 j k X V Z W i Double rotation : second rotation Now do a right rotation

48 AVL Trees48 j k X V Z W i Double rotation : second rotation right rotation complete Balance has been restored h h h or h-1

49 AVL Trees49 Implementation balance (1,0,-1) key right left No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree

50 AVL Trees50 Example of Insertions in an AVL Tree 1 0 2 20 1030 25 0 35 0 Insert 5, 40

51 AVL Trees51 Example of Insertions in an AVL Tree 1 0 2 20 1030 25 1 35 0 5 0 20 1030 25 1 355 40 0 0 0 1 2 3 Now Insert 45

52 AVL Trees52 Single rotation (outside case) 2 0 3 20 1030 25 1 35 2 5 0 20 1030 25 1 405 0 0 0 1 2 3 45 Imbalance 35 45 00 1 Now Insert 34

53 AVL Trees53 Double rotation (inside case) 3 0 3 20 1030 25 1 40 2 5 0 20 1035 30 1 405 45 0 1 2 3 Imbalance 45 0 1 Insertion of 34 35 34 0 0 1 2534 0

54 Heap A heap is a binary tree T that stores a key- element pairs at its internal nodes. Properties : Min Heap: key(parent) < key(child) Max Heap: key(parent) > key(child)] 54

55 Example 55

56 Example of a complete binary max- heap if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap. 56

57 57

58 58

59 59

60 Max heap Terminate heap when – reach root – key parent is greater than key child 60

61 61

62 62

63 63

64 64

65 6.8 and 6.10 no need 65


Download ppt "Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can."

Similar presentations


Ads by Google