1 / 14

Lecture 17

Lecture 17. Non-Linear data structures. Richard Gesick. 2 primary types: Trees Graphs All trees are graphs, but not all graphs are trees Recursion is useful and is the easiest way to process them. It helps keep track of what's been processed and what remains. Non-Linear data structures.

phila
Download Presentation

Lecture 17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17 Non-Linear data structures Richard Gesick

  2. 2 primary types: Trees Graphs All trees are graphs, but not all graphs are trees Recursion is useful and is the easiest way to process them. It helps keep track of what's been processed and what remains Non-Linear data structures

  3. Graphs can have multiple references in and multiple references out (whereas tree node only has one reference in) Graphs can be directed or undirected and cyclic or acyclic Graphs

  4. Single parent 0 or more children A node with no children is called a "leaf" The topmost node is called the "root" N-ary trees Binary trees Trees

  5. The best example of an n-ary tree is your computer’s directory system. It has a single starting point and then 0 or more branches. N-ary Trees

  6. Binary search trees allow for fast insertion and removal of elements They are specially designed for fast searching A binary tree consists of two nodes, each of which has two child nodes All nodes in a binary search tree fulfill the property that: Descendants to the left have smaller data values than the node data value Descendants to the right have larger data values than the node data value Binary Trees and Binary Search Trees

  7. All nodes in a binary search tree fulfill the property that: Descendants to the left have smaller data values than the node data value Descendants to the right have larger data values than the node data value BST Tree Nodes

  8. A BST

  9. Balanced tree: each node has approximately as many descendants on the left as on the right If a binary search tree is balanced, then adding an element takes O(log(n)) time If the tree is unbalanced, insertion can be slow Perhaps as slow as insertion into a linked list BST

  10. Tree traversal schemes include Preorder traversal (root, left, right) Inorder traversal (left, root, right) Postorder traversal ( left, right, root) Preorder generates prefix expression, (polish notation) from an expression trees Inorder generates a sorted ordering Postorder generates a post fix expression, also useful for node deletion Tree Traversal

  11. Pre-order traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) In-order traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right) Post-order traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) Tree Traversals

  12. void PrintTree(Node current) { if (current != null) { Console.WriteLine(current.data); PrintTree(current.left); PrintTree(current.right); } } Recursive Processing Example

  13. A tree struc- ture?

  14. A graph?

More Related