1 / 39

Chapter 7

Chapter 7. Non Linear Data Structure www.asyrani.com. Data Structure. Linear – In Sequence. Non Linear – Not in sequence. Insertion/Deletion is not possible Example: Graphs Trees Hash Tree. Insertion/Deletion is possible in linear(sequential) fashion Example: Array Stacks Queues

edita
Télécharger la présentation

Chapter 7

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. Chapter 7 Non Linear Data Structure www.asyrani.com

  2. Data Structure Linear – In Sequence Non Linear – Not in sequence Insertion/Deletion is not possible Example: Graphs Trees Hash Tree • Insertion/Deletion is possible in linear(sequential) fashion • Example: • Array • Stacks • Queues • Linked List

  3. Non-linear Data Structures • Each node may be connected with two or more nodes in a non-linear arrangement • Removing one of the links could divide the data structure into two different data structures

  4. Example of non-linear ds

  5. Graphs It is non linear Set of nodes with set of connections One-way or two-way Directed (with arrow to show direction) or Non-Directed (two way links without any arrow)

  6. Graphs Behaviour Node A has three neighbors B,C, and D We will use adjacency matrix for graphs data structures

  7. Trees • Hierarchical Data Structures • We have • Nodes (root, child nodes, leaf nodes) • Branches

  8. TREES – TERMINOLOGY – TRAVERSAL – TYPE OF TREES TREES

  9. Definition of A Tree • Trees are a special case of a graph data structure. • The tree has nodes (shown with circles) that are connected with branches. Each node will have a parent node (except for the root) and may have multiple child nodes.

  10. “Trees” in real application

  11. Trees Terminology Branch/Edge Child Nodes

  12. By definition Child of a node u u She has few children “a,b,c,d” a d b c

  13. By definition Parent node But root node does not have parent. Poor root u a d b c d has a parent node “u”

  14. By definition subtree “u” has a subtree of a,b,c, and d u a d b c

  15. By definition Depth of a node u Level 0 a d b c Level 1 e h f g Level 2

  16. By definition Height of the tree u Level 0 a d b c Level 1 e h f g Level 2 Height of the tree = 2

  17. By definition Path Length u Each arrow length = 1 Thus, 4 arrows = length = 4 a d b c e h f g Each arrow length = 2 Thus, 4 arrows = length = 8 Total Length = 8 + 4 = 12

  18. By definition Degree u “u” has a degree = 4 “a” has a degree of 0 a d b c e h f g “b” has a degree of 4

  19. By definition Full Tree : This tree has a degree of 2 Height = 2 Full tree = (2^3 – 1)/(2-1) = 7 . . Or just calculate yourself how much node this tree has.. u b c e h f g

  20. Traversal Algorithm We have multiple traverse patterns Let’s go for Depth First Search The Mr.Algo visit “u” first Then Mr. Algo visit “b”, and “e” and “f”. After that, Mr. Algo back to “c” and visit “g” and “h” u b c e h f g

  21. Traversal Algorithm We have multiple traverse patterns Let’s go for Breath First Search The Mr.Algo visit “u” first Then Mr. Algo visit “b”, and “c”. After that, Mr. Algo back to “e”, “f”, “g”, and “h” u b c e h f g

  22. Rooted Tree/Ordered Tree Rooted Tree Ordered Tree Rooted tree where order of each child node is specified. [English] It is where you determined yourself where you put your node. But if we have spesific order, then we could have so called n-tree (binary,quad, oct-tree) • Any node can become a root for its own subnode • General term “Free Tree” • They have more family tree definition such as siblings or grandparents.

  23. Binary Tree

  24. Binary Tree u Left child Right child b c Left Subtree Right Subtree e h f g

  25. Binary Tree : Code structTreeNode { intitem; //The data in this node. TreeNode*left; //Pointer to the left subtree. TreeNode*right;//Pointer to the right subtree. }

  26. Binary Tree : Code intcountNodes( TreeNode *root ) { // Count the nodes in the binary tree to which // root points, and return the answer. if ( root == NULL ) return 0; // The tree is empty. It contains no nodes. else { intcount = 1; // Start by counting the root. count += countNodes(root->left); // Add the number of nodes // in the left subtree. count += countNodes(root->right); // Add the number of nodes // in the right subtree. return count; // Return the total. } } // end countNodes()

  27. Binary Tree Traversal Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted sequence Postordertraversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)

  28. Delete a node

  29. DELETE A NODE IN TREE There are three possible cases to consider: • Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree. • Deleting a node with one child: Remove the node and replace it with its child. • Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Replace the value of N with the value of R, then delete R.

  30. Deleting a node with two children from a binary search tree. The triangles represent subtrees of arbitrary size, each with its leftmost and rightmost child nodes at the bottom two vertices.

  31. Steps • There is another simple situation: suppose the node we're deleting has only one subtree. In the following example, `3' has only 1 subtree.

  32. Steps • To delete a node with 1 subtree, we just `link past' the node, i.e. connect the parent of the node directly to the node's only subtree. This always works, whether the one subtree is on the left or on the right. Deleting `3' gives us:

  33. Steps • Finally, let us consider the only remaining case: how to delete a node having two subtrees. For example, how to delete `6'? We'd like to do this with minimum amount of work and disruption to the structure of the tree.

  34. Steps • Erase “6”, but keep its node

  35. Steps • Find value that can be moved into vacated node while preserving BST properties. Pick X such that: • everything in left subtree < X • everything in right subtree > X • If we get X from left subtree, (2) is guaranteed. • For (1), X must be largest value in left subtree, i.e. 3.

  36. Steps • General algorithm: to delete N, if it has 2 subtrees, replace the value in N with the largest value in its left subtree and then delete the node with the largest value from its left subtree. • Note: the largest value has at most one subtree. • Why? It is the largest value: it doesn't have a right subtree.

  37. Let’s run sample program • Start your Visual Studio 2010 now • Create new C++ empty project • Project : Binary Tree with Traversal of DFS

More Related