390 likes | 527 Views
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
E N D
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 • Linked List
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
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)
Graphs Behaviour Node A has three neighbors B,C, and D We will use adjacency matrix for graphs data structures
Trees • Hierarchical Data Structures • We have • Nodes (root, child nodes, leaf nodes) • Branches
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.
Trees Terminology Branch/Edge Child Nodes
By definition Child of a node u u She has few children “a,b,c,d” a d b c
By definition Parent node But root node does not have parent. Poor root u a d b c d has a parent node “u”
By definition subtree “u” has a subtree of a,b,c, and d u a d b c
By definition Depth of a node u Level 0 a d b c Level 1 e h f g Level 2
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
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
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
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
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
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
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.
Binary Tree u Left child Right child b c Left Subtree Right Subtree e h f g
Binary Tree : Code structTreeNode { intitem; //The data in this node. TreeNode*left; //Pointer to the left subtree. TreeNode*right;//Pointer to the right subtree. }
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()
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)
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.
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.
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.
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:
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.
Steps • Erase “6”, but keep its node
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.
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.
Let’s run sample program • Start your Visual Studio 2010 now • Create new C++ empty project • Project : Binary Tree with Traversal of DFS