Binary Tree — up to 3 Related Nodes (List Is Special-Case)

Binary Tree — up to 3 Related Nodes (List Is Special-Case)

trees 1 are lists enough? for correctness — sure want to efficiently access items better than linear time to find something want to represent relationships more naturally 2 inter-item relationships in lists 1 2 3 4 5 List: nodes related to predecessor/successor 3 trees trees: allow representing more relationships (but not arbitrary relationships — see graphs later in semester) restriction: single path from root to every node implies single path from every node to every other node (possibly through root) 4 natural trees: phylogenetic tree image: Ivicia Letunic and Mariana Ruiz Villarreal, via the tool iTOL (Interative Tree of Life), via Wikipedia 5 natural trees: phylogenetic tree (zoom) image: Ivicia Letunic and Mariana Ruiz Villarreal, via the tool iTOL (Interative Tree of Life), via Wikipedia 6 natural trees: Indo-European languages INDO-EUROPEAN ANATOLIAN Luwian Hittite Carian Lydian Lycian Palaic Pisidian HELLENIC INDO-IRANIAN DORIAN Mycenaean AEOLIC INDO-ARYAN Doric Attic ACHAEAN Aegean Northwest Greek Ionic Beotian Vedic Sanskrit Classical Greek Arcado Thessalian Tsakonian Koine Greek Epic Greek Cypriot Sanskrit Prakrit Greek Maharashtri Gandhari Shauraseni Magadhi Niya ITALIC INSULAR INDIC Konkani Paisaci Oriya Assamese BIHARI CELTIC Pali Bengali LATINO-FALISCAN SABELLIC Dhivehi Marathi Halbi Chittagonian Bhojpuri CONTINENTAL Sinhalese CENTRAL INDIC Magahi Faliscan Oscan Vedda Maithili Latin Umbrian Celtiberian WESTERN INDIC HINDUSTANI PAHARI INSULAR Galatian Classical Latin Aequian Gaulish NORTH Bhil DARDIC Hindi Urdu CENTRAL EASTERN Vulgar Latin Marsian GOIDELIC BRYTHONIC Lepontic Domari Ecclesiastical Latin Volscian Noric Dogri Gujarati Kashmiri Haryanvi Dakhini Garhwali Nepali Irish Common Brittonic Lahnda Rajasthani Nuristani Rekhta Kumaoni Palpa Manx Ivernic Potwari Romani Pashayi Scottish Gaelic Pictish Breton Punjabi Shina Cornish Sindhi IRANIAN ROMANCE Cumbric ITALO-WESTERN Welsh EASTERN Avestan WESTERN Sardinian EASTERN ITALO-DALMATIAN Corsican NORTH SOUTH NORTH Logudorese Aromanian Dalmatian Scythian Sogdian Campidanese Istro-Romanian Istriot Bactrian CASPIAN Megleno-Romanian Italian Khotanese Romanian GALLO-IBERIAN Neapolitan Ossetian Khwarezmian Yaghnobi Deilami Sassarese Saka Gilaki IBERIAN Sicilian Sarmatian Old Persian Mazanderani GALLIC SOUTH Shahmirzadi Alanic Middle Persian Lurish Talysh Aragonese Arpitan CISALPINE LANGUE D'OÏL OCCITAN Rhaetian Pamiri Pashto Median Astur-Leonese Bukhori Bakhtiari Galician-Portuguese Emilian French Catalan Friulian Sarikoli Waziri Dari Kumzari Parthian Zaza-Gorani Mozarabic Ligurian Gallo Occitan Ladin Vanji Persian Old Spanish Eonavian Lombard Norman Romansh Yidgha Shughni Tat Balochi Gorani Asturian Fala Piedmontese Walloon Yazgulami Hazaragi Kurdish Zazaki Extremaduran Ladino Galician Venetian ARMENIAN Tajik Juhuru Leonese Spanish Portuguese Mirandese GERMANIC Armenian TOCHARIAN Old Norse EAST BALTO-SLAVIC Kuchean Old West Norse Old East Norse Elfdalian Burgundian Turfanian BALTIC SLAVIC Old Gutnish Crimean Gothic Faroese Danish Gothic WEST EAST EAST Greenlandic Norse Swedish Vandalic Icelandic WEST Galindan Latvian Old Novgorod Old East Slavic Norn Prussian Lithuanian Norwegian Old High German Old Saxon Sudovian Selonian Russian Ruthenian Semigallian WEST SOUTH LOW FRANCONIAN Yiddish Low German ALBANIAN Belorussian ANGLO-FRISIAN Old West Slavic WESTERN Rusyn WEST EAST Albanian EASTERN Ukrainian Old East Low Franconian UPPER GERMAN LECHITIC Slovene Old Dutch CENTRAL GERMAN Old Frisian Old English Czech-Slovak Old Church Slavonic Old Polish Knaanic Serbo-Croatian Limburgish Standard German Polabian Sorbian Czech Bulgarian Dutch Luxembourgish Alemannic North Frisian English Polish Pomeranian Slovak Bosnian Church Slavonic image: via Wikipedia/Mandrak Flemish Ripuarian Austro-Bavarian Saterland Frisian Scots Silesian Croatian Macedonian 7 Afrikaans Thuringian Swiss German Cimbrian West Frisian Yola Kashubian Serbian list to tree list — up to 2 related nodes predecessor element successor binary tree — up to 3 related nodes (list is special-case) parent element left child left child 8 more general trees tree — any number of relationships (binary tree is special case) at most one parent parent element child 1 child 2 … child n 9 tree terms (1) parent child A root: node with no parents B C siblings: nodes with the same parent D E F G leafs: nodes with no children H 10 paths and path lengths A path: sequence of nodes n1, n2, . , nk such that ni is parent of ni+1 example: {B, D, H} B C D E F G length (of path): number of edges in path H example: 2 (B → D and D → H) internal path length: sum of depth of nodes example: 6 = 1 + 2 + 3 11 tree/node height parent child A 3 height (of a node): length of longest path to leaf B 2 C 1 height (of a tree): height of tree’s root (this example: 3) D 1 E 0 F 0 G 0 H 0 12 tree/node depth parent child A 0 depth (of a node): length of path to root B 1 C 1 D 2 E 2 F 2 G 2 H 3 13 first child/next sibling home class TreeNode { private: string element; aaron TreeNode *firstChild; * TreeNode nextSibling; nextSibling public: ... cs2150 cs4970 mail }; firstChild lab1 lab2 proj1 coll.h coll.cpp proj.h 14 another tree representations class TreeNode { private: string element; vector<TreeNode *> children; public: ... }; // and more --- see when we talk about graphs 15 tree traversal / × × + - 5 6 1 2 3 4 pre-order: /*+12-34*56 in-order: (((1+2) * (3-4)) / (5*6)) (parenthesis optional?) post-order: 12+34-*56*/ 16 pre/post-order traversal printing (this is pseudocode) TreeNode::printPreOrder() { this−>print(); for each child c of this: c−>printPreOrder() } TreeNode::printPostOrder() { for each child c of this: c−>printPostOrder() this−>print(); } 17 in-order traversal printing (this is pseudocode) BinaryTreeNode::printInOrder() { if (this−>left) this−>left−>printInOrder(); cout << this−>element << "␣"; if (this−>right) this−>right−>printInOrder(); } 18 post-order traversal counting (this is pseudocode) int numNodes(TreeNode *tnode) { if ( tnode == NULL ) return 0; else { sum=0; for each child c of tnode sum += numNodes(c); return 1 + sum; } } 19 expression tree and traversals + (a + ((b + c) * d)) a * + d b c 20 expression tree and traversals + infix: (a + ((b + c) * d)) a * postfix: a b c + d * + prefix: + a * + b c d + d b c 21 postfix expression to tree use a stack of trees number n → push( n ) operator OP → pop into A, B; then push OP B A 22 e * + * + * d d e c + a b c + top of stack d bc e d e + a a b example a b + c d e + * * 23 e * + * + * d d e c + a b c + d c e d e + a b example a b + c d e + * * top of stack b a 23 e * + * + * d d e c + a b c + d bc e d e a example a b + c d e + * * top of stack + a b 23 * + * + * d e c + a b c + d bc e d e a example a b + c d e + * * e d top of stack c + a b 23 e * * + * d c + a b c + d bc e d e a example a b + c d e + * * + d e top of stack c + a b 23 e * + + * d d e a b c + bc d e a example a b + c d e + * * * c + top of stack d e + a b 23 e + d d e top of stack bc a example a b + c d e + * * * * + * c + a b c + d e d e + a b 23 element = 2 left = NULL right = addr of node 3 element = 7 left = NULL right = NULL binary trees all nodes have at most 2 children 1 class BinaryNode { ... int element; 2 BinaryNode *left; BinaryNode *right; 3 }; 4 1 5 2 3 6 4 5 6 7 7 24 element = 7 left = NULL right = NULL binary trees all nodes have at most 2 children 1 class BinaryNode { element = 2 ... int element; 2 left = NULL BinaryNode *left; right = addr of node 3 BinaryNode *right; 3 }; 4 1 5 2 3 6 4 5 6 7 7 24 element = 2 left = NULL right = addr of node 3 binary trees all nodes have at most 2 children 1 class BinaryNode { ... int element; 2 BinaryNode *left; BinaryNode *right; 3 }; 4 1 5 2 3 element =67 4 5 6 7 left = NULL right = NULL7 24 right subtree of 4 left subtree of 4 right subtree of 5 binary search trees binary tree and… each node has a key for each node: keys in node’s left subtree are less than node’s keys in node’s right subtree are greater than node’s 4 2 5 1 3 7 6 8 25 right subtree of 5 binary search trees binary tree and… each node has a key for each node: keys in node’s left subtree are less than node’s keys in node’s right subtree are greater than node’s 4 2 5 1 3 7 right subtree of 4 left subtree of 4 6 8 25 right subtree of 4 left subtree of 4 binary search trees binary tree and… each node has a key for each node: keys in node’s left subtree are less than node’s keys in node’s right subtree are greater than node’s 4 2 5 1 3 7 6 8 right subtree of 5 25 not a binary search tree 8 5 11 2 6 10 18 4 15 20 21 26 binary search tree versus binary tree binary search trees are a kind of binary tree …but — often people say “binary tree” to mean “binary search tree” 27 BST: find (pseudocode) find(node, key) { if (node == NULL) return NULL; else if (key < node−>key) return find(node−>left, key) else if (key > node−>key) return find(node−>right, key) else // if (key == node->key) return node; } 28 BST: insert (pseudocode) insert(Node *&node, key) { if (node == NULL) node = new BinaryNode(key); else if (key < node−>key) insert(node−>left, key); else if (key < root−>key) insert(node−>right, key); else // if (key > root->key) ; // duplicate -- no new node needed } 29 BST: findMin (pseudocode) findMin(Node *node, key) { if (node−>left == NULL) return node; else insert(node−>left, key); } 30 BST: remove (1) case 1: no children 5 5 4 9 4 9 1 7 11 1 11 3 3 31 BST: remove (2) case 2: one child 5 5 4 9 4 9 1 7 11 3 7 11 3 32 BST: remove (3) case 3: two children 5 7 4 9 4 9 1 7 11 3 11 3 replace with minimum of right

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    132 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us