what is binary tree?
A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root. Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.
package javaapplication1;
public class Node {
Node left;
Node right;
Node parent;
int key;
Node(int key){
this.key=key;
}
Node(Node value)
{
this.left=value.left;
this.right=value.right;
this.parent=value.parent;
this.key=value.key;
}
}
package javaapplication1;
public class Tree {
Node root;
Node rand=root;
Tree()
{
root=null;
}
public void addNode(Node newNode)
{
if(root==null)
{
root=newNode;
}
else
{
Node mover=root;
Node focus=root;
while(mover!=null)
{
focus=mover;
if(mover.key>newNode.key)
{
mover=mover.left;
if(mover==null)
{
focus.left=newNode;
return;
}
}
else
{
mover=mover.right;
if(mover==null)
{
focus.right=newNode;
return;
}
}
}
}
}
public void printTree(Node n)
{
}
}
A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root. Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.
package javaapplication1;
public class Node {
Node left;
Node right;
Node parent;
int key;
Node(int key){
this.key=key;
}
Node(Node value)
{
this.left=value.left;
this.right=value.right;
this.parent=value.parent;
this.key=value.key;
}
}
package javaapplication1;
public class Tree {
Node root;
Node rand=root;
Tree()
{
root=null;
}
public void addNode(Node newNode)
{
if(root==null)
{
root=newNode;
}
else
{
Node mover=root;
Node focus=root;
while(mover!=null)
{
focus=mover;
if(mover.key>newNode.key)
{
mover=mover.left;
if(mover==null)
{
focus.left=newNode;
return;
}
}
else
{
mover=mover.right;
if(mover==null)
{
focus.right=newNode;
return;
}
}
}
}
}
public void printTree(Node n)
{
}
}
->>
package javaapplication1;
public class JavaApplication1 {
public static void main(String[] args) {
Tree Love= new Tree();
Love.addNode(new Node(10));
Love.addNode(new Node(1));
Love.addNode(new Node(2));
Love.addNode(new Node(11));
System.out.println(Love.root.key);
System.out.println(Love.root.left.key);
System.out.println(Love.root.left.right.key);
System.out.println(Love.root.right.key);
Love.printTree(Love.root);
}
}
ConversionConversion EmoticonEmoticon