Binary Tree

public class BinaryTree
{

Node root;

public void addNode(int key, String name)
{

// Create a new Node and initialize it

Node newNode = new Node(key, name);

// If there is no root this becomes root

if (root == null)
{

root = newNode;

}
else
{

// Set root as the Node we will start
// with as we traverse the tree

Node focusNode = root;

// Future parent for our new Node

Node parent;

while (true)
{

// root is the top parent so we start
// there

parent = focusNode;

// Check if the new node should go on
// the left side of the parent node

if (key < focusNode.key)
{

// Switch focus to the left child

focusNode = focusNode.leftChild;

// If the left child has no children

if (focusNode == null)
{

// then place the new node on the left of it

parent.leftChild = newNode;
return; // All Done

}

}
else
{ // If we get here put the node on the right

focusNode = focusNode.rightChild;

// If the right child has no children

if (focusNode == null)
{

// then place the new node on the right of it

parent.rightChild = newNode;
return; // All Done

}

}

}
}

}

// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth

public void inOrderTraverseTree(Node focusNode)
{

if (focusNode != null)
{

// Traverse the left node

inOrderTraverseTree(focusNode.leftChild);

// Visit the currently focused on node

System.out.println(focusNode);

// Traverse the right node

inOrderTraverseTree(focusNode.rightChild);

}

}

public void preorderTraverseTree(Node focusNode)
{

if (focusNode != null)
{

System.out.println(focusNode);

preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);

}

}

public void postOrderTraverseTree(Node focusNode)
{

if (focusNode != null)
{

postOrderTraverseTree(focusNode.leftChild);
postOrderTraverseTree(focusNode.rightChild);

System.out.println(focusNode);

}

}

public Node findNode(int key)
{

// Start at the top of the tree

Node focusNode = root;

// While we haven't found the Node
// keep looking

while (focusNode.key != key)
{

// If we should search to the left

if (key < focusNode.key)
{

// Shift the focus Node to the left child

focusNode = focusNode.leftChild;

}
else
{

// Shift the focus Node to the right child

focusNode = focusNode.rightChild;

}

// The node wasn't found

if (focusNode == null)
return null;

}

return focusNode;

}

public static void main(String[] args)
{

BinaryTree theTree = new BinaryTree();

theTree.addNode(50, "Boss");

theTree.addNode(25, "Vice President");

theTree.addNode(15, "Office Manager");

theTree.addNode(30, "Secretary");

theTree.addNode(75, "Sales Manager");

theTree.addNode(85, "Salesman 1");

// Different ways to traverse binary trees

theTree.inOrderTraverseTree(theTree.root);

System.out.println();
theTree.preorderTraverseTree(theTree.root);
System.out.println();
theTree.postOrderTraverseTree(theTree.root);

// Find the node with key 75

System.out.println("\nNode with the key 75");

System.out.println(theTree.findNode(75));

}
}

class Node
{

int key;
String name;

Node leftChild;
Node rightChild;

Node(int key, String name)
{

this.key = key;
this.name = name;

}

@Override
public String toString()
{

return name + " has the key " + key;

/*
* return name + " has the key " + key + "\nLeft Child: " + leftChild + "\nRight Child: " +
* rightChild + "\n";
*/

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值