data structures data structures
play

Data Structures Data Structures Lists Trees Trees Graphs CSE - PowerPoint PPT Presentation

Overview Introduction to Algorithms Introduction to Algorithms Review basic abstract data structures Sets Data Structures Data Structures Lists Trees Trees Graphs CSE 680 Review basic concrete data structures


  1. Overview Introduction to Algorithms Introduction to Algorithms � Review basic abstract data structures � Sets Data Structures Data Structures � Lists � Trees � Trees � Graphs CSE 680 � Review basic concrete data structures � Linked-List and variants Prof. Roger Crawfis � Trees and variants � Examine key properties � Examine key properties � Discuss usage for solving important problems (search, sort, selection). Sets and Multisets Set – Language Support g g pp � Common operations � .NET Framework Support � .NET Framework Support � Fixed Sets (C#,VB,C++,…) � Contains (search) � Is empty � IEnumerable interface � Size Size � Enumerate � ICollection interface � Dynamic Sets add: � Add � Java Framework Support pp � Remove Remo e � Other operations (not so common) � Collection � Intersection � Set � Union � Union � Sub-set � STD library (C++) � Note, these can, and probably should, be implemented � Set and Multiset classes and their iterators. statically (outside of the class).

  2. List List – Language Support g g pp � Common Queries � Arrays – fixed size. Arrays fixed size. � Enumerate � .NET Framework Support (C#,VB,C++,…) � Number of items in the list � IList interface � Return element at index i . i R t l t t i d � List<T> class � Search for an item in the list (contains) � Java Framework Support pp � Common Commands � Common Commands � List interface � Add element � Set element at index i . � ArrayList<T> and Vector<T> classes � Remove element? � STD library (C++) � Insert before index i ? � std::vector<T> class. Concrete Implementations p Rooted Trees � A tree is a collection of nodes and � A tree is a collection of nodes and � Set � Set directed edges , satisfying the following � What might you use to implement a properties: p p concrete set? concrete set? � There is one specially designated node � What are the pro’s and con’s of each called the root , which has no edges approach? approach? pointing to it. i ti t it � List � Every node except the root has exactly one edge pointing to it edge pointing to it. � Other than arrays, could you implement a � Other than arrays could you implement a list with any other data structure? � There is a unique path (of nodes and edges) from the root to each node. g )

  3. Basic Tree Concepts p Height and Level of a Tree g A � Height – # of edges on g g � Node – user-defined data structure that that contains pointers to data and pointers to other nodes: the longest path from the root to a leaf. � Root – Node from which all other nodes descend B C � Parent � Parent – has child nodes arranged in subtrees. has child nodes arranged in subtrees � Level – Root is at level � Child – nodes in a tree have 0 or more children. 0, its direct children are � Leaf – node without descendants D E at level 1, etc. t l l 1 t � Degree – number of direct children a tree/subtree has. � Recursive definition for height: height: F G 1+ max(height(T L ), height(T R )) H Rooted Trees: Example p Rooted Trees � If an edge goes from node a to node b , then a is � A is the root A is the root called the parent of b , and b is called a child of a . f b d b i ll d h ll d hild f A � D, E, G, H, J & � Children of the same parent are called siblings . K are leaves � If there is a path from a to b , then a is called an If there is a path from a to b , then a is called an � B is the parent � B is the parent ancestor of b , and b is called a descendent of a . B C of D, E & F � A node with all of its descendants is called a � D, E & F are subtree . subtree . siblings and siblings and F F children of B � If a node has no children, then it is called a leaf of D E G H the tree. � I, J & K are I subtree descendants of descendants of � If a node has no parent (there will be exactly one of � If a node has no parent (there will be exactly one of these), then it is the root of the tree. B � A & B are J K ancestors of I ancestors of I

  4. Binary Trees y Binary Search Trees y � Intuitively, a binary tree is a tree in which each node has � A binary search tree is a binary tree in which each y y y y In other words can we In other words, can we no more than two children. node, n , has a value satisfying the following put non-hierarchical data into a tree. We properties: will study Binary � n s value is > all values in its left subtree, T L , � n ’s value is > all values in its left subtree T Search Trees later. � n ’s value is < all values in its right subtree, T R , and � T L and T R are both binary search trees . 21 John 3 34 (These two binary Brenda Peter trees are distinct .) 55 2 8 Amy Mary Tom 5 13 This term is Binary Trees y Binary Trees y ambiguous, some i di indicate that each t th t h node is either full or empty. � A binary tree is full if it has no missing nodes. � A binary tree of height h is complete if it is full down to y g y g p level h – 1 , and level h is filled from left to right. � It is either empty. � All nodes at level h – 2 and above have 2 children each, � Otherwise, the root’s subtrees are full binary trees � If a node at level h � If a node at level h – 1 has children, all nodes to its left 1 has children all nodes to its left of height h – 1. at the same level have 2 children each, and � If not empty, each node has 2 children, except the nodes at � If a node at level h – 1 has 1 child, it is a left child. level h which have no children. level h which have no children � Contains a total of 2 h+1 -1 nodes (how many leaves?)

  5. Binary Trees y Complete & Balanced Trees p � A binary tree is balanced if the difference in height b between any node’s left and right subtree is ≤ 1. d ’ l f d i h b i 1 Complete and Balanced � Note that: � A full binary tree is also complete. � A complete binary tree is not always full. � A complete binary tree is not always full � Full and complete binary trees are also balanced. Not Balanced, Why? � Balanced binary trees are not always full or complete. Binary Tree: Pointer-Based Binary Tree: Table-Based Representation Representation Representation Representation Basic Idea: struct TreeNode; // Binary Tree nodes are struct ’s typedef string TreeItemType;// items in TreeNodes are string ’s d f i T I T // i i T N d i ’ � Instead of using pointers to the left and class BinaryTree right child of a node, use indices into an { private: private: array of nodes representing the binary tree. array of nodes representing the binary tree TreeNode *root; // pointer to root of Binary Tree � Also, use variable free as an index to the }; first position in the array that is available for first position in the array that is available for struct TreeNode // node in a Binary Tree: y a new entry. Use either the left or right { // place in Implementation file TreeItemType item; child indices to indicate additional, available TreeNode *leftChild; // pointer to TreeNode’s left positions. positions child child TreeNode *rightChild; // pointer to TreeNode’s right child � Together, the list of available positions in }; the array is called the free list . y

  6. Binary Tree: Table-Based Binary Tree: Table-Based Representation Representation Representation Representation root root Index Item Left Right Index Item Left Right * Mary Added under Nancy. Child Child Child Child Child Child Child Child 0 0 0 Jane 1 2 0 Jane 1 2 1 Bob 3 4 1 Bob 3 4 free free Jane Jane 2 Tom 5 -1 2 Tom 5 -1 6 7 3 Alan -1 -1 3 Alan -1 -1 Bob Bob Tom Tom Bob Bob Tom Tom 4 4 Ellen Ell -1 1 -1 1 4 4 Ellen Ell -1 1 -1 1 5 Nancy -1 -1 5 Nancy 6 -1 6 ? -1 7 6 6 Mary Mary -1 1 -1 1 Alan Ellen Nancy Alan Ellen Nancy 7 ? -1 8 7 ? -1 8 8 ? -1 9 8 ? -1 9 Mary 9 . . . . . . . . . 9 . . . . . . . . . Binary Tree: Table-Based Binary Tree: Table-Based Representation Representation Representation Representation root Index Item Left Right * Ellen deleted. const int MaxNodes = 100; // maximum size of a Binary Tree Child Child Child Child typedef string TreeItemType; t d f t i T It T // it // items in TreeNodes are string’s i T N d t i ’ 0 0 Jane 1 2 struct TreeNode // node in a Binary Tree { 1 Bob 3 -1 free Jane TreeItemType item; TreeItemType item; 2 Tom 5 -1 int leftChild; // index of TreeNode’s left child 4 int rightChild; // index of TreeNode’s right child 3 Alan -1 -1 }; Bob Bob Tom Tom 4 4 ? ? -1 1 7 7 class BinaryTree l Bi T { 5 Nancy 6 -1 private: TreeNode node[MaxNodes]; 6 6 Mary Mary -1 1 -1 1 int root; // index of root of Binary Tree Alan Nancy 7 ? -1 8 int free; // index of free list, linked by rightChild }; 8 ? -1 9 Mary 9 . . . . . . . . .

  7. Level Ordering g Array-Based Representation y p 1 1 1 1 2 3 2 3 4 5 6 7 4 5 6 7 Let i , 1 < i < n , be the number assigned to an element of a 1 2 3 6 complete binary tree. complete binary tree. Array-Based Representation y p Array-Based Representation y p � Array-based representations allow for � Array based representations allow for efficient traversal. Consider the node at index i index i. � Left Child is at index 2i+1. � Right Child is at index 2i+2 � Right Child is at index 2i+2. � Parent is at floor ( (i-1)/2 ).

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend