Additional Tree Traversal Example #1

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
Chapter 4: Trees Binary Search Trees
CS 367 – Introduction to Data Structures
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures AVL Trees.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Search Trees.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL Tree Mohammad Asad Abbasi Lecture 12
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
Cinda Heeren / Geoffrey Tien
Lecture 18. Basics and types of Trees
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Tree A Balanced Binary Search Tree
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Data Structures & Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
short illustrative repetition
AVL Tree By Rajanikanth B.
AVL Trees (a few more slides)
AVL-Trees (Part 1).
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Trees B.Ramamurthy 4/27/2019 BR.
AVL Tree Chapter 6 (cont’).
AVL Trees (Adelson – Velskii – Landis)
AVL-Trees (Part 2).
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Additional Tree Traversal Example #1 // Public function to “spell out” the values from the root to the tree’s // // leaf nodes, with the results output on a separate line for each leaf. // template <class E> void BinaryTree<E>::spellFromTop(ofstream &os) { continueSpelling(root, os, ""); } // Protected function to concatenate the char value of the current subtree’s // // root to the str param. until a leaf is reached, whereupon str is output. // void BinaryTree<E>::continueSpelling(nodePtr treeRoot, ofstream &os, string str) if (treeRoot == NULL) return; else str += char(treeRoot->data); if ((treeRoot->left == NULL) && (treeRoot->right == NULL)) os << str << endl; continueSpelling(treeRoot->left, os, str); continueSpelling(treeRoot->right, os, str); CS 240 Chapter 10 – Trees

Applying spellFromTop to a Sample Tree str: “M” M I C E L K O P S M str: “MI” I O str: “MO” str: “MIC” str: “MOM” str: “MOP” str: “MIL” C L M P output “MOM” str: “MILK” str: “MICE” str: “MOPS” str: “MILL” E K L S output “MICE” output “MILK” output “MILL” output “MOPS” CS 240 Chapter 10 – Trees

Additional Tree Traversal Example #2 // Public function to output the sequence of left and right offspring that // // comprise the longest path from the tree’s root to one of its leaf nodes. // template <class E> void BinaryTree<E>::outputLongestPath(ofstream &os) { os << longestPath(root); } // Protected function to recursively generate a string indicating which // // offspring (left or right) yield the longest path from the root to a leaf. // string BinaryTree<E>::longestPath(nodePtr treeRoot) if (treeRoot == NULL) return “”; else string leftStr = longestPath(treeRoot->left); string rightStr = longestPath(treeRoot->right); if (leftStr.size() > rightStr.size()) leftStr.insert(0, ‘L’); return leftStr; rightStr.insert(0, ‘R’); return rightStr; CS 240 Chapter 10 – Trees

Applying outputLongestPath to a Sample Tree Final result: “LLRR”             leftStr: “LLRR” rightStr: “RRL” “LRR” “RL” leftStr: “LRR”   leftStr: “LL” rightStr: “” rightStr: “RL” “L” “L” “RR”   leftStr: “L”  leftStr: “L” leftStr: “” rightStr: “” rightStr: “” rightStr: “RR” “” “” “R”   leftStr: “” leftStr: “L”  leftStr: “” rightStr: “” rightStr: “R” rightStr: “” “” “” leftStr: “”   leftStr: “” rightStr: “” rightStr: “” CS 240 Chapter 10 – Trees

AVL Trees Although an average search in an n-node binary search tree is O(logn), the worst case could be as bad as O(n). An AVL (Adelson-Velskii and Landis) tree places a balance condition on a binary search tree by requiring the left and right subtrees of each node to have heights differing by at most one. During insertion, this is accomplished by means of single and double rotations. Single rotations: Note that in each of the trees illustrated below: (any element of X)  k1  (any element of Y)  k2  (any element of Z) k1 k2 X Z Y So when a new element’s insertion causes an imbalance, “rotate” the tree to restore the balance. CS 240 Chapter 10 – Trees

Single Rotation Examples 27 14 31 12 30 45 27 14 31 12 30 45 5 27 12 31 5 30 45 14 INSERT 5 ROTATE 84 75 93 92 98 84 75 93 92 98 99 93 84 98 75 99 92 INSERT 99 ROTATE CS 240 Chapter 10 – Trees

Double rotations: If a single rotation doesn’t restore balance, a double rotation will. Note that in the two trees illustrated below: (any value in A)  k1  (any value in B)  k2  (any value in C)  k3  (any value in D) k3 k2 D C B k1 A Also note that in the two trees illustrated below: (any value in E)  k1  (any value in F)  k2  (any value in G)  k3  (any value in H) k3 k2 H G F k1 E If a single rotation fails to restore balance after a new insertion, a double rotation may be tried. CS 240 Chapter 10 – Trees

Double Rotation Example 25 16 49 9 36 64 19 31 41 25 16 49 9 36 64 19 31 41 47 SINGLE ROTATION 25 16 36 9 31 49 19 41 64 47 INSERT 47 STILL UNBALANCED 25 16 49 9 36 64 19 31 41 25 16 49 9 36 64 19 31 41 47 DOUBLE ROTATION 25 16 41 9 36 49 19 47 64 31 INSERT 47 BALANCED! CS 240 Chapter 10 – Trees