The process of converting the general
tree to a binary tree is as follows:
1. use the root of
the general tree as the root of the binary tree
2. Determine the first child of the root. This is the leftmost node in
the general tree at the next
level
3. Insert this node. The child reference of the parent node refers to
this node
4. Continue finding the first child of each parent node and insert it
below the parent node with
the child reference of the parent to this node.
5. When no more first children exist in the path just used, move
back to the parent of the last
node entered and repeat the above process. In other words, determine
the first sibling of the
last node entered.
6. Complete the tree for all nodes. In
order to locate where the node fits you must search for the
first child at that level and then follow the sibling references to a
nil where the next sibling
can be inserted. The children of any sibling node can be inserted by
locating the parent and
then inserting the first child. Then the above process is repeated.
|
Tree* lookUp(struct Tree *node, char
key)
|
if(node == NULL) return node;
|
if(node->data == key) return node;
|
return lookUp(node->right, key) ;
|
else
return lookUp(node->left, key);
|
Tree* leftMost(struct Tree *node)
|
if(node == NULL) return NULL;
|
while(node->left != NULL)
|
struct Tree *newTreeNode(int data)
|
struct Tree* insertTreeNode(struct
Tree *node, int data)
|
return lookUp(node->left, key);
|
Tree* leftMost(struct Tree *node)
|
if(node == NULL) return NULL;
|
while(node->left != NULL)
|
struct Tree *newTreeNode(int data)
|
struct Tree* insertTreeNode(struct
Tree *node, int data)
|
if(node != NULL) p = node;
|
retNode = newTreeNode(data);
|
if(data <= node->data ) {
|
node->left =
insertTreeNode(node->left,data);
|
node->right =
insertTreeNode(node->right,data);
|
void isBST(struct Tree *node)
|
static int lastData = INT_MIN;
|
/* check if the given tree is BST */
|
if(lastData < node->data)
|
else {
|
cout << "Not a BST"
<< endl;
|
int treeSize(struct Tree *node)
|
if(node == NULL) return 0;
|
return treeSize(node->left) + 1 +
treeSize(node->right);
|
int maxDepth(struct Tree *node)
|
if(node == NULL) return 0;
|
int leftDepth =
maxDepth(node->left);
|
int rightDepth =
maxDepth(node->right);
|
return leftDepth > rightDepth ?
|
leftDepth + 1 : rightDepth + 1;
|
int minDepth(struct Tree *node)
|
{
|
if(node == NULL) return 0;
|
int leftDepth =
minDepth(node->left);
|
int rightDepth =
minDepth(node->right);
|
return leftDepth < rightDepth ?
|
leftDepth + 1 : rightDepth + 1;
|
bool isBalanced(struct Tree *node)
|
if(maxDepth(node)-minDepth(node)
<= 1)
|
Tree* minTree(struct Tree *node)
|
if(node == NULL) return NULL;
|
Tree* maxTree(struct Tree *node)
|
/* In Order Successor - a node which
has the next higher key */
|
Tree *succesorInOrder(struct Tree
*node)
|
/* if the node has right child,
seccessor is Tree-Minimum */
|
if(node->right != NULL) return
minTree(node->right);
|
while(y != NULL && node ==
y->right) {
|
/* In Order Predecessor - a node
which has the next lower key */
|
Tree *predecessorInOrder(struct Tree
*node)
|
/* if the node has left child,
predecessor is Tree-Maximum */
|
if(node->left != NULL) return
maxTree(node->left);
|
/* if it does not have a left child,
|
predecessor is its first left
ancestor */
|
while(y != NULL && node ==
y->left) {
|
Tree *lowestCommonAncestor(Tree
*root, Tree *p, Tree *q)
|
if(root == NULL) return NULL;
|
if(root->left == p ||
root->left == q
|
|| root->right == p ||
root->right == q) return root;
|
left =
lowestCommonAncestor(root->left,p,q);
|
right =
lowestCommonAncestor(root->right, p,q);
|
return (left) ? left : right;
|
void clear(struct Tree *node)
|
clear(node->left);
|
/* print tree in order */
|
/* 1. Traverse the left subtree.
|
3. Traverse the right subtree.
|
void printTreeInOrder(struct Tree
*node)
|
static int lastData = INT_MIN;
|
printTreeInOrder(node->left);
|
cout << node->data <<
" ";
|
printTreeInOrder(node->right);
|
/* print tree in postorder*/
|
/* 1. Traverse the left subtree.
|
2. Traverse the right subtree.
|
void printTreePostOrder(struct Tree
*node)
|
if(node == NULL) return;
|
printTreePostOrder(node->left);
|
printTreePostOrder(node->right);
|
cout << node->data <<
" ";
|
2. Traverse the left subtree.
|
3. Traverse the right subtree.
|
void printTreePreOrder(struct Tree
*node)
|
cout << node->data <<
" ";
|
printTreePreOrder(node->left);
|
printTreePreOrder(node->right);
|
void printThisPath(int path[], int n)
|
for(int i = 0; i < n; i++)
|
cout << (char)path[i] <<
" ";
|
}
/* recursion routine to find path */
|
void pathFinder(struct Tree *node,
int path[], int pathLength)
|
path[pathLength++] = node->data;
|
/* Leaf node is the end of a path.
|
So, let's print the path */
|
if(node->left == NULL &&
node->right == NULL)
|
printThisPath(path, pathLength);
|
pathFinder(node->left, path,
pathLength);
|
pathFinder(node->right, path,
pathLength);
|
Given a binary tree, print out all of
its root-to-leaf
|
paths, one per line. Uses a recursive
helper to do the work. */
|
void printAllPaths(struct Tree *root)
|
bool matchTree(Tree *r1, Tree *r2)
|
{
|
/* Nothing left in the subtree */
|
if(r1 == NULL && r2 == NULL)
|
/* Big tree empty and subtree not found
*/
|
if(r1 == NULL || r2 == NULL)
|
return (matchTree(r1->left,
r2->left) &&
|
matchTree(r1->right,
r2->right));
|
bool subTree(Tree *r1, Tree *r2)
|
/*Big tree empty and subtree not found
*/
|
if(matchTree(r1, r2)) return true;
|
return (subTree(r1->left, r2) ||
subTree(r1->right, r2));
|
bool isSubTree(Tree *r1, Tree *r2)
|
/* Empty tree is subtree */
|
return true;
|
/* change a tree so that the roles of
the left
|
and right hand pointers are swapped
at every node */
|
/* create a new tree from a sorted
array */
|
Tree *addToBST(char arr[], int start,
int end)
|
if(end < start) return NULL;
|
int mid = (start + end)/2;
|
r->data = arr[mid];
|
r->left = addToBST(arr, start,
mid-1);
|
r->right = addToBST(arr, mid+1,
end);
|
Tree *createMinimalBST(char arr[],
int size)
|
return addToBST(arr,0,size-1);
|
/* Breadth first traversal using
queue */
|
void BreadthFirstTraversal(Tree
*root)
|
if (root == NULL) return;
|
queue.push_back(p->left);
|
queue.push_back(p->right);
|
cout << endl;
|
/* find n-th max node from a tree */
|
void NthMax(struct Tree* t)
|
cout << n_th_max <<
"-th maximum data is " << t->data << endl;
|
/* Converting a BST into an Array */
|
void TreeToArray(struct Tree *node,
int a[]){
|
TreeToArray(node->left,a);
|
TreeToArray(node->right,a);
|
int main(int argc, char **argv)
|
{
|
=
{'A','B','C','D','E','F','G','H','I'};
|
Tree *root = newTreeNode('F');
|
insertTreeNode(root,'B');
|
insertTreeNode(root,'A');
|
insertTreeNode(root,'D');
|
insertTreeNode(root,'C');
|
insertTreeNode(root,'E');
|
insertTreeNode(root,'G');
|
insertTreeNode(root,'I');
|
insertTreeNode(root,'H');
|
cout << "size = "
<< treeSize(root) << endl;
|
cout << "max depth =
" << maxDepth(root) << endl;
|
/* min depth */
|
cout << "min depth =
" << minDepth(root) << endl;
|
cout << "This tree is
balanced!\n";
|
cout << "This tree is not
balanced!\n";
|
/* min value of the tree*/
|
cout << "Min value =
" << minTree(root)->data << endl;
|
/* max value of the tree*/
|
cout << "Max value =
" << maxTree(root)->data << endl;
|
cout << "Min value of
subtree " << ch << " as a root is "
|
<< minTree(found)->data
<< endl;
|
cout << "Max value of
subtree " << ch << " as a root is "
|
<< maxTree(found)->data
<< endl;
|
if(found) {
|
succ = succesorInOrder(found);
|
cout << "In Order
Successor of " << ch << " is "
|
<< succesorInOrder(found)->data
<< endl;
|
cout << "In Order
Successor of " << ch << " is None\n";
|
succ = succesorInOrder(found);
|
cout << "In Order
Successor of " << ch << " is "
|
<<
succesorInOrder(found)->data << endl;
|
cout << "In Order
Successor of " << ch << " is None\n";
|
succ = succesorInOrder(found);
|
cout << "In Order
Successor of " << ch << " is "
|
<<
succesorInOrder(found)->data << endl;
|
cout << "In Order Successor of " << ch
<< " is None\n";
|
pred = predecessorInOrder(found);
|
cout << "In Order
Predecessor of " << ch << " is "
|
<<
predecessorInOrder(found)->data << endl;
|
cout << "In Order
Predecessor of " << ch << " is None\n";
|
pred = predecessorInOrder(found);
|
cout << "In Order
Predecessor of " << ch << " is "
|
<<
predecessorInOrder(found)->data << endl;
|
cout << "In Order
Predecessor of " << ch << " is None\n";
|
pred = predecessorInOrder(found);
|
cout << "In Order
Predecessor of " << ch << " is "
|
<<
predecessorInOrder(found)->data << endl;
|
cout << "In Order
Predecessor of " << ch << " is None\n";
|
/* Lowest Common Ancestor */
|
lowestCommonAncestor(root,
|
lookUp(root,ch1), lookUp(root,ch2));
|
cout << "The lowest common
ancestor of " << ch1 << " and "
|
<< ch2 << " is
" << ancestor->data << endl;
|
lowestCommonAncestor(root,
|
lookUp(root,ch1), lookUp(root,ch2));
|
cout << "The lowest common
ancestor of " << ch1 << " and "
|
<< ch2 << " is
" << ancestor->data << endl;
|
/* print tree in order */
|
cout << "increasing sort order\n";
|
/* print tree in postorder*/
|
printTreePostOrder(root);
|
/* print tree in preorder*/
|
cout << found->data <<
" is in the tree\n";
|
cout << ch << " is
not in the tree\n";
|
cout << found->data <<
" is in the tree\n";
|
cout << ch << " is not in the tree\n";
|
Given a binary tree, print out all of
its root-to-leaf
|
paths, one per line. Uses a recursive
helper to do the work. */
|
cout << "printing paths
..." << endl;
|
/* find n-th maximum node */
|
/* convert the tree into an array */
|
int treeSz = treeSize(root);
|
int *array = new int[treeSz];
|
for (int i = 0; i < treeSz; i++)
|
cout << (char)array[i] <<
' ';
|
Tree *root2 = newTreeNode('D');
|
insertTreeNode(root2,'C');
|
insertTreeNode(root2,'E');
|
cout << "1-2 subtree:
" << isSubTree(root, root2) << endl;
|
Tree *root3 = newTreeNode('B');
|
insertTreeNode(root3,'A');
|
insertTreeNode(root3,'D');
|
insertTreeNode(root3,'C');
|
insertTreeNode(root3,'E');
|
cout << "1-3 subtree:
" << isSubTree(root, root3) << endl;
|
Tree *root4 = newTreeNode('B');
|
insertTreeNode(root4,'D');
|
insertTreeNode(root4,'C');
|
insertTreeNode(root4,'E');
|
cout << "1-4 subtree:
" << isSubTree(root, root4) << endl;
|
cout << "2-3 subtree:
" << isSubTree(root2, root3) << endl;
|
cout << "3-2 subtree:
" << isSubTree(root3, root2) << endl;
|
/* swap left and right */
|
/* make a new tree with minimal depth
*/
|
Tree *newRoot =
createMinimalBST(charArr,9);
|
/* Traversing level-order.
|
We visit every node on a level before
going to a lower level.
|
This is also called Breadth-first
traversal.*/
|
cout << "printing with Breadth-first traversal"
<< endl;
|
BreadthFirstTraversal(newRoot);
|
}
|
|
No comments:
Post a Comment