08 binary trees

55
1 UNH-M / NHTI - Professor Yusuf 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

Upload: tristan-burgess

Post on 20-Jan-2016

13 views

Category:

Documents


0 download

DESCRIPTION

Binary Trees

TRANSCRIPT

Page 1: 08 Binary Trees

1UNH-M / NHTI - Professor Yusuf

TreesTrees

– 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

Page 2: 08 Binary Trees

2UNH-M / NHTI - Professor Yusuf

TreesTrees

– 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 don’t 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

Page 3: 08 Binary Trees

3UNH-M / NHTI - Professor Yusuf

What Is a Tree?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

Page 4: 08 Binary Trees

4UNH-M / NHTI - Professor Yusuf

What Is a Tree?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

Page 5: 08 Binary Trees

5UNH-M / NHTI - Professor Yusuf

What Is a Tree?What Is a Tree?

Figure 8.1

nodes

edges

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.

A General (Non-Binary) Tree

Page 6: 08 Binary Trees

6UNH-M / NHTI - Professor Yusuf

Trees - TerminologyTrees - 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 it’s own. At

the lowest level of the tree. Typically, this is where new nodes are inserted

Page 7: 08 Binary Trees

7UNH-M / NHTI - Professor Yusuf

Trees - TerminologyTrees - Terminology

• Subtree– A node that has children and descendents of it’s 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 it’s 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 it’s 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

Page 8: 08 Binary Trees

8UNH-M / NHTI - Professor Yusuf

Figure 8.2

Tree TerminologyTree Terminology

D E

B

A

C

H

F

I J

G

Root

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

D is the left child

of B

A subtree with F as its root.

E is the right child of B

B is the parent of D and E

The dashed lined is a

path

Level 0

Level 3

Level 1

Level 2

D E

B

Page 9: 08 Binary Trees

9UNH-M / NHTI - Professor Yusuf

Invalid TreeInvalid Tree

Figure 8.3A non-tree. A node can have only path. This tree contains a cycle

Page 10: 08 Binary Trees

10UNH-M / NHTI - Professor Yusuf

Binary TreesBinary 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

Page 11: 08 Binary Trees

11UNH-M / NHTI - Professor Yusuf

An AnalogyAn 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\UNH-M\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.

Page 12: 08 Binary Trees

12UNH-M / NHTI - Professor Yusuf

An Analogy - DifferencesAn 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.

Page 13: 08 Binary Trees

13UNH-M / NHTI - Professor Yusuf

Binary TreesBinary 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 child’s value is lower than the parent node)

• All nodes that are right descendents have values greater than their parent nodes (A right child’s 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)

Page 14: 08 Binary Trees

14UNH-M / NHTI - Professor Yusuf

Binary Search Trees - RulesBinary 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 child’s value is lower than the parent node. A right child’s value is greater than the parent node

Page 15: 08 Binary Trees

15UNH-M / NHTI - Professor Yusuf

A Binary Search TreeA Binary Search Tree

Figure 8.4

53

72

84

61

79

47

39

30

14

23

34

9

In a Binary Search Tree, a node’s left child’s key value must be lower than the node’s key value. Also a node’s right child’s key value must be greater than the node’s key value.

Page 16: 08 Binary Trees

16UNH-M / NHTI - Professor Yusuf

Unbalanced TreesUnbalanced 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

Page 17: 08 Binary Trees

17UNH-M / NHTI - Professor Yusuf

DrawbacksDrawbacks

– 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

I3

5 Level 0

3

4

1

2

Level 1

Level 2

Level 3

Level 4

4

2

I J

5

1 3

Level 0

Level 1

Level 2

Fig A Fig B

Page 18: 08 Binary Trees

18UNH-M / NHTI - Professor Yusuf

An Unbalanced TreeAn Unbalanced Tree

75

42

90

95

83

78

87

23

31

10

7 18

Figure 8.6

An unbalanced tree (with an unbalanced subtree.)

Unbalanced subtree

In an unbalanced tree, most ofthe nodes are on side.

Note: there is no left child of Node 75

Page 19: 08 Binary Trees

19UNH-M / NHTI - Professor Yusuf

Representing Tree in Java CodeRepresenting 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

Page 20: 08 Binary Trees

20UNH-M / NHTI - Professor Yusuf

Finding a node in a TreeFinding a node in a Tree

Finding node 57

public Node find(int key){ Node current = root; while(current.iData != key) { if(key < current.iData) current = current.leftChild; else current = current.rightChild; if(current == null) return null; } return current;} // end find()

Figure 8.7

58

33

51

27

13

26

92

82

63

70

80

57

60

57 < 63

57 > 27

57 > 51

57 < 58

57 = 57

current

Page 21: 08 Binary Trees

21UNH-M / NHTI - Professor Yusuf

Code for TreeCode for Tree

class Tree {

private Node root; // first node of tree

public Tree() // constructor

{ root = null; } // no nodes in tree yet

public Node find(int key) // find node with given key

{ // (assumes non-empty tree)

} // end find()

public void insert(int id, double dd)

{

} // end insert()

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

{ // (assumes non-empty list)

} // end delete()

}

Page 22: 08 Binary Trees

22UNH-M / NHTI - Professor Yusuf

Code for TreeCode 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")

}

}

Page 23: 08 Binary Trees

23UNH-M / NHTI - Professor Yusuf

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 node’s left child and repeat the searchIf the key is > the node, then we go to the node’s right child and repeat the searchIf 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) // if no child, return null; // didn't find it } return current; // found it } // end find()

Finding a node in a TreeFinding a node in a Tree

Page 24: 08 Binary Trees

24UNH-M / NHTI - Professor Yusuf

Finding a node in a TreeFinding 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

Page 25: 08 Binary Trees

25UNH-M / NHTI - Professor Yusuf

Finding a node in a TreeFinding 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.

Page 26: 08 Binary Trees

26UNH-M / NHTI - Professor Yusuf

Inserting a NodeInserting 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

Page 27: 08 Binary Trees

27UNH-M / NHTI - Professor Yusuf

Inserting a Node in a TreeInserting a Node in a Tree

60

50

40

30

a) Before insertion

null

60

50

40

30

45

b) After insertion

Figure 8.8

Inserting a node with 45

45 > 40

45 < 50

Found null

Note: current end up as null, therefore we need a reference to parent

Page 28: 08 Binary Trees

28UNH-M / NHTI - Professor Yusuf

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; } } // end if go left

Inserting a new node in a treeInserting a new node in a tree

Page 29: 08 Binary Trees

29UNH-M / NHTI - Professor Yusuf

} // 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()

Inserting a new node in a treeInserting a new node in a tree

Page 30: 08 Binary Trees

30UNH-M / NHTI - Professor Yusuf

Traversing the TreeTraversing 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 node’s left subtree• Go up one level and visit the parent node• Call itself to traverse the node’s right subtree

– Note: there is difference between visiting and traversal. In traversal, we don’t care about the key values, just about the node’s children. We are simply passing through

– In visiting, we explicitly go to that node to perform some action, like printing or displaying the node’s value

Page 31: 08 Binary Trees

31UNH-M / NHTI - Professor Yusuf

Inorder TraversalInorder 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 it’s argument1. Call itself to traverse the node’s left subtree2. Visit the node3. Call itself to traverse the node’s right subtree

private void inOrder(Node localRoot)

{

if (localRoot != null)

{

inOrder(localRoot.leftChild);

System.out.print(localRoot.iData + " ");

inOrder(localRoot.rightChild);

}

}

Page 32: 08 Binary Trees

32UNH-M / NHTI - Professor Yusuf

Traversing a TreeTraversing a Tree

A

B C

In inOrder() method applied (recursively) to a three-node tree.inOrder

(A)1. Call inOrder

(B)

2. Visit A

3. Call inOrder (C)

1. Call inOrder (null)

2. Visit B

3. Call inOrder (null)

1. Call inOrder (null)

2. Visit C

3. Call inOrder (null)

Returns Returns Returns Returns

inOrder (null)

inOrder (null)

inOrder (null)

inOrder (null)

inOrder (C)

inOrder (B)

Figure 8.9

Page 33: 08 Binary Trees

33UNH-M / NHTI - Professor Yusuf

Traversing a TreeTraversing a Tree

Figure 8.10

50

40

30

20

60

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

1

2 12

5 119

86

3

4. Visit 20

7. Visit 30

10. Visit 40

13. Visit 50

17

15

14

16. Visit 60

18

Page 34: 08 Binary Trees

34UNH-M / NHTI - Professor Yusuf

Traversing a TreeTraversing a Tree

50

inOrder (50)

1. Call inOrder (null)

2. Visit 60

3. Call inOrder (null)

Returns

Returns

inOrder (null)

inOrder (null)

inOrder (60)

Returns

Returns

inOrder (null)

inOrder (null)

60

30

20

40

inOrder (30)Call inOrder

(20)

Visit 30

Call inOrder (40)

Call inOrder (null)

Visit 20

Call inOrder (null)

inOrder (40)

Call inOrder (null)

Visit 40

Call inOrder (null)

Returns

Returns

inOrder (null)

inOrder (null)

Call inOrder (30)Visit 50

Call inOrder (60)

inOrder (20)

Page 35: 08 Binary Trees

35UNH-M / NHTI - Professor Yusuf

Preorder & Postorder TraversalPreorder & 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 node’s left subtree– Call itself to traverse the node’s right subtree

Page 36: 08 Binary Trees

36UNH-M / NHTI - Professor Yusuf

Traversing a TreeTraversing a Tree

+

Figure 8.11

*

A

B C

Tree representing an algebraic expression.

A*(B+C)

Infix: A*(B+C)

Prefix: *A+BC

Postfix: ABC+*

Page 37: 08 Binary Trees

37UNH-M / NHTI - Professor Yusuf

Finding Maximum and Minimum Values

Finding Maximum and Minimum Values

47

63

53

22

33

67

71

49

51

60

50

17

11

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

Minimum

Page 38: 08 Binary Trees

38UNH-M / NHTI - Professor Yusuf

Deleting a nodeDeleting 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 children2. The Node to be deleted has one child3. 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 it’s memory

Page 39: 08 Binary Trees

39UNH-M / NHTI - Professor Yusuf

Deleting a node with no childrenDeleting a node with no children

10

7

3

Figure 8.13

10

7

5

3

5

a) Before deleting node 7

b) After deleting node 7

Awaiting

garbage

collection

null

Page 40: 08 Binary Trees

40UNH-M / NHTI - Professor Yusuf

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) // search for node { parent = current; if(key < current.iData) // go left? { isLeftChild = true; current = current.leftChild; } else // or go right? { isLeftChild = false; current = current.rightChild; } if(current == null) // end of the line, return false; // didn't find it } // end while, found node to delete

Page 41: 08 Binary Trees

41UNH-M / NHTI - Professor Yusuf

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 3Case 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 parentparent.rightChild = null;

}

Page 42: 08 Binary Trees

42UNH-M / NHTI - Professor Yusuf

Deleting a node – Case 2Deleting 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 it’s parent and one to

it’s only child– Simply connect the link from the node’s parent to the node’s 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 it’s parent• The node (to be deleted) is right child of it’s 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

Page 43: 08 Binary Trees

43UNH-M / NHTI - Professor Yusuf

80

71

52

48

63

Node to be deleted

80

71

52

48

63

Node to be deleted

80

48

52

71

63

Node to be deleted

80

48

52

71

63

Node to be deleted

Node TBD is a RC and has a LC Node TBD is a RC and has a RC

Node TBD is a LC and has a RC

Node TBD is a LC and has a LC

Page 44: 08 Binary Trees

44UNH-M / NHTI - Professor Yusuf

80

71

52

48

63

Node TBD is a RC and has a LC

While (cur.iData != key)

parent = current,

if key < cur.iData

isLeftChild = true; cur = cur.left;

else

isLeftChild = false; cur = cur.right;

CurrentParent

Parent80

71

52

48

63

Current

IsLeft = True

Parent

80

71

52

48

63

Current

IsLeft = False

Page 45: 08 Binary Trees

45UNH-M / NHTI - Professor Yusuf

Node TBD is a RC and has a LC

// Case 2:

// If no right child, replace with left subtree

else if(current.rightChild==null)

if(current == root)

root = current.leftChild;

else if (isLeftChild)

parent.leftChild = current.leftChild;

else

parent.rightChild = current.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;

Parent

80

71

52

48

63

Current

IsLeft = False

cur.leftChild

par.rightChild

Page 46: 08 Binary Trees

46UNH-M / NHTI - Professor Yusuf

Deleting a Node with one Child

Deleting a Node with one Child80

71

52

48

Figure 8.14

Deleting a node with one child.

80

63

52

48

63

67

67

a) Before deletion

b) After deletion

To be deleted

Page 47: 08 Binary Trees

47UNH-M / NHTI - Professor Yusuf

Deleting a node – Case 3Deleting a node – Case 3

• Deleting a Node in the tree when there are two children– Most complicated– Cannot just connect the parent’s link to it’s child’s 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

Page 48: 08 Binary Trees

48UNH-M / NHTI - Professor Yusuf

25

50

35

20

40

30

5

Recursion – Tree TerminologyRecursion – Tree Terminology

15

35

50

40

20

5

15

30

Figure 8.15

Cannot replace with subtree. If node 25 is replaced with node 35, tree looks like this. It is two left children

To be deleted

Root of right

subtree

Which node goes here?

Page 49: 08 Binary Trees

49UNH-M / NHTI - Professor Yusuf

Recursion – Tree TerminologyRecursion – Tree Terminology

25

50

35

20

40

30

5

30

50

35

20

40

5

Figure 8.16

Node 25 replaced by its successor (node 30)

15

To be delete

d

a) Before deletion

b) After deletion

Successor to 25

15

Page 50: 08 Binary Trees

50UNH-M / NHTI - Professor Yusuf

Deleting Nodes – Case 3Deleting 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

Page 51: 08 Binary Trees

51UNH-M / NHTI - Professor Yusuf

Figure 8.17

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

72

90

55

60

74

92

78

43

41

26

38

Go to left child

Go to left child

Successor

No left child

Page 52: 08 Binary Trees

52UNH-M / NHTI - Professor Yusuf

Finding the Successor NodeFinding the Successor Node

72

90

55

60

74

92

78

43

41

Figure 8.17

26

38

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

Go to left child

Go to left child

Successor

No left child

Page 53: 08 Binary Trees

53UNH-M / NHTI - Professor Yusuf

26

26

75

50

26

26

26

75

50

26

Page 54: 08 Binary Trees

54UNH-M / NHTI - Professor Yusuf

DefinitionsDefinitions

• 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

Page 55: 08 Binary Trees

55UNH-M / NHTI - Professor Yusuf

ReferencesReferences

http://en.wikipedia.org/wiki/Main_PageData Structures and Algorithms in Java 2nd Edition. Robert

Lafore, SAMShttp://www.otherwise.com/Lessons/index.html