DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

Size: px
Start display at page:

Download "DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011."

Transcription

1 DataStruct 7. Trees Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., November 6, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, College of Engineering, Yeungnam University, KOREA (Tel : ; Fax : ytkim@yu.ac.kr)

2 General Trees Tree Traversal Algorithms Binary Trees Outline DS 7-2

3 7.1 General Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Applications: Organization charts File systems Programming environments US Computers R Us Sales Manufacturing International Laptops Desktops R&D Europe Asia Canada DS 7-3

4 Formal Tree Definitions (1) Root: node without parent (A) Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grandgrandchild, etc. Subtree: tree consisting of a node and its descendants E B F I J K A G C H subtree D DS 7-4

5 Formal Tree Definitions (2) parent, child, subtree root parent child sibling: nodes that are children of the same parent external node: leaf node that has no children internal node: node that has one or more children ancestor descendent subtree DS 7-5

6 Edges and paths Formal Tree Definitions (3) edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge Ordered Tree a tree is ordered if there is a linear ordering defined for the children of each node e.g.) structured document DS 7-6

7 Tree Functions We use positions to abstract nodes Generic methods: integer size() // returns the number of nodes in the tree boolean empty() // return true if the tree is empty Accessor methods: position root() // return a position for the tree s root list<position> positions() // return a position list of all the nodes of the tree Position-based methods: position p.parent() // return the parent of p list<position> p.children() // return a position list containing the children of p Query methods: boolean p.isroot() boolean p.isexternal() Additional update methods may be defined by data structures implementing the Tree ADT DS 7-7

8 C++ Tree Interface Informal interface for a position in a tree template <typename E> // base element type class Position<E> { // a node position public: E& operator*(); // get element Position parent() const; // get parent PositionList children() const; // get node's children bool isroot() const; // root node? bool isexternal() const; // external node? }; Informal interface for the tree ADT template <typename E> // base element type class Tree<E> { public: // public types class Position; // a node position class PositionList; // a list of positions of children public: // public functions int size() const; // number of nodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // get positions of all nodes }; DS 7-8

9 Linked Structure for General Trees linked structure of general tree parent element children container (a) Node Structure (b) Portion of data structure associated with a node and its children DS 7-9

10 Depth 7.2 Tree Traversal Algorithms p : a node of a tree T depth of p is the number of ancestors of p int depth(const Tree& T, const Position& p) { if (p.isroot()) return 0; // root has dept else return (1 + depth(t, p.parent())); // 1 + (depth of parent) } DS 7-10

11 Height if p is external, then the height of p is 0 height of a node p in a tree T is one plus the maximum height of a child of p height of a tree T is the height of the root of T int height2(const Tree& T, const Position& p) { if (p.isexternal()) return 0; // leaf has height 0 int h = 0; PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q!= ch.end(); ++q) h = max(h, height2(t, *q)); return 1 + h; // 1 + max height of children } DS 7-11

12 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Algorithm preorder(v) visit(v) for each child w of v preorder (w) DS 7-12

13 preorderprint void preorderprint(const Tree& T, const Position& p) { cout << *p; // print element PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q!= ch.end(); ++q) { cout << " "; preorderprint(t, *q); } } DS 7-13

14 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Algorithm postorder(v) for each child w of v postorder (w) visit(v) DS 7-14

15 postorderprint void postorderprint(const Tree& T, const Position& p) { PositionList ch = p.children(); // list of children for (Iterator q = ch.begin(); q!= ch.end(); ++q) { postorderprint(t, *q); cout << " "; } cout << *p; // print element } DS 7-15

16 7.3 Binary Trees A binary tree is a tree with the following properties: Each internal node has at most two children (exactly two for proper binary trees) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching D B H E I A F C G DS 7-16

17 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a 1) + (3 b)) + 2 a 1 3 b DS 7-17

18 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? Yes How about coffee? No On expense account? Yes No Yes No Starbucks Spike s Al Forno Café Paragon DS 7-18

19 BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position p.left() position p.right() Update methods may be defined by data structures implementing the BinaryTree ADT Proper binary tree: Each node has either 0 or 2 children DS 7-19

20 Traversals of a Binary Tree Preorder Traversal Algorithm binarypreorder(t, p) perform the visit action for node p if p is an internal node then binarypreorder(t, p.left()) binarypreorder(t, p.right()) In-order Traversal Algorithm binaryinorder(t, p) if p is an internal node then binaryinorder(t, p.left()) perform the visit action for node p if p is an internal node then binaryinorder(t, p.right()) Post-order Traversal Algorithm binarypostorder(t, p) if p is an internal node then binarypostorder(t, p.left()) binarypostorder(t, p.right()) perform the visit action for node p DS 7-20

21 Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Application: draw a binary tree x(v) = in-order rank of v y(v) = depth of v 2 6 Algorithm inorder(v) if v.isexternal() inorder(v.left()) visit(v) if v.isexternal() inorder(v.right()) DS 7-21

22 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree + Algorithm printexpression(v) if v.isexternal() print( ( ) inorder(v.left()) print(v.element()) if v.isexternal() inorder(v.right()) print ( ) ) 2 a 1 3 b ((2 (a 1)) + (3 b)) DS 7-22

23 Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees + Algorithm evalexpr(v) if v.isexternal() return v.element() else x evalexpr(v.left()) y evalexpr(v.right()) operator stored at v return x y DS 7-23

24 Euler Tour Traversal Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) L R B DS 7-24

25 Class Position C++ Binary Tree Interface template <typename E> // base element type class Position<E> { // a node position public: E& operator*(); // get element Position left() const; // get left child Position right() const; // get right child Position parent() const; // get parent bool isroot() const; // root of tree? bool isexternal() const; // an external node? }; DS 7-25

26 Class BinaryTree template <typename E> // base element type class BinaryTree<E> { // binary tree public: // public types class Position; // a node position class PositionList; // a list of positions public: // member functions int size() const; // number of nodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // list of nodes }; DS 7-26

27 Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height Properties: e = i + 1 n = 2e 1 h i h (n 1)/2 e 2 h h log 2 e h log 2 (n + 1) 1 DS 7-27

28 Maximum Number of Nodes in the levels of a Binary Tree DS 7-28

29 Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node Node objects implement the Position ADT B B A C D E A D C E DS 7-29

30 Linked Binary Tree (1) /** LinkedBinaryTree.h */ #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include <iostream> #include <list> using namespace std; typedef int Elem; // base element type class LinkedBinaryTree { protected: // TreeNode declaration struct TreeNode { // a node of the tree Elem elt; // element value TreeNode* parent; // parent TreeNode* left; // left child TreeNode* right; // right child TreeNode() : elt(), parent(null), left(null), right(null) { } // constructor TreeNode(Elem e) : elt(e), parent(null), left(null), right(null) { } // constructor }; DS 7-30

31 Linked Binary Tree (2) /** LinkedBinaryTree.h (2) */ public: // Position declaration class Position { // position in the tree private: TreeNode* v; // pointer to the TreeNode public: Position(TreeNode* _v = NULL) : v(_v) { } // constructor Elem& operator*() { return v->elt; } // get element Position left() const { return Position(v->left); } // get left child Position right() const { return Position(v->right); } // get right child Position parent() const { return Position(v->parent); } // get parent bool isnull() { return (v == NULL); } // check if position is null bool isroot() const { return v->parent == NULL; } // root of the tree? bool isexternal() const { return v->left == NULL && v->right == NULL; } // an external TreeNode? friend class LinkedBinaryTree; // give tree access }; typedef std::list<position> PositionList; // list of positions DS 7-31

32 Linked Binary Tree (3) /** LinkedBinaryTree.h (2) */ public: LinkedBinaryTree(); // constructor int size() const; // number of TreeNodes bool empty() const; // is tree empty? Position root() const; // get the root PositionList positions() const; // list of TreeNodes void addroot(); // add root to empty tree void expandexternal(const Position& p); // expand external TreeNode Position removeaboveexternal(const Position& p); // remove p and parent Position addelementinorder(position& p, Position& paren, const Elem& element); void printtreeinorder(position p); void printtreebylevel(position p, int level); // housekeeping functions omitted... protected: // local utilities void preorder(treenode* v, PositionList& pl) const; // preorder utility private: TreeNode* _root; // pointer to the root int n; // number of TreeNodes }; #endif DS 7-32

33 Linked Binary Tree (4) /** LinkedBinaryTree.cpp (1) */ #include "LinkedBinaryTree.h" LinkedBinaryTree::LinkedBinaryTree() : _root(null), n(0) { } int LinkedBinaryTree::size() const { return n; } bool LinkedBinaryTree::empty() const { return size() == 0; } // constructor // number of nodes // is tree empty? LinkedBinaryTree::Position LinkedBinaryTree::root() const // get the root { return Position(_root); } void LinkedBinaryTree::addRoot() { _root = new TreeNode; n = 1; } // add root to empty tree DS 7-33

34 Linked Binary Tree (5) /** LinkedBinaryTree.cpp (2) */ // expand external node void LinkedBinaryTree::expandExternal(const Position& p) { TreeNode* v = p.v; // p's node v->left = new TreeNode; // add a new left child v->left->parent = v; // v is its parent v->right = new TreeNode; // and a new right child v->right->parent = v; // v is its parent n += 2; // two more nodes } LinkedBinaryTree::Position // remove p and parent LinkedBinaryTree::removeAboveExternal(const Position& p) { TreeNode* w = p.v; TreeNode* v = w->parent; // get p's node and parent TreeNode* sib = (w == v->left? v->right : v->left); if (v == _root) { // child of root? _root = sib; //...make sibling root sib->parent = NULL; } else { TreeNode* gpar = v->parent; // w's grandparent if (v == gpar->left) gpar->left = sib; // replace parent by sib else gpar->right = sib; sib->parent = gpar; } delete w; delete v; // delete removed nodes n -= 2; // two fewer nodes return Position(sib); } DS 7-34

35 Binary Tree Update Functions expandexternal(p) removeaboveexternal(p) DS 7-35

36 Linked Binary Tree (6) /** LinkedBinaryTree.cpp (3) */ // list of all nodes LinkedBinaryTree::PositionList LinkedBinaryTree::positions() const { PositionList pl; preorder(_root, pl); // preorder traversal return PositionList(pl); // return resulting list } // preorder traversal void LinkedBinaryTree::preorder(TreeNode* v, PositionList& pl) const { pl.push_back(position(v)); // add this node if (v->left!= NULL) // traverse left subtree preorder(v->left, pl); if (v->right!= NULL) // traverse right subtree preorder(v->right, pl); } DS 7-36

37 Linked Binary Tree (7) /** LinkedBinaryTree.cpp (4) */ LinkedBinaryTree::Position LinkedBinaryTree::addElementInOrder(Position& p, Position& paren, const Elem& element) { LinkedBinaryTree::Position newpos; if (p.isnull()) { p.v = new TreeNode(element); if (paren.v == NULL) { _root = p.v; } p.v->parent = paren.v; n++; // increment the number of elements return LinkedBinaryTree::Position(p.v); } } else if (element < *p) { newpos = addelementinorder(p.left(), p, element); p.v->left = newpos.v; } else if (element > *p) { newpos = addelementinorder(p.right(), p, element); p.v->right = newpos.v; } else { cout << "Duplicated element in addelementinorder!!" << endl; } DS 7-37

38 Linked Binary Tree (8) /** LinkedBinaryTree.cpp (5) */ void LinkedBinaryTree::printTreeInOrder(Position p) { if (p.isnull()) return; printtreeinorder(p.left()); cout << *p <<" "; printtreeinorder(p.right()); } void LinkedBinaryTree::printTreeByLevel(Position p, int level) { LinkedBinaryTree::TreeNode* pchild = NULL; if (p.v!= NULL) { if (level == 0) cout << "\ nroot (data: "; cout << *p << ")" << endl; pchild = (p.left()).v; for (int i=0; i<level; i++) cout << " "; DS 7-38

39 Linked Binary Tree (9) /** LinkedBinaryTree.cpp (6) */ if (pchild!= NULL) { cout << "L (data: "; printtreebylevel(pchild, level+1); } else { cout << "L (NULL)" << endl; } } } pchild = (p.right()).v; for (int i=0; i<level; i++) cout << " "; if (pchild!= NULL) { cout << "R (data: "; printtreebylevel(pchild, level+1); } else { cout << "R (NULL)" << endl; } DS 7-39

40 Linked Binary Tree (10) /** main.cpp */ #include <iostream> #include "LinkedBinaryTree.h" void main() { LinkedBinaryTree* plbtree; int data; plbtree = new LinkedBinaryTree; cout << endl; for (int i=0; i<10; i++) { data = rand() % 100; cout << "Adding " << data << " into LinkedBinaryTree" << endl; plbtree->addelementinorder(plbtree->root(), LinkedBinaryTree::Position(NULL), data); } cout << "\ nelements in LinkedBinaryTree, in order : " << endl; plbtree->printtreeinorder(plbtree->root()); cout << endl; plbtree->printtreebylevel(plbtree->root(), 0); // start from level 0 cout << endl; } delete plbtree; DS 7-40

41 Execution Results (case 1) (case 2) DS 7-41

42 Tri-node Restructuring height height of leaf node : 1 height of inner node: 1 + MAX (height of left child, height of right child) height difference height difference = height of left child height of right child tri-node restructuring re-arrange three node so that the re-arranged subtree has height difference less than or equal to 1 h 3 diff 1 h 4 diff 3 h 4 diff -3 h 3 diff -1 h 2 diff 1 h 2 diff -1 (Example 1) DS 7-42 (Example 2)

43 Tri-node Restructuring Single rotation LL of subtree h 2 diff 1 A h 3 diff 2 B C rotate_ll() h 2 A B C DS 7-43

44 Example of Tri-node Restructuring Single rotation RR of subtree A h 3 diff 2 h 2 B diff 1 ht 0 C rotate_rr() A B h 2 C DS 7-44

45 Example of Tri-node Restructuring Double rotation LR of subtree h 2 diff -1 A h 3 diff 2 B C rotate_lr() h 2 A B C DS 7-45

46 Example of Tri-node Restructuring Double rotation RL of subtree A h 3 diff -2 B C h 2 diff 1 rotate_rl() h 2 A B C DS 7-46

47 Algorithm getheight() Algorithm getheight(treenode* ptn) height = 0; if (ptn!= NULL) height = 1 + max (getheight(ptn->left()), getheight(ptn->right()); return height; end Algorithm getheightdiff() Algorithm getheightdiff(treenode* ptn) heightdiff = 0; if (ptn == NULL) return 0; heightdiff = getheight(ptn->left() - getheight(ptn->right(); return heightdiff; end DS 7-47

48 Algorithm rebalance() Algorithm rebalance(treenode** pptn) heightdiff = getheightdiff(*pptn); if (heightdiff > 1) // L child is higher than R child if (getheightdiff(*pptn->left() > 0) *pptn = rotate_ll(*pptn); // L.L child is higher than L.R child else *pptn = rotate_lr(*pptn); // L.R child is higher than L.L child else if (heightdiff < -1) // R child is higher than L child if (getheightdiff(*pptn->right() < 0) *pptn = rotate_rr(*pptn); // R.R child is higher than R.L child else *pptn = rotate_rl(*pptn); // R.L child is higher than R.R child return *pptn; end DS 7-48

49 Algorithm rotate_ll() Algorithm rotate_ll(treenode* pparent) TreeNode* pchild; pchild = pparent->left(); pparent->setleft(pchild->right()); pchild->setright(pparent); return pchild; end h 2 A B h 3 diff 2 C D rotate_ll() h 3 diff 1 A B C D h diff 1 DS 7-49

50 Algorithm rotate_rr() Algorithm rotate_rr(treenode* pparent) TreeNode* pchild; pchild = pparent->right(); pparent->setright(pchild->left()); pchild->setleft(pparent); return pchild; end A h 3 diff -2 B C h 2 D rotate_rr() h diff 1 A B C h 3 diff 1 D DS 7-50

51 Algorithm rotate_lr(), rotate_rl() Algorithm rotate_lr(treenode* pparent) TreeNode* pchild; pchild = pparent->left(); pparent->setleft(rotate_rr(pchild)); return rotate_ll(pparent); end h 2 A B h 3 diff 2 C D setleft(rotate_rr()) h 2 diff 1 A B diff 2 C h 3 diff 3 D rotate_lr() h 2 diff 1 A h 3 diff 1 B C D DS 7-51

52 Algorithm rotate_lr(), rotate_rl() Algorithm rotate_rl(treenode* pparent) TreeNode* pchild; pchild = pparent->right(); pparent->setright(rotate_ll(pchild)); return rotate_rr(pparent); end A h 3 diff -2 h 2 C B D setright(rotate_cc( C )) A h 3 diff -3 B diff -2 C h 2 diff -1 D rotate_rr( B ) A B h 3 diff -1 C h 2 diff -1 D DS 7-52

53 Homework DS-7 DS-7.1 Projects P-7.2 (LinedBinaryTree using class template) DS-7.2 Projects P-7.11 (game tree for Tic-Tac-Toe) DS 7-53

Trees. 9/21/2007 8:11 AM Trees 1

Trees. 9/21/2007 8:11 AM Trees 1 Trees 9/21/2007 8:11 AM Trees 1 Outline and Reading Tree ADT ( 6.1) Preorder and postorder traversals ( 6.2.3) BinaryTree ADT ( 6.3.1) Inorder traversal ( 6.3.4) Euler Tour traversal ( 6.3.4) Template

More information

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2) CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO 1 OUTLINE

More information

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 02/06/2006 Trees 1 Outline and Reading Tree ADT ( 7.1.2) Preorder and postorder traversals ( 7.2.2-3) BinaryTree ADT ( 7.3.1) Inorder traversal

More information

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees Part 4: Trees Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 2013 Goodrich, Tamassia, Goldwasser Trees 2 Example: Family Tree 2013 Goodrich, Tamassia, Goldwasser Trees 3 Example: Unix File

More information

Data Structures and Algorithms. Trees

Data Structures and Algorithms. Trees Data Structures and Algorithms Trees Tree Example Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A

More information

Trees and Binary Trees

Trees and Binary Trees Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or

More information

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees 1 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child

More information

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Chapter 7: Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures

More information

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery 1/18/2005 4:17 AM Trees 1 Outline and Reading Tree ADT ( 2.3.1) Preorder and postorder traversals ( 2.3.2) BinaryTree ADT ( 2.3.3) Inorder traversal

More information

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India Unit 1 (9Hrs) Trees Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India The class notes are a compilation and edition from many sources.

More information

Trees 3/19/14. Mammal. Dog Pig Cat

Trees 3/19/14. Mammal. Dog Pig Cat Presentation for use with the textbook ata Structures and lgorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 014 Trees Mammal og Pig at Trees 1 What is a Tree

More information

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Make Money Fast! Trees Stock Fraud Ponzi Scheme Bank Robbery Data structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich,

More information

Trees. Trees. CSE 2011 Winter 2007

Trees. Trees. CSE 2011 Winter 2007 Trees CSE 2011 Winter 2007 2/5/2007 10:00 PM 1 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

Outline. Definitions Traversing trees Binary trees

Outline. Definitions Traversing trees Binary trees Trees Chapter 8 Outline Definitions Traversing trees Binary trees Outline Definitions Traversing trees Binary trees Graph a b Node ~ city or computer Edge ~ road or data cable c Undirected or Directed

More information

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees Binary Tree (Chapter 0. Starting Out with C++: From Control structures through Objects, Tony Gaddis) Le Thanh Huong School of Information and Communication Technology Hanoi University of Technology 11.1

More information

Data Structures Lecture 6

Data Structures Lecture 6 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 6 Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy

More information

Binary trees. Binary trees. Binary trees

Binary trees. Binary trees. Binary trees Binary trees March 23, 2018 1 Binary trees A binary tree is a tree in which each internal node has at most two children. In a proper binary tree, each internal node has exactly two children. Children are

More information

Data Structures. Trees, Binary trees & Binary Search Trees

Data Structures. Trees, Binary trees & Binary Search Trees Data Structures Trees, Binary trees & Binary Search Trees Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parentchild relation Applications:

More information

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees The Stack ADT ( 2.1.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think

More information

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model What is a Tree Trees Stock raud Make Money ast! Ponzi Scheme ank Robbery In computer science, a tree is an abstract model omputers R Us of a hierarchical structure tree consists of with a parent-child

More information

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1 What is a Tree Trees Stock Fraud Make Money Fast! Winning Lotto / Bank Robbery In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes ith a parent-child relation

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Ch02 Data Structures

Ch02 Data Structures Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under

More information

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries 2013-2 DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 22, 2013 Advanced

More information

Ch02 Data Structures

Ch02 Data Structures Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under

More information

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo The tree data structure 1 Trees COL 106 Amit Kumar Shweta Agrawal Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo 1 Trees The tree data structure 3 A rooted tree data structure stores

More information

Trees: examples (Family trees)

Trees: examples (Family trees) Ch 4: Trees it s a jungle out there... I think that I will never see a linked list useful as a tree; Linked lists are used by everybody, but it takes real smarts to do a tree Trees: examples (Family trees)

More information

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex

More information

Topic Binary Trees (Non-Linear Data Structures)

Topic Binary Trees (Non-Linear Data Structures) Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

COSC 2011 Section N. Trees: Terminology and Basic Properties

COSC 2011 Section N. Trees: Terminology and Basic Properties COSC 2011 Tuesday, March 27 2001 Overview Trees and Binary Trees Quick review of definitions and examples Tree Algorithms Depth, Height Tree and Binary Tree Traversals Preorder, postorder, inorder Binary

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

DataStruct 8. Heaps and Priority Queues

DataStruct 8. Heaps and Priority Queues 2013-2 DataStruct 8. Heaps and Priority Queues Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 13, 2013 Advanced Networking Technology

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

selectors, methodsinsert() andto_string() the depth of a tree and a membership function Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using a tree of integer numbers 2 Header Files defining a node struct defining a tree class 3 Definition of Methods selectors, methodsinsert()

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Binary Trees Fall 2018 Margaret Reid-Miller

Binary Trees Fall 2018 Margaret Reid-Miller Binary Trees 15-121 Fall 2018 Margaret Reid-Miller Trees Fall 2018 15-121 (Reid-Miller) 2 Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements B-Trees 1 B-Trees nodes with many children a type node a class for B-trees 2 manipulating a B-tree an elaborate example the insertion algorithm removing elements MCS 360 Lecture 35 Introduction to Data

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C The British Constitution Crown Church of England Cabinet House of Commons House of Lords Supreme Court Ministers County Metropolitan Council police Rural District County Borough

More information

tree nonlinear Examples

tree nonlinear Examples The Tree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example 10-2

More information

Topic 14. The BinaryTree ADT

Topic 14. The BinaryTree ADT Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

Tree Travsersals and BST Iterators

Tree Travsersals and BST Iterators Tree Travsersals and BST Iterators PIC 10B May 25, 2016 PIC 10B Tree Travsersals and BST Iterators May 25, 2016 1 / 17 Overview of Lecture 1 Sorting a BST 2 In-Order Travsersal 3 Pre-Order Traversal 4

More information

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

More information

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech Trees Dr. Ronaldo Menezes Hugo Serrano (hbarbosafilh2011@my.fit.edu) Introduction to Trees Trees are very common in computer science They come in different variations They are used as data representation

More information

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications Trees 1 Outline Preliminaries What is Tree? Implementation of Trees using C++ Tree traversals and applications Binary Trees Binary Search Trees Structure and operations Analysis 2 What is a Tree? A tree

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures. Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,

More information

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees called the left and right subtrees which are disjoint

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

CSE 230 Intermediate Programming in C and C++ Binary Tree

CSE 230 Intermediate Programming in C and C++ Binary Tree CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu Introduction to Tree Tree is a non-linear data structure

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them. Laboratory 11: Expression Trees and Binary Search Trees Introduction Trees are nonlinear objects that link nodes together in a hierarchical fashion. Each node contains a reference to the data object, a

More information

Trees, Binary Trees, and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York CSC212 Data Structure Lecture 13 Trees and Tree Traversals Instructor: Prof. George Wolberg Department of Computer Science City College of New York Motivation Linear structures arrays dynamic arrays linked

More information

csci 210: Data Structures Trees

csci 210: Data Structures Trees csci 210: Data Structures Trees 1 Summary Topics general trees, definitions and properties interface and implementation tree traversal algorithms depth and height pre-order traversal post-order traversal

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree is a finite set of one or more nodes

More information

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement

More information

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I Review from Lecture 16 CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

interface Position<E> { E getelement() throws IllegalStateExcept..; }

interface Position<E> { E getelement() throws IllegalStateExcept..; } 1 interface Position { E getelement() throws IllegalStateExcept..; } position node e Tree ADT: Interface 2 Elements of Tree ADT stores elements at Positions which, in fact, Tree ADT are abstractions

More information

Tree Data Structures CSC 221

Tree Data Structures CSC 221 Tree Data Structures CSC 221 Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition:

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH Unix/Linux file systems Hierarchical Structure (2015F) Lecture11: Tree I ohyung Han S, POSTH bhhan@postech.ac.kr 2 Tree ata Structures n abstract model of a hierarchical structure 2 dimensional structure

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 02 / 24 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Trees binary trees two ideas for representing them in code traversals start binary

More information

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Lecture 37 Section 9.4. Wed, Apr 22, 2009 Preorder Inorder Postorder Lecture 37 Section 9.4 Hampden-Sydney College Wed, Apr 22, 2009 Outline Preorder Inorder Postorder 1 2 3 Preorder Inorder Postorder 4 Preorder Inorder Postorder Definition (Traverse)

More information

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date: Upcoming ACM Events Linux Crash Course Date: Oct. 2nd and 3rd Time: 1:15 pm - 3:15 pm Location: UW1-210 (10/02) and UW1-221 (10/03) Weekly Crack the Coding Interview Date: Weekly Fridays from Oct. 5th

More information

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III Review from Lecture 17 & 18 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order traversal;

More information

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) IX. Binary Trees (Chapter 10) -1- A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following: a. If Loc >= length

More information

BBM 201 Data structures

BBM 201 Data structures BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS DATA STRUCTURES AND ALGORITHMS 13 BINARY TREES INORDER, PREORDER, POSTORDER TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE,

More information

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Chapter 7 Trees 7.1 Introduction A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w Tree Terminology Parent Ancestor Child Descendant Siblings

More information

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I Review from Lectures 17 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.

More information

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39 Binary Search Tree 1.0 Generated by Doxygen 1.7.1 Mon Jun 6 2011 16:12:39 Contents 1 Binary Search Tree Program 1 1.1 Introduction.......................................... 1 2 Data Structure Index 3

More information

Examples

Examples Examples 1 Example [ proper binary tree properties ] Draw a proper binary tree with exactly 3 internal vertices and exactly 10 leaves. If not possible, explain why no such tree exists. Recall: the root

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III Review from Lecture 18 & 19 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1 Trees Terminology. Rooted Trees. Traversals. Labeled Trees and Expression Trees. Tree ADT. Tree Implementations. Binary Search Trees. Optimal Search Trees T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

More information

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find IX. Binary Trees IX-1 IX. Binary Trees (Chapter 10) A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following:

More information

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III Review from Lecture 16 & 17 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 19, 2016 Outline Outline 1 Chapter 7: Trees Outline Chapter 7: Trees 1 Chapter 7: Trees Uses Of Trees Chapter 7: Trees

More information

Trees, Heaps, and Priority Queues

Trees, Heaps, and Priority Queues Chapter 17 Trees, Heaps, and Priority Queues Objectives To know how to represent and access the elements in a binary tree ( 17.2.1-17.2.2). To insert an element to a binary tree ( 17.2.3). To traverse

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12) H.O.#12 Fall 2015 Gary Chan Binary Tree (N:12) Outline Binary tree terminology Tree traversals: preorder, inorder and postorder Dictionary and binary search tree Binary search tree operations Search min

More information

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Review for Test 2 (Chapter 6-10) Chapter 6: Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B.

More information