数据结构之树的基本操作(java版本)

本博客来自慕课网《数据结构探险之树篇》,慕课网主讲老师使用C++实现的,这里我将其改为java实现,

以下是对代码的几点说明:

二叉树:所有节点的度都小于等于2

二叉树的遍历:根据访问根的顺序:前序、中序、后序。
二叉树数组实现:
左孩子下标 = 父节点下标2 + 1;
右孩子下标 = 父节点下标2 + 2;
父节点下标 = (孩子节点下标 - 1) / 2;

测试二叉树的数据如下:

一共是三个类,一个是节点Node类,一个是树Tree类,还有一个是测试数的类,三个类的代码如下所示:

Node类:

package tree;
 
public class Node {
 
	public int index;
	public int data;
	public Node LChild;
	public Node RChild;
	public Node PChild;
	
	public Node()
	{
		index=0;
		data=0;
		LChild=null;
		RChild=null;
		PChild=null;
	}
	
	public Node SearchNode(int nodeIndex)
	{
		if(this.index==nodeIndex)
		{
			return this;
		}
		Node temp=null;
		if(this.LChild!=null)
		{
			if(this.LChild.index==nodeIndex)
			{
				return this.LChild;
			}else{
				temp=this.LChild.SearchNode(nodeIndex);
				if(temp!=null)
					return temp;
			}
		}
		
		if(this.RChild!=null)
		{
			if(this.RChild.index==nodeIndex)
			{
				return this.RChild;
			}else{
				temp=this.RChild.SearchNode(nodeIndex);
				return temp;
			}
		}
		return null;
	}
	void DeleteNode()
	{
		if(this.LChild!=null)
			this.LChild.DeleteNode();
		
		if(this.RChild!=null)
			this.RChild.DeleteNode();
		
		if(this.PChild!=null)
		{
			if(this.PChild.LChild==this)
			{
				this.PChild.LChild=null;
			}
			if(this.PChild.RChild==this)
			{
				this.PChild.RChild=null;
			}
		}
	}
	/**
	 * 前序遍历
	 */
	public void PreorderTraversal()
	{
		System.out.println("index="+index+"  data="+data);
		if(this.LChild!=null)
		{
			this.LChild.PreorderTraversal();
		}
		if(this.RChild!=null)
		{
			this.RChild.PreorderTraversal();
		}		
	}
	/**
	 * 中序遍历
	 */
	public void InorderTraversal()
	{
		
		if(this.LChild!=null)
		{
			this.LChild.InorderTraversal();
		}
		System.out.println("index="+index+"  data="+data);
		if(this.RChild!=null)
		{
			this.RChild.InorderTraversal();
		}
			
	}
	/**
	 * 后序遍历
	 */
	public void PostorderTraversal()
	{	
		if(this.LChild!=null)
		{
			this.LChild.PostorderTraversal();
		}
		if(this.RChild!=null)
		{
			this.RChild.PostorderTraversal();
		}
		System.out.println("index="+index+"  data="+data);	
	}
}

Tree类:

package tree;
 
public class Tree {
 
	public Node pRoot;
	public Tree()
	{
		pRoot=new Node();
	}
	/**
	 * 搜索节点
	 * @param nodeIndex
	 * @return
	 */
	public Node SearchNode(int nodeIndex)
	{
		return pRoot.SearchNode(nodeIndex);
	}
	/**
	 * 添加节点
	 * @param nodeIndex
	 * @param direction
	 * @param node
	 * @return
	 */
	public boolean AddNode(int nodeIndex,int direction,Node node)
	{
		Node tempNode=SearchNode(nodeIndex);
		if(tempNode==null)
			return false;
		Node tNode=new Node();
		if(tNode==null)
		{
			//申请内存失败
			return false;
		}
		tNode.index=node.index;
		tNode.data=node.data;
		tNode.PChild=tempNode;
		if(direction==0)//插入到左节点
		{
			tempNode.LChild=tNode;
		}
		if(direction==1)//插入到右节点
		{
			tempNode.RChild=tNode;
		}
		return true;
	}
	boolean DeleteNode(int nodeIndex,Node node)
	{
		Node temp=SearchNode(nodeIndex);
		if(temp==null)
			return false;
		if(node!=null)
		{
			node.data=temp.data;
		}
		temp.DeleteNode();	
		return true;
	}
	public void DistroyTree()
	{
		//pRoot.DeleteNode();
		DeleteNode(0, null);
	}
	/**
	 * 前序遍历
	 */
	public void PreorderTraversal()
	{
		pRoot.PreorderTraversal();
	}
	/**
	 * 中序遍历
	 */
	public void InorderTraversal()
	{
		pRoot.InorderTraversal();	
	}
	/**
	 * 后序遍历
	 */
	public void PostorderTraversal()
	{	
		pRoot.PostorderTraversal();
	}
}

测试类:

package tree;
 
public class TestTree {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * 			0(0)
		 * 
		 *      5(1)    8(2)
		 *      
		 *   2(3) 6(4) 9(5) 7(6)
		 *   
		 *   前序:0526897-0134256
		 *   中序:2560982-3140526
		 *   后序:2650978-3415620
		 */
		Tree tree=new Tree();
		
		Node node1=new Node();
		node1.index=1;
		node1.data=5;		
		Node node2=new Node();
		node2.index=2;
		node2.data=8;
		
		Node node3=new Node();
		node3.index=3;
		node3.data=2;
		Node node4=new Node();
		node4.index=4;
		node4.data=6;
		Node node5=new Node();
		node5.index=5;
		node5.data=9;		
		Node node6=new Node();
		node6.index=6;
		node6.data=7;
		
		tree.AddNode(0, 0, node1);
		tree.AddNode(0, 1, node2);
		tree.AddNode(1, 0, node3);
		tree.AddNode(1, 1, node4);
		tree.AddNode(2, 0, node5);
		tree.AddNode(2, 1, node6);
		
		tree.DeleteNode(2, null);
		//tree.DeleteNode(5, null);
		//tree.PreorderTraversal();
		//tree.InorderTraversal();
		tree.PostorderTraversal();
	}
 
}

猜你喜欢

转载自blog.csdn.net/yuanheng19930119/article/details/88022578