Professional Documents
Culture Documents
Tree
Non-tree
Term of tree
Parent: any node (except the root) has exactly one edge running upward to another node. Child: any node may have one or more lines running downward to other nodes. Leaf: a node that has no children Subtree: any node may be considered to be the root of a subtree, which consists of its children, and its childrens children, and so on
Traversing: to visit all the nodes in some specified order. For example, you might visit all the nodes in order of ascending key value Levels: how many generations the nodes is from the root. It assumed, the root is level 0, then its children will be level 1, and so on. Keys: one data field in an object
Binary Tree
If every node in a tree can have at most two children Two children of each node in binary tree are called left child and right child The kind of binary tree that will be discussed is binary search tree
// // // //
data used as key value other data this nodes left child this nodes right child
class Tree { private Node root; public void find (int key) public void insert(int id, double dd) public void delete (int id) // various other methods }
Finding a Node
Finding node 57 Starts at the root. If key is less, then it goes to the left right. Otherwise, it goes to the right side
{ if (key<current.iData) // go left current = current.leftChild; else current = current.rightChild; // or go right? if (current == null) // if no child, return null; // didnt find it } return current; // found it }
Tree Efficiency
The time required to find anode depends on how many levels down it is situated. This is O(log2 N) time
Inserting a Node
To insert a node, we must first find the place to insert it. Example: to insert a new node 45
InOrder Traversal
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); } } This method is initially called with the root as an argument: inOrder(root)
PreOrder Traversal
1. Visit the node 2. Call itself to traverse the nodes left subtree 3. Call itself to traverse the nodes right subtree
PostOrder Traversal
1. Call itself to traverse the nodes left subtree 2. Call itself to traverse the nodes right subtree 3. Visit the node
Finding Minimum & Maximum value of a Tree For the minimum, go to the left child of the root; then go to the left child of that child and so on
Deleting a Node
It is started by finding the node that want to delete. There are three cases:
1. The node to be deleted is a leaf (has no children) 2. The node to be deleted has one child 3. The node to be deleted has two children
Case 1: The Node to be Deleted has No Children To delete a leaf node, it just change the appropriate child field in the nodes parent to point to null, instead of to the node.
To delete a node with two children, replace the node with its inorder successor
if (successor != delNode.rightChild)// if successor not { // right child, make connections successorParent.leftChild=successor.rightChild; successor.rightChild=delNode.rightChild; } return successor; }
// if successor is not right child, make connections if (successor !=delNode.rightChild) { successorParent.leftChild=successor.rightChild; successor.rightChild=delNode.rightChild; }