Beruflich Dokumente
Kultur Dokumente
Answer:
// Driver code
int main()
{
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
print2D(root);
return 0;
}
// of a generic tree
#include <bits/stdc++.h>
struct Node
int key;
vector<Node *>child;
};
temp->key = key;
return temp;
if (root==NULL)
return;
// using queue
while (!q.empty())
int n = q.size();
while (n > 0)
Node * p = q.front();
q.pop();
q.push(p->child[i]);
n--;
// Driver program
int main()
{
* 10
* //\\
* 2 34 56 100
* /\ |/|\
* 77 88 1 7 8 9
*/
(root->child).push_back(newNode(2));
(root->child).push_back(newNode(34));
(root->child).push_back(newNode(56));
(root->child).push_back(newNode(100));
(root->child[0]->child).push_back(newNode(77));
(root->child[0]->child).push_back(newNode(88));
(root->child[2]->child).push_back(newNode(1));
(root->child[3]->child).push_back(newNode(7));
(root->child[3]->child).push_back(newNode(8));
(root->child[3]->child).push_back(newNode(9));
LevelOrderTraversal(root);
return 0;
}
3.Write a program that can input and display a person's family tree.
Answer:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/*
* @version 1.0
* @since 5-9-2015
*/
{
{
/**
*/
Node(String val)
{
value = val;
left = null;
right = null;
}
/**
*/
{
value = val;
left = leftChild;
right = rightChild;
}
}
/**
*/
{
/**
Constructor.
tree to display.
*/
BTreeDisplay(Node tree)
{
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
{
add(rootLabel, BorderLayout.NORTH);
panel.add(new BTreeDisplay(tree.left));
panel.add(new BTreeDisplay(tree.right));
add(panel);
}
}
}
/**
*/
{
}
/**
@return true.
*/
{
return true;}
else
return false;
}
/**
@return true.
*/
{
return false;}
//Adds node
return true;}
else
return false;
}
/**
*/
{
return false;}
//Adds node
return true;}
else
return false;
}
{
}
/**
*/
{
return null;
if (famTree.value.equals(p))
return famTree;
//if left child not null, recursively call locate with left child.
result = locate(p,famTree.left);
//if parent still not found, recursively call right locate passing the right child.
result = locate(p,famTree.right);
return result;
}
}