Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC2100: Data Structures and Algorithms

Similar presentations


Presentation on theme: "COSC2100: Data Structures and Algorithms"— Presentation transcript:

1 COSC2100: Data Structures and Algorithms
Tree Basics and Binary Tree

2 Review So far, we have learnt two fundamental data storage structures
Array Linked List An ordered array Search a key in the array: O(lgN) with binary search Insert or delete a new key: O(N) worst case Linked list Search a key in the list: O(N) worst case Insert a key: O(1)

3 Overview We are going to learn another fundamental data storage structure Binary Tree With a special binary tree, Balanced Binary Search Tree Search: O(lgN) Insert and Delete: O(lgN) The basics about trees Terminology Tree traversals

4 Tree Structure in Real Life
College College of Art and Science Departments BSCI MSCS CHEM Course types MATH COSC COSC courses COSC1000 COSC2100 COSC3100 Other examples: Family tree Table of contents Company organization chart

5 A file system that stores documentations for several courses
Trees 11/10/2018 5:42 PM Trees in Computers /usr/rt/courses cosc1020 cosc2100 cosc3100 programs homework lectures A file system that stores documentations for several courses Trees

6 Tree Data Structure A tree consists of nodes connected by edges
Trees 11/10/2018 5:42 PM Tree Data Structure A tree consists of nodes connected by edges Nodes are represented as circles in the graph Edges are lines in the graph Trees are hierarchical structures B C A D E G H X Y Z Trees

7 Tree Terminology I Root: the node at the top of the tree
Trees 11/10/2018 5:42 PM Tree Terminology I A B D C G H E F I J K Root: the node at the top of the tree Node A Edge: directed connection from a node’s parent to the node Edges from A to B, from B to F, from F to K Parent: the node to which the given node is connected via the upward edge B’s parent: A A’s parent: null Child: the node to which the given node is connected via downward edges A’s child: B, C, D C’s child: G, H Trees

8 Tree Terminology II Internal node: node with at least one child
Trees 11/10/2018 5:42 PM Tree Terminology II Internal node: node with at least one child Nodes A, B, C, F leaf node node without children Nodes E, I, J, K, G, H, D Siblings of a node The nodes with the same parent I’s siblings: J and K Subtree (CGH): tree consisting of a node and its children, children’s children… Root of the subtree: C Edge from A (C’s parent) to C subtree A B D C G H E F I J K Trees

9 Trees 11/10/2018 5:42 PM Tree Terminology III Path: a path form node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1 <= i < k Length: the length of this path is the number of edges on the path The depth of a node ni is the length of path form the root to ni. The depth of the root is 0 The depth of the tree is the largest depth among leaves. The height of ni is the length of the longest path from ni to a leaf. All leaves are at height 0. The height of a tree is the height of the root. Trees

10 Examples Exercises: Parent of J: children of J:
B D C G H E F I J K Exercises: Parent of J: children of J: Path from A to F and its length: Height and depth of F:

11 More Exercises Which node is the root? Which nodes are leaves?
The tree’s depth? For node E, list its parent children siblings height depth B C D E F G H I J K L M

12 Yet More Exercises Which node is the root? Which nodes are leaves?
The tree’s depth? For node A, list its parent children siblings height depth A

13 Trees 11/10/2018 5:42 PM Binary Trees A A binary tree is a tree in which every node has at most two children left child and right child A node in a binary tree doesn’t necessarily have two children only a left child only a right child or no children at all B C D E G H I Trees

14 Representing Tree in Java Code
Trees 11/10/2018 5:42 PM Representing Tree in Java Code class Node { int iData; Node left; Node right; //constructor //display } class Tree { private Node root; //constructor //methods for insertion, deletion… } Node is the building blocks for Tree, just as Link for LinkList Node root is the only entry to a Tree object, as Link first to a LinkList object Trees

15 Traversing a Tree To traverse a tree means to visit all the nodes in some specified order Inorder traversal Preorder traversal Postorder traversal

16 Trees 11/10/2018 5:42 PM Inorder Traversal Algorithm inOrder(v) If v==null Return inOrder (left (v)) System.out.print(v.iData + “ “) inOrder (right (v)) A node is visited after its left subtree and before its right subtree What is printed for the given tree if executing inOrder(root) 6 2 8 1 4 7 9 3 5 Trees

17 Preorder Traversal A node is visited before its children
Trees 11/10/2018 5:42 PM Preorder Traversal A node is visited before its children If visiting a node is printing the string, what will be printed for the given tree? Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 Make Money Fast! 2 5 1. Motivations 2. Methods 7 6 3 4 2.1 Stock Fraud 2.3 Bank Robbery 1.1 Greed 1.2 Avidity Trees

18 Postorder Traversal A node is visited after its descendants
Trees 11/10/2018 5:42 PM Postorder Traversal A node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Demonstrate how it works. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 7 cs16/ 3 6 homeworks/ programs/ 1 2 4 5 h1c.doc 3K h1nc.doc 2K DDR.java 10K Robot.java 20K Trees

19 Pop Quiz A By visiting the node, we print the character stored in the node Show the result of Preorder Inorder Postorder of the given tree. B C D E F G H I J K L M

20 Summary Tree Terminologies Tree traversals Hierarchical data structure
Root Internal, external (leaf) nodes Parent Depth Height Path Tree traversals Preorder, inorder, and postorder


Download ppt "COSC2100: Data Structures and Algorithms"

Similar presentations


Ads by Google