Escolar Documentos
Profissional Documentos
Cultura Documentos
Terminology
A tree is a collection of nodes and directed edges, satisfying the following properties: There is one specially designated node called the root, which has no edges pointing to it. Every node except the root has exactly one edge pointing to it. There is a unique path (of nodes and edges) from the root to each node. Each item can have multiple successors All items have exactly one predecessor. Except the root
2
Graphical Representation
Trees, as defined on the preceding slide, are typically
drawn with circles (or rectangles) representing the nodes and arrows representing the edges. The root is typically placed at the top of the diagram, with the rest of the tree below it. Trees:
root edge node
Not Trees:
Terminology (Contd.)
If an edge goes from node a to node b, then a is called the parent of b, and b is called a child of a. Children of the same parent are called siblings. If there is a path from a to b, then a is called an ancestor of b, and b is called a descendent of a. A node with all of its descendants is called a subtree. If a node has no children, then it is called a leaf of the tree. If a node has no parent (there will be exactly one of these), then it is the root of the tree.
4
Terminology: Example
A B F D E I J K subtree G H C
A is the root D, E, G, H, J & K are leaves B is the parent of D, E & F D, E & F are siblings and children of B I, J & K are descendants of B A & B are ancestors of I
5
Binary Trees
General trees: Trees with no restrictions on number of children
Terminology (Contd.)
Child and parent Every node except the root has one parent A node can have an arbitrary number of children Leaves Nodes with no children Sibling nodes with same parent
7
Terminology (Contd.)
Path Length number of edges on the path Depth of a node length of the unique path from the root to that node The depth of a tree is equal to the depth of the deepest leaf Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree is equal to the height of the root Ancestor and descendant Proper ancestor and proper descendant
8
Terminology (Contd.)
A binary tree is balanced if the difference in height between any nodes left and right subtree is 1.
Note that: A full binary tree is also complete. A complete binary tree is not always full. Full and complete binary trees are also balanced. Balanced binary trees are not always full or complete.
9
Terminology (Contd.)
10
11
12
60
7
20
3 4
70
10
5
40
6
30
50
14
60
7
20
3 4
70
10
5
40
6
30
50
16
60
7
20
3 4
70
10
5
40
6
30
50
21 34 8 55
Searching BST
If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.
20
findMin/ findMax
Return the node containing the smallest element in the tree Start at the root and go left as long as there is a left child. The stopping point is the smallest element Similarly for findMax Time complexity = O(height of the tree)
21
findMin
findMax
22
Insert 13
Proceed down the tree as you would with a Search If X is found or X is greater than the value in the node visited, take the right path If X is smaller than the value in the node visited We continue until we come to an empty node, then we insert X
23
delete
When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.
24
25
Second Case
The node has one child Adjust a pointer from the parent to bypass that node
26
Third Case The node has 2 children Replace the key of that node with the minimum element in the right subtree (InOrder Successor) OR with the maximum element in the left subtree (Logical Predecessor) Delete the minimum element which would have either no child or only one child (right child for InOrder Successor because if it has a left child, that left child would be smaller and would have been chosen). Now apply the same steps as for First Case or 27 Second Case.
Deleting 6
Replace with 4 (Logical Predecessor) or 7 (InOrder Successor)
28
void set_value(int x) { value=x; } int get_value() { return value; } void set_left(BSTNode l) { left=l; } //next slide
30
BSTNode get_left() { return left; } void set_right(BSTNode r){ right=r; } BSTNode get_right() { return right; } } //End BSTNode
31
public class BST { BSTNode root; public BST() { root=null; } public BSTNode get_root() { return root; }
32
public void InsertElement (int x) { // x points to root of a subtree BSTNode temp=new BSTNode(x); if (root==null) root=temp; else { BSTNode P, T; P=T=root; while(T!=null) { P=T; if(x<T.get_value()) T=T.get_left(); else T=T.get_right(); } if(x<P.get_value()) P.set_left(temp); else P.set_right(temp); } } //end InsertElement 33
public BSTNode SearchElement (int x) { BSTNode T; T=root; while((T!=null)&&(T.get_value()!=x)) { if(x<T.get_value()) T=T.get_left(); else T=T.get_right(); } return T; } // End SearchElement
34
public void deleteElement(int K) // T is the node to be removed and // P is the parent of T. { BSTNode T=root; BSTNode P=root; BSTNode temp; BSTNode x, f, s; x=f=s=null; while((T!=null)&&(T.get_value()!=K)) { P=T; if(K<T.get_value()) T=T.get_left(); else T=T.get_right(); } // next Slide
35
temp=T; // Set x to the node that will replace T; // First two cases: Case of no children or one child if (T==null) System.out.println("\n\tKey does not exist in tree..."); else if(T.get_left()==null) x=T.get_right(); else if(T.get_right()==null) x=T.get_left(); //next Slide
36
else //Third case. T has two children. Set x to the // inorder successor of T and f to the parent of x. { f=T; x=T.get_right(); s=x.get_left(); while(s!=null) { f=x; x=s; s=x.get_left(); } //next Slide
37
// At this point, x is the inorder successor of T. if(f!=T) { // T is not the parent of x, and x==f.left f.set_left(x.get_right()); // remove x from its current position and replace it // with the right child of x // x takes the place of T x.set_right(T.get_right()); } // set the left child of x so that // x takes the place of T x.set_left(T.get_left()); } // end third Case -else
38
// insert x into the position formerly occupied by T. if(P==null) root=x; else if(T==P.get_left()) P.set_left(x); else P.set_right(x); temp=null; //hint to be collected by Garbage Collector } End deleteElement
39
public void PrinBSTNode(BSTNode T) { System.out.print("\t"+ T.get_value()); } public void printPREorder(BSTNode T) { if (T !=null) { PrinBSTNode(T) ; printPREorder(T.get_left()); printPREorder(T.get_right()); } }
40
public void printINorder(BSTNode T) { if (T !=null) { printINorder(T.get_left()); PrinBSTNode(T) ; printINorder(T.get_right()); } } public void printPOSTorder(BSTNode T) { if (T !=null) { printPOSTorder(T.get_left()); printPOSTorder(T.get_right()); PrinBSTNode(T); } }
41
public void removeAll(BSTNode T) { if(T!=null) { //Traverse the left subtree in postorder removeAll(T.get_left()); //Traverse the right subtree in postorder removeAll(T.get_right()); //Delete the leaf node from the tree T=null; //hint to be collected by Garbage Collector } } }//end class
42
Activity
1. Write a BSTApp class to create and manipulate a BST object (making the different method calls). Create the BST by inputting 60, 70, 20, 10, 40, 50, 30 in an initially empty BST. 2. Write a method LeafCount to count the number of leaves in a given BST. Test the method in BSTApp. 3. Write a method SingleChildCount to return the number of nodes that have only one child. Test the method in BSTApp.
43