Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
It is one of the fundamental data storage structure Combines the advantage of an ordered array and a linked list In an ordered array, you can search items fast
You can use binary search to find an item. The run time for it is O(logN) Insertion and deletion is slow, since you have to first find the item, then move all items that are greater one space up in the array. On an average, there are N/2 moves
In a linked list, insertion and deletion is fast. These operations take a constant time; O(1) time. However, finding an element is slow. You can start from the beginning and go through each item until you find the item you are looking for. On an average, you will visit N/2 objects or a run time of O(N). The tree data structure has both, fast search time and insertion/deletion time
UNH-M / NHTI - Professor Yusuf
Trees
Example of data arranged as a Tree
Root folder of your hard drive. There are sub folders and there are files. Files are the leaf nodes, i.e. there dont have any children An organization chart in a company
All employees report to CEO, which is the root node. Each group within the company has someone in charge and supervises employees.
John Smith
Mark Smith
JC Penny
Pam Sears
Bart Jones
A Shaw
B Roberts
C Kline
D Cooper
E Judd
What Is a Tree?
In computer programs, nodes often represent such entities as people, car parts, airline reservations, and so on. In other words, the typical items we store in any kind of data structure. In an OOP language such as Java, C# and C++, these real-world entities are represented by objects. The edges between the nodes represent the way the nodes are related. Edges are likely to be represented in a program by references or pointers (C++) Edges represent a path from one node to another. In fact, the only way to get from node to node is to follow a path along the lines. In trees, you are restricted to going in one direction along edges, from the root downward to the leaves. In a typical tree, there is one node in the top row of a tree. It is called the root node and has edges connecting to nodes on the second row, which in turn are may be connected to nodes in the third row and so on. Thus trees are small on the top and large on the bottom. This may seem upside-down compared with real trees, but generally a program starts an operation at the small end of the tree
UNH-M / NHTI - Professor Yusuf
What Is a Tree?
Trees are part of the graph family. There are many conditions/ restrictions that separates a graph from a tree Trees always have one node as a root node. All nodes in a tree except for the root, have only one parent node. All nodes can have an arbitrary number of children Any other node in the tree can be reached from any other node There are no cycles. A cycle exists when you start from a node and there is a path back to that same node. In a tree, there is only one path to a node. The number of edges in a tree is always one less than the number of nodes There are different kinds of trees. The tree shown in Figure 8.1 has more than two children per node. In an Binary tree, each node can have two or less nodes
UNH-M / NHTI - Professor Yusuf
What Is a Tree?
A General (Non-Binary) Tree
nodes
edges
Figure 8.1
A tree consists of nodes connected by edges. Above, the nodes are represented as circles and the edges are represented by lines. Generally speaking, a tree is falls into the category of graphs, which we will study later. Specific restrictions on a the tree distinguishes it from a graph.
Trees - Terminology
Path
A path from one node to another node, along the edges. There is only path from the root to a node
Root
The topmost node. It does not have any parents. All other nodes are it descendents
Parent
Any node that has a parent and a child, excluding the root node
Child
Any node that has a parent and may or may not have children
Leaf
A child node that does not have any children of its own. At the lowest level of the tree. Typically, this is where new nodes are inserted
UNH-M / NHTI - Professor Yusuf
Trees - Terminology
Subtree
A node that has children and descendents of its own. It is like the root node, except that it is one or more level below the root node.
Visiting
When you visit a node, you specifically go to that node, for the purpose of carrying out some operation, like displaying the value of its node
Traversing
Going to nodes in a specific order, from one node to another until you reach the end or find the node you may be looking for
Levels
How many generations. Root is level 0, then its children are at level 1 and so on.
Keys
The data value that is associated with the node. It code be a number or a reference to an object
UNH-M / NHTI - Professor Yusuf
Tree Terminology
Root B B A The dashed lined is a path C Level 0
Level 1
Level 2
Level 3
Figure 8.2
Invalid Tree
A non-tree. A node can have only path. This tree contains a cycle
UNH-M / NHTI - Professor Yusuf
Figure 8.3
Binary Trees
Binary Tree
Is a type of tree whose nodes can contain at most, two children. A left child or a right child or both It combines the advantages of an ordered array (quick search) and linked list (quick insert and delete) Like other trees, it consists of nodes connected by edges. Nodes represent data, edges show the relationship between nodes Like all trees, there is only one root and it is the top most node
An Analogy
One commonly encountered tree is the hierarchical file structure in a computer system The root directory on a Windows system is C:\ and / on Unix systems. They can be considered a tree's root node The directories one level below the root directory are its children. There may be many levels of subdirectories. Files represent leaves and they have no children of their own Clearly a hierarchical file structure is not a binary tree, because a directory may have many children A complete pathname, such as H:\Lectures\UNHM\ET601\Slides\08 binary trees.ppt corresponds to the path from the root to the 08 binary trees.ppt. Terms used for the file structure, such as root and path, were borrowed from tree theory.
UNH-M / NHTI - Professor Yusuf
11
An Analogy - Differences
A hierarchical file structure differs in a significant way from the trees In the file structure, subdirectories contain no data; only references to other subdirectories or to files. Only files contain data In a tree, every node contains data (a personnel record, car-part specifications, or whatever). In addition to the data, all nodes except leaves contain references to other nodes.
12
Binary Trees
A Binary Search Tree imposes certain rules on how items in the tree are arranged.
All nodes that are left descendents have values smaller than their parent nodes (A left childs value is lower than the parent node) All nodes that are right descendents have values greater than their parent nodes (A right childs value is greater than the parent node)
This results in a sub-linear search time. It also results in quick insertions and deletions. The runtime is O (log N)
13
14
14
39
61
84
23 34
47 79
In a Binary Search Tree, a nodes left childs key value must be lower than the nodes key value. Also a nodes right childs key value must be greater than the nodes key value.
UNH-M / NHTI - Professor Yusuf
Figure 8.4
15
Unbalanced Trees
Trees become unbalanced because of the order in which the data items are inserted If key values are inserted randomly, the tree will be more or less balanced. However, if an ascending sequence (like 11, 18, 33, 42, 65, and so on) or a descending sequence is generated, all the values will be right children (if ascending) or left children (if descending) and the tree will be unbalanced
16
Drawbacks
If sorted data is entered into a tree, the tree becomes unbalanced and the worst case for a search becomes O(N) Tree in Fig A is unbalanced, it received 5, 4, 3, 2, 1 Tree in Fig B is balanced. It received 4, 5, 2, 1, 3 To search data in Fig A, you may have to go down 4 levels. To search data in Fig B, at most, you will have to go down 2 levels 5 4 3 2 I 1 3 Level 0 Level 1 Level 2 Level 3 Level 4
Fig A
Level 0
Level 1
I 1
Fig B
J 3
Level 2
17
An Unbalanced Tree
90 42 Unbalanced subtree In an unbalanced tree, most of the nodes are on side. 10 31 83 Note: there is no left child of Node 75 95
23
75
18
78
87
Figure 8.6
18
19
27
13
51
57 > 51
26 33
58 57 < 58
82
57 = 57
57
60
Figure 8.7
Finding node 57
20
21
{
int value; Tree theTree = new Tree(); theTree.insert(25, 1.2);
22
23
25
Inserting a Node
Nodes are inserted at the leaf We can search for the new node. If it is not there, then we must be at (a leaf) the place where the node should be inserted Algorithm is similar to find except we must also keep track of the parent node Once we are at the leaf node, this node will now become the parent of the new node The new node will become the right child if value of new node is > than parent or the new node will become the left child if the value of new node is < than parent
26
60
60
45 > 40
40
40
30
50
45 < 50
30
50
null
Found null
45
Figure 8.8
a) Before insertion
UNH-M / NHTI - Professor Yusuf
b) After insertion
27
28
29
Note: there is difference between visiting and traversal. In traversal, we dont care about the key values, just about the nodes children. We are simply passing through In visiting, we explicitly go to that node to perform some action, like printing or displaying the nodes value
UNH-M / NHTI - Professor Yusuf
30
Inorder Traversal
Causes all nodes to be visited in ascending order Used for creating a sorted list of data A recursive method is used to traverse the entire tree with current node as its argument
1. Call itself to traverse the nodes left subtree 2. Visit the node 3. Call itself to traverse the nodes right subtree
private void inOrder(Node localRoot) { if (localRoot != null) {
inOrder(localRoot.leftChild);
System.out.print(localRoot.iData + " "); inOrder(localRoot.rightChild); }
31
Traversing a Tree
In inOrder() method applied (recursively) to a three-node tree. inOrder (A) 1. 2. 3. Call inOrder (B) Visit A Call inOrder (C) B A
inOrder (B)
1. Call inOrder (null) 2. Visit B 3. Call inOrder (null)
inOrder (null)
inOrder (null)
inOrder (null)
inOrder (null)
Returns
Returns
Returns
Returns
Figure 8.9
32
Traversing a Tree
1 7. Visit 30 2 6 5
50
13. Visit 50 14
18
60 15 16. Visit 60 17
30 12 8 9
20 3
40
10. Visit 40 11
4. Visit 20
Traveling a tree in-order. Keep going to the Left node (recursively), go back to the parent, then keep going (recursively) to the right node
Figure 8.10
33
Traversing a Tree
inOrder (50) Call inOrder (30) Visit 50 Call inOrder (60) inOrder (30) Call inOrder (20) Visit 30 Call inOrder (40) inOrder (20) Call inOrder (null) Visit 20 Call inOrder (null) inOrder (40) Call inOrder (null) Visit 40 Call inOrder (null) inOrder (null) inOrder (null) Returns inOrder (null) Returns Returns inOrder (null) inOrder (null) Returns Returns inOrder (null) Returns 20 inOrder (60) 1. Call inOrder (null) 2. Visit 60 3. Call inOrder (null) 50 30 40
60
34
Preorder Traversal
Visit the node Call itself to traverse the nodes left subtree Call itself to traverse the nodes right subtree
35
Traversing a Tree
* A +
A*(B+C)
UNH-M / NHTI - Professor Yusuf
36
47
71
Minimum 22 53
67
11
33 50
60
To find the minimum value of a tree, keep going to the left most node, until the left child is null. To find the Maximum value, keep traversing the right child until the next right child is null Figure 8.12
17
49
51
37
Deleting a node
Deleting a Node in the tree
Most complicated common operation in the tree First find the node to delete. There are three possibilities
1. The Node to be deleted is a leaf node, there are no children 2. The Node to be deleted has one child 3. The Node to be delete has two children
38
10
null
3 7 3 7
Awaiting garbage
collection
39
go left?
or go right?
40
There are two possibilities. 1) We do not find the node and return null. Therefore no deletion is necessary. 2) The data of the node matches the key. We then exit the loop
// Now see if this case 1, 2 or 3 Case 1: Leaf node, therefore no children. // Simply delete it, but make sure that it is not the root // root is handled differently if(current.leftChild==null && current.rightChild==null) { if(current == root) // if root, root = null; // tree is empty else if(isLeftChild) parent.leftChild = null; // disconnect else // from parent parent.rightChild = null; }
UNH-M / NHTI - Professor Yusuf
41
42
52
52
Node to be deleted 71 63
48 63
80
Node to be deleted 71 63
52
Node to be deleted 71
52
48
48 63
43
80
Parent
80
52 52 48 63 63
While (cur.iData != key) parent = current,
52
71 48 71 48 63 71
Current
44
80
// If no right child, replace with left subtree else if(current.rightChild==null) if(current == root) Parent root = current.leftChild;
52
IsLeft = False
else if (isLeftChild)
parent.leftChild = current.leftChild; else parent.rightChild = current.leftChild;
48 63
71
45
52
To be deleted
48
52
71
48
63
63
67 b) After deletion
Figure 8.14
a) Before deletion
67
46
47
To be deleted
25
15
35
20 30
40
20
Cannot replace with subtree. If node 25 is replaced with node 35, tree looks like this. It is two left children
Figure 8.15
48
25
30
15
35
15
35
20 30
40
20
40
Successor to 25
a) Before deletion b) After deletion
Figure 8.16
49
50
38 26
90
Successor
41
60
78
92
No left child
43
74
Figure 8.17
51
Successor
41
60 78
92
No left child
43
74
Figure 8.17
52
50 75 26 26
26 50
75
26 26
26
53
Definitions
Data Type
In computer terms, type of data. Primarily refers to the Primitive or built-in (to compiler) type such as int for integers, float for floating point numbers or char for characters.
Data Structure
A way to represent data or a way of storing data in a computer
54
References
http://en.wikipedia.org/wiki/Main_Page Data Structures and Algorithms in Java 2nd Edition. Robert Lafore, SAMS http://www.otherwise.com/Lessons/index.html
55