Download presentation
Presentation is loading. Please wait.
1
Non Linear Data Structure www.asyrani.com
Chapter 7 Non Linear Data Structure
2
Linear – Data is in sequential
Non Linear – Data is not in sequential
3
Data Structure Linear – In Sequence Non Linear – Not in sequence
Insertion/Deletion is possible in linear(sequential) fashion Example: Array Stacks Queues Linked List Insertion/Deletion is not possible Example: Graphs Trees Hash Tree
4
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
5
Example of non-linear ds
6
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)
7
Graphs Behaviour Node A has three neighbors B,C, and D
We will use adjacency matrix for graphs data structures
8
Trees Hierarchical Data Structures We have
Nodes (root, child nodes, leaf nodes) Branches
9
TREES – TERMINOLOGY – TRAVERSAL – TYPE OF TREES
10
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.
11
“Trees” in real application
12
Trees Terminology Branch/Edge Child Nodes
13
By definition Child of a node u u a b c d
She has few children “a,b,c,d” a b c d
14
By definition Parent node u a b c d
But root node does not have parent. Poor root u a b c d d has a parent node “u”
15
By definition subtree “u” has a subtree of a,b,c, and d u a b c d
16
By definition Depth of a node u a b c d e f g h Level 0 Level 1
17
By definition Height of the tree u a b c d e f g h Level 0 Level 1
18
By definition Path Length u a b c d e f g h Each arrow length = 1
Thus, 4 arrows = length = 4 a b c d e f g h Each arrow length = 2 Thus, 4 arrows = length = 8 Total Length = = 12
19
By definition Degree u a b c d e f g h “u” has a degree = 4
“a” has a degree of 0 a b c d e f g h “b” has a degree of 4
20
By definition Full Tree : u b c e f g h 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 f g h
21
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 f g h
22
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 f g h
23
Rooted Tree/Ordered 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. 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)
24
Binary Tree
25
Binary Tree u b c e f g h Left child Right child Left Subtree
Right Subtree e f g h
26
Binary Tree : Code struct TreeNode { int item; //The data in this node. TreeNode *left; //Pointer to the left subtree. TreeNode *right;//Pointer to the right subtree. }
27
Binary Tree : Code int countNodes( 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 { int count = 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()
28
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 Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)
29
Delete a node
30
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.
31
Deleting a node with two children from a binary search tree
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.
32
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.
33
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:
34
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.
35
Steps Erase “6”, but keep its node
36
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.
38
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.
39
Let’s run sample program
Start your Visual Studio 2010 now Create new C++ empty project Project : Binary Tree with Traversal of DFS
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.