30
Introduction to Trees 1 Joe Meehean

1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X Terminology each circle is a node pointers are edges topmost node is the root

Embed Size (px)

Citation preview

CS242 Data Structures II

Introduction to Trees1Joe Meehean

Conceptual PictureABDICEXConceptual PictureABDICEXTerminologyeach circle is a nodepointers are edgestopmost node is the rootbottom nodes are leavesno outgoing edgesEvery non-empty tree hasone rootone or more leavesMore terminologyNode A is the parent of node BNode B is the child of node AThe root has no parentAll other nodes have exactly 1 parentABMore terminologyNot a treeD has 2 parentsABDCMore terminologyPath is a sequence of connected nodesLength of a path is the number of edges in the pathA to D is 2C to F is 1X is 0ABDCFXTree 1Tree 2Tree 3More terminologyHeight of a tree: length of its longest path from root to leafEx: Height of 2ABDCEMore terminologyDepth of a node: length of path from rootExampleA: 0B: 1E: 2D: 2C: 1ABDCEMore terminologySubtrees of a node are the nodes rooted at a nodes childrenAs subtreesrooted at Brooted at CABDCEBinary TreesSpecial TreeNo node has more than 2 childrencan have 0,1, or 2Each child is either right or leftABDCEBinary TreesABDCEXYZSTRepresenting Binary Treestemplate class BinaryTreenode{ private: D data_; BinaryTreenode* left_; BinaryTreenode* right_;

public: BinaryTreenode(D d = D(),BinaryTreenode* left = NULL,BinaryTreenode* right = NULL);};

Representing Binary TreesABDCdata: Aright:left:data: Cright:left:data: Bright:left:data: Dright:left:Binary Trees and RecursionExampleRecursive definition of height for binary treesa tree with only a root (no children) has height 0an non-empty tree has height 1 + max(height of left subtree, height of right subtree)Representing General TreesNo fixed number of children per nodecant have single member variable per childuse a list of childrenitems in list will be Treenode *stemplate class Treenode{ private: D data; list kids;

Representing General TreesABCDdata:Akids:items:count: 3data: Bkids:data: Ckids:data: Dkids:Representing General TreesAlternativeeach node has a pointer to its first childeach node has a pointer to its next siblingmore basic linked list styletemplate class Treenode{ private: D data; Treenode* first_child_; Treenode* next_sibling_;Representing General TreesABCDdata:Afirst_:next_:data: Bfirst_:next_:data: Cfirst_:next_:data: Dfirst_:next_:Questions?19Tree TraversalsIterate through all nodeseach node visited once, toprint all valuessee if a node has some propertymodify the node, etc4 common orders for visiting nodespreorderpostorderin order (binary trees only)level orderPreorder TraversalDepth-first traversalVisit the root firstRecursive definitionvisit the rootdo a preorder traversal of each subtree, left to rightEx: A B E D C FABDCEF123456Preorder Traversal Codevoid preorder(TreeNode* node){ if( node != null ){ //--visit node // preorder the children list::iterator iter; for( iter = node->kids.begin(); iter != node->kids.end(); iter++){ preorder(*iter); } }}

Postorder traversalDepth-first traversalVisit the root lastRecursive definitiondo a postorder traversal of each subtree, left to rightvisit the rootEx: E D B F C AABDCEF631254In-order traversalDepth-first traversalFor binary trees onlyVisit root in between subtreesRecursive definitionin-order traversal of left subtreevisit the rootin-order traversal of right subtreeEx: E B D A F CABDCEF421365In, post, preorder traversalDifference is in when root is visitedRoot first => preorderRoot last => postorderInbetween => in-orderLevel-order traversalBreadth-first traversalVisit all nodes at level 1 (depth 1)then level 2, level 3, etcalways left to right (or some order)Ex: A B C E D FABDCEF123425Level-order traversalUse a queue instead of recursion (implicitly uses a stack)

q.push(root);while( !q.empty() ){ //dequeue node n //visit n //enqueue all of ns children(L to R)}Questions?28Ilaughedandhejumpedshe(ha!)ateallfivecakesDo the pre, post, in, and level order traversals

DISCUSSIONBREAK!!!Ilaughedandhejumpedshe(ha!)ateallfivecakesPre: I laughed and he jumped she (ha!) ate all 5 cakesIn: and he laughed she jumped I all ate 5 (ha!) cakesPost: he and she jumped laughed all 5 ate cakes (ha!) ILevel: I laughed (ha!) and jumped ate cakes he she all 5DISCUSSIONBREAK!!!