Sei sulla pagina 1di 55

Trees

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

UNH-M / NHTI - Professor Yusuf

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.

UNH-M / NHTI - Professor Yusuf

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

D is the left child of B D D

B is the parent of D and E F

Level 1

E is the right child of B I A subtree with F as its root.

Level 2

Level 3

H, E, I, J, and G leaf nodes


UNH-M / NHTI - Professor Yusuf

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

Common Binary Tree operations


finding a node with a given key inserting a new node traversing the tree deleting a node
10

UNH-M / NHTI - Professor Yusuf

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.

UNH-M / NHTI - Professor Yusuf

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)

UNH-M / NHTI - Professor Yusuf

13

Binary Search Trees - Rules


Rules
Only one Root All nodes except the root have one parent There is a path to all the nodes (all nodes are reachable) There is only one path to each node There are no cycles. That is, if you start from a given node, there is no path that takes you back to the same (starting) node A left childs value is lower than the parent node. A right childs value is greater than the parent node

UNH-M / NHTI - Professor Yusuf

14

A Binary Search Tree


53 30 72

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

UNH-M / NHTI - Professor Yusuf

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

UNH-M / NHTI - Professor Yusuf

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

An unbalanced tree (with an unbalanced subtree.)


UNH-M / NHTI - Professor Yusuf

18

Representing Tree in Java Code


There are several approaches on how to represent a tree in the computer's memory The most common way is to store the nodes at unrelated locations in memory and connect them using references in each node that point to its children. You can also represent a tree in memory as an array, with nodes in specific positions stored in corresponding positions in the array

UNH-M / NHTI - Professor Yusuf

19

Finding a node in a Tree


57 < 63 63 current 57 > 27 70
public Node find(int key) { Node current = root; while(current.iData != key) 80 { if(key < current.iData) current = current.leftChild; else current = current.rightChild; 92

27

13

51

57 > 51

if(current == null) return null;

26 33

58 57 < 58

82

} return current; // end find()

57 = 57

57

60

Figure 8.7

Finding node 57
20

UNH-M / NHTI - Professor Yusuf

Code for Tree


class Tree { // first node of tree // constructor // no nodes in tree yet // find node with given key // (assumes non-empty tree) // end find() private Node root; public Tree() { root = null; } public Node find(int key) { }

public void insert(int id, double dd) { } { } }


UNH-M / NHTI - Professor Yusuf

// end insert() // (assumes non-empty list) // end delete()

public boolean delete(int key) // delete node with key

21

Code for Tree


class TreeApp { public static void main(String[] args) throws IOException

{
int value; Tree theTree = new Tree(); theTree.insert(25, 1.2);

node found = theTree.find(25); // find the node


if (found != null) System.out.println("Found node with 25") else

System.out.println("Found node with 25")


} }
UNH-M / NHTI - Professor Yusuf

22

Finding a node in a Tree


Finding a node is the simplest operation in the tree. We compare the key with node. If it matches, we return. If key is < the node, then we go to the nodes left child and repeat the search If the key is > the node, then we go to the nodes right child and repeat the search If we find the key , we return the key, else we return null
public Node find(int key) // find node with given key { // (assumes non-empty tree) Node current = root; // start at root while(current.iData != key) // while no match, { if(key < current.iData) // go left? current = current.leftChild; else // or go right? current = current.rightChild; if(current == null) return null; } return current; // end find() // if no child, // didn't find it // found it

UNH-M / NHTI - Professor Yusuf

23

Finding a node in a Tree


current holds the node it is currently examining and key is the value to be found. The routine starts at the root by setting current to the root. In the while loop, it compares the value to be found, key, with the value of the iData field (the key field) in the current node.
If key is less than this field, then current is set to the node's left child. If key is greater than (or equal) to the node's iData field, then current is set to the node's right child.

Can't Find the Node


If current becomes null, then we couldn't find the next child node in the sequence; we've reached the end of the line without finding the node we were looking for, so it can't exist. We return null to indicate this fact.

Found the Node


If the condition of the while loop is not satisfied, so that we exit from the bottom of the loop, then the iData field of current is equal to key; that is, we've found the node we want. We return the node, so that the routine that called find() can access any of the node's data 24

UNH-M / NHTI - Professor Yusuf

Finding a node in a Tree


Efficiency
How long it takes to find a node? It depends on how many levels down the value is located. To search up to 31 nodes, you only need to go down 5 levels This is O(logN) time, or more specifically O(log2N) time; the logarithm to the base 2.

UNH-M / NHTI - Professor Yusuf

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

UNH-M / NHTI - Professor Yusuf

26

Inserting a Node in a Tree


Inserting a node with 45
Note: current end up as null, therefore we need a reference to parent

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

Inserting a new node in a tree


public void insert(int id, double dd) { Node newNode = new Node(); // make new node newNode.iData = id; // insert data newNode.dData = dd; if(root==null) // no node in root root = newNode; else // root occupied { Node current = root; // start at root Node parent; // previous location while(true) // (exits internally) { parent = current; if(id < current.iData) // go left? { current = current.leftChild; if(current == null) // if end of the line, { // insert on left parent.leftChild = newNode; return; } }- Professor // end if go left UNH-M / NHTI Yusuf

28

Inserting a new node in a tree


} // end if go left else // or go right? { current = current.rightChild; if(current == null) // if end of the line { // insert on right parent.rightChild = newNode; return; } } // end else go right } // end while } // end else not root // end insert()

UNH-M / NHTI - Professor Yusuf

29

Traversing the Tree


Not very common operation or very fast, but useful The ways to traverse a tree
Preorder Inorder (most common) Postorder

Simplest way to traverse a tree is to use recursion


Start from the root Call itself to traverse the nodes left subtree Go up one level and visit the parent node Call itself to traverse the nodes right subtree

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); }

UNH-M / NHTI - Professor Yusuf

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 (C) 1. Call inOrder (null) 2. Visit C 3. Call inOrder (null)

inOrder (null)

inOrder (null)

inOrder (null)

inOrder (null)

Returns

Returns

Returns

Returns

Figure 8.9

UNH-M / NHTI - Professor Yusuf

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

UNH-M / NHTI - Professor Yusuf

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

UNH-M / NHTI - Professor Yusuf

34

Preorder & Postorder Traversal


A binary tree (not Binary Search Tree) can be used represent an algebraic expression that represent binary arithmetic operators The root holds the operators, nodes hold the operands This is same as infix notation

Preorder Traversal
Visit the node Call itself to traverse the nodes left subtree Call itself to traverse the nodes right subtree

UNH-M / NHTI - Professor Yusuf

35

Traversing a Tree
* A +

Infix: Prefix: Postfix:

A*(B+C) *A+BC ABC+*

Tree representing an algebraic expression.


Figure 8.11

A*(B+C)
UNH-M / NHTI - Professor Yusuf

36

Finding Maximum and Minimum Values


63

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

UNH-M / NHTI - Professor Yusuf

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

Case 1 is the simplest


Find the parent of the node. Change the field in the parent to point to null, instead of that node. The node will remain, but will no longer be part of the tree. Eventually, GC (garbage collector) will reclaim its memory

UNH-M / NHTI - Professor Yusuf

38

Deleting a node with no children


10

10

null
3 7 3 7

Awaiting garbage

collection

a) Before deleting node 7

b) After deleting node 7


Figure 8.13

UNH-M / NHTI - Professor Yusuf

39

public boolean delete(int key) // delete node with given key

// (assumes non-empty list)


Node current = root; Node parent = root; boolean isLeftChild = true; while(current.iData != key) // { parent = current; if(key < current.iData) // { isLeftChild = true; current = current.leftChild; } else // { isLeftChild = false; current = current.rightChild; } if(current == null) // return false; // } // end while, found node to delete

search for node

go left?

or go right?

end of the line, didn't find it

UNH-M / NHTI - Professor Yusuf

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

Deleting a node Case 2


Deleting a Node in the tree when there is one child
Slightly more difficult This node has only two connections, one to its parent and one to its only child Simply connect the link from the nodes parent to the nodes child Garbage collection will remove the node since no is referencing it There are four variations of this
The node (to be deleted) is left child of its parent The node (to be deleted) is right child of its parent The child of the node (to be deleted) is right child The child of the node (to be deleted) is left child Parent.leftChild to node.leftChild Parent.leftChild to node.rightChild Parent.rightChild to node.leftChild Parent.rightChild to node.rightChild

UNH-M / NHTI - Professor Yusuf

42

Node TBD is a RC and has a LC 80

Node TBD is a RC and has a RC 80

52

Node to be deleted 71 48 80 Node TBD is a LC and has a LC

52

Node to be deleted 71 63

48 63

80

Node to be deleted 71 63

52

Node to be deleted 71

52

Node TBD is a LC and has a RC

48

48 63

UNH-M / NHTI - Professor Yusuf

43

Node TBD is a RC and has a LC 80


Current Parent

80

Parent

80

52 52 48 63 63
While (cur.iData != key) parent = current,

IsLeft = True Current

52

Parent IsLeft = False

71 48 71 48 63 71

Current

if key < cur.iData


isLeftChild = true; cur = cur.left; else isLeftChild = false; cur = cur.right;
UNH-M / NHTI - Professor Yusuf

44

Node TBD is a RC and has a LC


// Case 2:

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

Current par.rightChild cur.leftChild

// if no left child, replace with right subtree


else if(current.leftChild==null) if(current == root) root = current.rightChild; else if (isLeftChild) parent.leftChild = current.rightChild; else parent.rightChild = current.rightChild;

UNH-M / NHTI - Professor Yusuf

45

Deleting a Node with one Child


80 80

52

To be deleted
48

52

71

48

63

63

67 b) After deletion
Figure 8.14

a) Before deletion

67

Deleting a node with one child.


UNH-M / NHTI - Professor Yusuf

46

Deleting a node Case 3


Deleting a Node in the tree when there are two children
Most complicated Cannot just connect the parents link to its childs link We must find the successor. The node that has the next higher value of the node we are deleting. Then re-build the tree Successor is the smallest value node in the right subtree of the node to be deleted Successor node has the next highest value compared to the node to be deleted

UNH-M / NHTI - Professor Yusuf

47

Recursion Tree Terminology


Which node goes here? 50 35 Root of right subtree 30 15 40 50

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

UNH-M / NHTI - Professor Yusuf

48

Recursion Tree Terminology


50 To be deleted 50

25

30

15

35

15

35

20 30

40

20

40

Successor to 25
a) Before deletion b) After deletion

Node 25 replaced by its successor (node 30)


UNH-M / NHTI - Professor Yusuf

Figure 8.16

49

Deleting Nodes Case 3


Case 3: Deleting a Node with 2 Children
Find the node to be deleted Find the node that has the next higher value than the node to be deleted This node will be the node that will replace the Node to be deleted. It is also called the successor node

UNH-M / NHTI - Professor Yusuf

50

38 26

To find successor of this node, Go to the right child


72

Go to left child 55 Go to left child

90

Successor

41

60

78

92

No left child

43

74

Figure 8.17

UNH-M / NHTI - Professor Yusuf

51

Finding the Successor Node


38 26 Go to left child 55 Go to left child 90 To find successor of this node, Go to the right child 72

Successor

41

60 78

92

No left child

43

74
Figure 8.17

UNH-M / NHTI - Professor Yusuf

52

50 75 26 26

26 50

75
26 26

UNH-M / NHTI - Professor Yusuf

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

Abstract Data Type

UNH-M / NHTI - Professor Yusuf

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

UNH-M / NHTI - Professor Yusuf

55

Potrebbero piacerti anche