1 / 15

CSE 3358 Note Set 9

CSE 3358 Note Set 9. Data Structures and Algorithms. Trees. Overcome the linear nature of a list. Allows for storage of hierarchical data. Recursive Definition of Tree: An empty structure is empty tree

gur
Télécharger la présentation

CSE 3358 Note Set 9

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. CSE 3358 Note Set 9 Data Structures and Algorithms

  2. Trees • Overcome the linear nature of a list. • Allows for storage of hierarchical data • Recursive Definition of Tree: • An empty structure is empty tree • If t1, …, tk are disjointed trees, then the structures whose root has as its children the roots of t1, …, tk is also a tree • Only structures generated by these two rules are trees

  3. Tree Terminology • Root • Children • Parent • Ancestors • Descendants

  4. Tree Terminology • Node and arc • Path • Length of Path • Level of a node • Height of a non-empty tree

  5. Tree Terminology • Children of 10 • Ancestors of 18 • Descendant 15 • Siblings of 8 20 10 15 5 6 7 16 17 8 9 18 19

  6. Binary Tree • Each node has 2 children at max. • Left and Right • Benefit w/ respect to efficiency of algorithms • Complete Binary Tree • All non-leaf nodes have both children • All leaves are at same level • Binary Tree can have at most _______ nodes at level i + 1

  7. Binary Search Tree Binary Search Property: For each node n of the tree, all values stores in its left subtree are less than the value v stored in n and all values stored in the right subtree are greater than v. v • No =. Only < and >. • Storing multiple copies of the same value is avoided. • Meaning of < and > depend on what the data payload of the node is

  8. Implementation • Can be implemented as: • Array • struct node { T* payload;intleftIdx;intrightIdx;}; • If element deleted, update of entire tree may be necessary. • Linked Dynamic structure • struct node { T* payload; node *left, *right};

  9. Searching template <class T> T* BST<T>::search(BSTNode<T>* p, const T& el) const { while (p != 0) if (el == p->key) return &p -> key; else if (el < p -> key) p = p->left; else p = p->right; return 0; } 13 10 25 2 12 20 31 29

  10. Complexity of Search • Based on the number of comparisons performed during the searching process • depends on the number of nodes on the unique path from root to the node being searched for. • Complexity: The length of the path leading to this node plus 1. • depends on the overall shape of the tree • Think about a tree that is essentially a List vs a balanced tree Internal Path Length: The sum of all path lengths of all nodes.

  11. IPL 14 12 16

  12. IPL 14 7 27 32 3 9

  13. IPL Formula for IPL: 14 7 27 3 9 32 Formula for Average IPL:

  14. IPL for Binary Tree • Worst Case: • tree that looks like a Linked List • Best Case: • all leaves in tree of height h are in at most two levels • only nodes in the next to last level can have children 2 3 4 5

  15. ?

More Related